31

i need to do not queries on my lucene index. Lucene currently allows not only when we have two or more terms in the query:

So I can do something like:

country:canada not sweden

but I can't run a query like:

country:not sweden

Could you please let me know if there is some efficient solution for this problem

Thanks

5 Answers 5

58

A very late reply, but it might be useful for somebody else later:

*:* AND NOT country:sweden

IF I'm not mistaken this should do a logical "AND" with all documents and the documents with a country that is different from "sweden".

1
  • I was struggling because the lucene StandardQueryParser was not correctly parsing this "-country:sweden". Apparently the parser converted it to "country:sweden". This comment clarified a common use case for me.
    – Gnafu
    Jan 28, 2019 at 13:26
15

Try with the following query in the search box:

NOT message:"warning"

message being the search field

2
  • 3
    Important: It is case sensitive
    – tread
    Apr 1, 2020 at 12:07
  • 2
    Important: “NOT” and field name are case sensitive. Field value is not.
    – Павле
    Nov 18, 2020 at 18:10
5

Please check answer for similar question. The solution is to use MatchAllDocsQuery.

0
3

The short answer is that this is not possible using the standard Lucene.

Lucene does not allow NOT queries as a single term for the same reason it does not allow prefix queries - to perform either, the engine would have to look through each document to ascertain whether the document is/is not a hit. It has to look through each document because it cannot use the search term as the key to look up documents in the inverted index (used to store the indexed documents).

To take your case as an example:

To search for not sweden, the simplest (and possibly most efficient) approach would be to search for sweden and then "invert" the result set to return all documents that are not in that result set. Doing this would require finding all the required (ie. not in the result set) documents in the index, but without a key to look them up by. This would be done by iterating over the documents in the index - a task it is not optimised for, and hence speed would suffer.

If you really need this functionality, you could maintain your own list of items when indexing, so that a not sweden search becomes a sweden search using Lucene, followed by an inversion of the results using your set of items.

1

OK, I see what you are trying to do.

You can use it as a query refinement since there are no unary Boolean operators in Lucene. Despite the answers above, I believe this is a better and most forward approach (note the space before the wildcard):

&query= *&qf=-country:Canada

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.