Search code examples
javalucenesearch-engineelasticsearch

Query Builders - Must Not / Should Not


I am new to the search-engine scene and I was wondering if anyone might be able to help me clarify the Must/MustNot and Should/ShouldNot search queries.


My understanding is as follows:

The Must/MustNot queries are absolute definitions (ie, the result must/must not contain a specified field)

The Should/ShouldNot queries are relative definitions (ie, in some scenarios it should/should not contain a specified fields)


If this is correct, could someone please provide me with an example of when you would use Should/ShouldNot...and if my understanding is completely wrong - would someone be kind enough to explain it for me (or point me to a good site)?

Thanks!

Ps. I am using elastic-search (based on Lucene) - but any examples / explanations are welcome


Solution

  • Your understanding is right. SHOULD NOT results get lower weight when found, while MUST NOT never included.

    SHOULD NOT is used when the term is likely belongs to a different domain, but could in some cases still be on the same document with the one I'm looking for. For instance, Google knows I'm a Java programmer. When I ask "ant" the first result would be "Apache Ant". I may use MUST NOT as "-Apache", or I may use SHOULD NOT "Apache" just in case some page describes a special place of ants in Apache natives worldview.

    Or, for instance, I'm looking for a free software to do a task; still, I would consider a paid one if it is not expensive or they have a personal license. Therefore I'd use SHOULD NOT "buy now", and the results would include paid software, but lower in the list.