apache / lucene

Apache Lucene open-source search software
https://lucene.apache.org/
Apache License 2.0
2.63k stars 1.02k forks source link

QueryParser parses query differently depending on the default operator [LUCENE-7550] #8601

Closed asfimport closed 7 years ago

asfimport commented 7 years ago

As explained by Paweł Róg on java-user [1], the output of parsing the queries below is different depending on the default operator. This looks odd and should be investigated.

QueryParser parser = new QueryParser("test", new WhitespaceAnalyzer());

    parser.setDefaultOperator(QueryParser.Operator.AND);
    Query query = parser.parse("foo AND bar OR baz ");
    System.out.println(query.toString());

    parser.setDefaultOperator(QueryParser.Operator.OR);
    query = parser.parse("foo AND bar OR baz ");
    System.out.println(query.toString());

Results in :

+test:foo test:bar test:baz
+test:foo +test:bar test:baz

[1] http://mail-archives.apache.org/mod_mbox/lucene-java-user/201611.mbox/browser


Migrated from LUCENE-7550 by Dawid Weiss (@dweiss), resolved Feb 13 2017

asfimport commented 7 years ago

Dawid Weiss (@dweiss) (migrated from JIRA)

Sorry to return to this so late. This is a known behavior of how QueryParser works, Paweł.

Quoting Hoss: "If the default operator is set to “And” then the behavior is just plain weird.". You can read about the Boolean logic and query parser behavior at [1]. Also, check out PrecedenceQueryParser which should return the result you expect.

[1] https://lucidworks.com/2011/12/28/why-not-and-or-and-not/