Question

test:1 AND NOT bool:true

returns 5 documents

but

test:1 AND (NOT bool:true)

returns 0 documents

Why?

Please Explain me the value of parentheses in lucene query formation

Était-ce utile?

La solution

When you place (NOT bool:true) in parentheses it becomes a subquery, which is executed independent of the query test:1. NOT clauses in Lucene ONLY remove elements from the result set, they don't find anything. In SQL, for instance, you implicitly start with every value available, and filter elements which don't match clauses out. In Lucene, you start with nothing, and find results based on the clauses. The query NOT bool:true tells it what not to match, but doesn't give Lucene anything to find and return. Any query of the form:

(any query finding results) AND (NOT something)

Will find zero results, because, on it's own, NOT something finds nothing, and (something) AND (nothing) returns nothing. You can perform a search like that, by getting all values first, before the lonely NOT clause, like:

test:1 AND (*:* AND NOT bool:true)

However, that will perform very poorly, and your first example:

test:1 AND NOT bool:true

Is definitely the correct one.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top