You haven't shown your data, and your queries change in odd ways (e.g., using a different URI for the ww:
prefix), so it's hard to say what any of the queries that you've provided you actually return. However, it is certainly possible to use more than one condition with ||
in a SPARQL filter.
When the individuals have values for all the properties
Using the values
construct, we can provide some data in the query and see that a multi-way ||
works:
prefix : <http://stackoverflow.com/q/23561351/1281433/>
select ?s where {
values (?s ?gr ?les ?eq ?low ?up) {
(:s1 3 0 0 0 0)
(:s2 0 3 0 0 0)
(:s3 0 0 3 0 0)
(:s4 0 0 0 3 0)
(:s5 0 0 0 0 3)
(:s6 0 0 0 0 0)
(:s7 1 1 1 1 1)
}
filter ( ?eq > 2 || ?les > 2 || ?gr > 2 || ?low > 2 || ?up > 2 )
}
Note that the values associated with :s6
and :s7
don't have any values greater than two, so we don't expect to see them in the output. The rest each have a value that will make one of the disjuncts true. Here are the query results:
-------
| s |
=======
| :s1 |
| :s2 |
| :s3 |
| :s4 |
| :s5 |
-------
If the conditions are the same
In the example above, as in the original version of the question, the constraint on each variable was the same. If that's the case, you can make this query shorter by using a disjunctive property path, or using values
. Using a disjunctive property path, you could write:
select ?s where {
?s :prop1|:prop2|:prop3|:prop4 ?value
filter ( ?value > 2 )
}
Using values, you could write:
select ?s where {
values ?prop { :prop1 :prop2 :prop3 :prop4 }
?s ?prop ?value
filter ( ?value > 2 )
}
Since you mentioned interaction with optional
, note that each of these has the advantage that ?s
doesn't have to have values defined for all the properties, but rather if ?s
has a value greater than two for any of the specified properties, you'll be able to find it. In some of your original queries, where you have patterns analogous to
?s :prop1 ?value1 ;
:prop2 ?value2 ;
:prop3 ?value3 .
you only get bindings for ?s
for resources that have a value for :prop1
and a value for :prop2
and a value for :prop3
.
When the individuals don't value values for all the properties
In the case that the individuals don't all have values for all the properties, things become a bit trickier, because you need to optionally match the values, since some won't be present. This isn't much harder though. Just put the parts that might not be present inside of optional
blocks:
select ?s where {
?s a :desiredType .
optional { ?s :prop1 ?value1 }
optional { ?s :prop2 ?value2 }
optional { ?s :prop3 ?value3 }
filter ( ?value1 > 2 || ?value2 < 5 || ?value3 = 42 )
}