How to create advanced queries with a series of AND/OR's?

From semantic-mediawiki.org

How would one create a query like this: get a list of persons where the hair color is brown or black and eye color is blue or green?

The reason im asking this is because im unsure of how precedence is handled in these queries. For example if i wrote:

#ask: [[Category:Person]] [[Hair color::brown]] OR [[Hair color::black]] [[Eye color::blue]] OR [[Eye color::green]]

Would the above query actually be correct? would smw group the logic as: ((hair = brown or black) and (eyes = blue or green)) or would the logic be grouped as: (hair = brown or (hair=black and eyes=blue) or eyes = green)?

08:46, 14 October 2013

On IRC you said you found the answer; for completeness an answer here as well:

  • Firstly, you can combine OR for property values: [[<property>::<value1>||<value2>||.....]]
  • Secondly, you cannot use brackets to group unions/disjoints, so grouping selection criteria works standard left-to-right, with AND stronger than OR.

Therefore, your question could be answered
[[Category:Person]][[Hair color::brown||black]] OR [[Category:Person]][[Eye color::blue||green]]
You can read this as
( [[Category:Person]][[Hair color::brown||black]] ) OR ( [[Category:Person]][[Eye color::blue||green]] )
Since you can't actually use the brackets, you need to repeat the common part for every disjoint selection, as shown with the Category selector.

13:28, 14 October 2013

You can also use sub queries

<q>[[Category:Person]] [[Hair color::brown]]</q> OR <q>[[Hair color::black]] [[Eye color::blue]]</q> OR [[Eye color::green]]

Not sure which forms are the most efficient. Probably not this one.

02:10, 16 October 2013

Thanks, Jeroen, for showing us another method, although I'm not sure I quite understand. The only use case of ... I was previously aware of is the one outlined in this section: [1]. If you're suggesting that ... can also be used in the same way as the rounded brackets above, then it would be powerful piece of syntax that is worth documenting.

15:35, 16 October 2013