0

I am trying to apply a Subquery in filters in Jena Sparql.Is this possible.If yes,how?eg:

SELECT ?x WHERE(?y <xyz:> ?z . ?y <abc:> ?x .FILTER regex(?z,subquery,"i")}

I mean I want to give the expression in filter using some subquery in jena..How Can I do it?If not whats its replacement?

cooljohny
  • 656
  • 5
  • 13
  • 31
  • It's not entirely clear what you're asking, @cooljohny. You won't be able to embed the subquery right in filter expression, but you can certainly use a subquery to provide the values that you'll use in the filter expression. – Joshua Taylor Jul 02 '14 at 11:53

1 Answers1

2

You can't put the subquery in the filter expression, because the subquery isn't an expression with a value. You can use a subquery to provide the values that you use in a filter expression, though. E.g.,

# Find persons whose names are also the names of flowers (Rose, Daisy, etc.) by
# performing a subquery to find all the flower names, and then finding people 
# whose names match those names.
select ?person where {
  ?person a :Person ;
          :name ?name .
  filter regex(?name,?flowerName, "i" )

  { select ?flowerName { ?flower a :Flower ; :name ?flowerName } }
}
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thanks for your answer ..this is exactly what I wanted...just one more doubt, in the regex filter the pattern I match for is and it is there in the database but when I query it using filter it gives nothing. Are there any restrictions on pattern in regex filters such as characters like + - . @ cannot be used? – cooljohny Jul 02 '14 at 14:15
  • @cooljohny well, it is a regexp, so characters like + typically mean `"at least one". If you're looking for an exact match, there's no reason to use a regexp, though; just query for the exact value. – Joshua Taylor Jul 02 '14 at 14:49
  • actually I am not looking for an exact match..its just a part of the whole string and if my pattern contains + etc what can I do? – cooljohny Jul 02 '14 at 15:02
  • If it's just containment, use the [contains](http://www.w3.org/TR/sparql11-query/#func-contains) function. That should be cheaper than doing a regex. – Joshua Taylor Jul 02 '14 at 15:56
  • But I don't want true or false as answer I want to query using thatWhat could be the replacement of regex(If I want to use + etc)? – cooljohny Jul 02 '14 at 16:21
  • "But I don't want true or false as answer" I don't understand what you're asking. regex returns true or false, just like contains does. If you're looking for, e.g., `?name` that contains `?flowerName`, you just do `filter contains( ?name, ?flowerName )`. – Joshua Taylor Jul 02 '14 at 16:24
  • 2
    Sorry I misunderstood something..an once again Thanks ! I really appreciate your help and you really helped me a lot with all my questions. :) – cooljohny Jul 02 '14 at 16:42