سؤال

I'm trying to build a query which satisfies condition1 and condition2. Here condition2 is in itself kind of subquery.

CID:1234 AND (NOT FIELD2: STR)
CID:1234 AND NOT FIELD2: STR

In the above 2nd query behaves as expected but the first doesn't. You might tell me to remove the braces but as i said condition2 is a subquery it could also be:

CID:1234 AND (NOT FIELD2: STR AND (FIELD3: ABC OR FIELD4: XYZ))

Condition 2 is dynamic. Its possible that it has only NOT clause or may have more.

Query required for: Give all docs which have CID 1234 and it also satisfy the following in together: 1) docs with FIELD1 not equal to STR and FIELD2 not equal to STR2,

هل كانت مفيدة؟

المحلول

The problems stems from a basic misunderstanding of how lucene queries work.

SQL queries imply a starting point of a full table of results, which are then filtered down. Lucene queries do not. You start with no results and find them through the provided query.

NOT FIELD2:STR means: prohibit FIELD2:STR. Nothing prohibit Something is still nothing. It does not mean get everything but FIELD2:STR. That would be something like *:* -FIELD2:STR, where *:* is a MatchAllDocsQuery (Note: The lucene query parser does not support *:*. Solr does, Lucene doesn't). You can support this sort of query construction, but it's generally inadvisable, since it requires the entire set of documents in the index to be enumerated, which can result in very poor performance.

Lucene's +/- syntax is much more clear and flexible than it's confusing AND/OR/NOT syntax. +/- are how lucene queries are constructed (lucene terminology uses the labels SHOULD, MUST and MUST_NOT). AND/OR/NOT get interpreted into those terms, and the interpretation is imperfect and leads to confusing issues like this.

For further reading on that topic: Why Not AND, OR, and NOT?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top