3

I have been struggling to form a solr field-query with AND and OR operator. why solr returns different result for 1 and 2, 3 and 4 queries even all queries have same logic-

  1. fq=(name:abc AND -city: ( 1 3 )) OR (name:abc AND -loc:(3 K D 5 7))
  2. fq=(name:abc AND (-city: ( 1 3 ) OR -loc:(3 K D 5 7)))
  3. fq=name:abc&fq=-(city:(1 3) AND loc:(3 K D 5 7))
  4. fq=name:abc&fq=(-city:(1 3) OR -loc:(3 K D 5 7))

could anyone please help me understand how solr internally execute above queries?

Rahul Sharma
  • 5,614
  • 10
  • 57
  • 91
  • See Yonik's answer w.r.t. parsing of "pure negative" queries: http://stackoverflow.com/questions/634765/using-or-and-not-in-solr-query/642452#21409106 – Peter Dixon-Moses Oct 08 '15 at 02:28
  • Thanks for reply, I was logically thinking from the perspective of Demorgan's law and according to it 3 and 4 both are same-"not (A and B)" is the same as "(not A) or (not B)" – Rahul Sharma Oct 08 '15 at 06:38
  • I've gotten hung up here in the past as well. Not sure the rationale for the inconsistent behavior. – Peter Dixon-Moses Oct 08 '15 at 14:06
  • Still i have not got answer from the link you suggested. i want to know how solr internally runs these expression. – Rahul Sharma Oct 09 '15 at 01:06

2 Answers2

8

This inconsistency is a known issue, and there's an open ticket: https://issues.apache.org/jira/browse/SOLR-3744

This thread covers is in simpler terms: Weird Solr/Lucene behaviors with boolean operators

You have some "pure negative" nested queries in your expressions that The Lucene query parser expects "negative queries" to be expressed together with a a positive selection query. In other words anything but city:foo is correctly written as *:* AND -city:foo.

Try your test with the following changes:

 1. <Same>

 2. fq=(name:abc AND (*:* AND -city:(1 3)) OR (*:* AND -loc:(3 K D 5 7)))

 3. <Same> 

 4. fq=name:abc&fq=((*:* AND -city:(1 3)) OR (*:* AND -loc:(3 K D 5 7)))

And see if things tie back up correctly.

Community
  • 1
  • 1
Peter Dixon-Moses
  • 3,169
  • 14
  • 18
  • When you pass *debugQuery=true* you want your parsed query to look something like `+name:abc -( +city:(1 3) +loc:(3 K D 5 7) )` in all cases. – Peter Dixon-Moses Oct 09 '15 at 03:29
2

Even *:* AND -city:foo does not seem to work reliably for me when I use both q and fq - e.g., a variant of #4 as below does not work:

4b. q=name:abc AND ((*:* AND -city:(1 3)) OR (*:* AND -loc:(3 K D 5 7)))

What works for me is to use id:* instead of *:* when id is some required field. Now any variants work.

4c. q=name:abc AND ((id:* AND -city:(1 3)) OR (id:* AND -loc:(3 K D 5 7)))
4d. q=name:abc&fq=((id:* AND -city:(1 3)) OR (id:* AND -loc:(3 K D 5 7)))
user1533634
  • 431
  • 4
  • 5