user2722667
user2722667

Reputation: 8651

What is the difference between MUST and SHOULD in an Elasticsearch bool query?

What is the difference between must and should in a bool query in Elasticsearch?

If I ONLY want results that contain my terms, should I then use must ?

For example, I have a query that should only contain certain values, and should also have no results that has a date/timestamp lower than today's (time/date - NOW). Also, can I use multiple filters inside a must query like the following code?

"filtered": {
"filter": {
"bool" : {
        "must" : {
            "term" : { "type" : 1 }
            "term" : { "totals" : 14 }
            "term" : { "groupId" : 3 }
            "range" : {
                "expires" : {
                    "gte": "now"
                }
            }
        },

Upvotes: 309

Views: 263424

Answers (3)

Utsav Dawn
Utsav Dawn

Reputation: 8246

must means: The clause (query) must appear in matching documents. These clauses must match, like logical AND.

should means: At least one of these clauses must match, like logical OR.

Basically they are used like logical operators AND and OR. See this.

Now in a bool query:

must means: Clauses that must match for the document to be included.

should means: If these clauses match, they increase the _score; otherwise, they have no effect. They are simply used to refine the relevance score for each document.


Yes you can use multiple filters inside must.

Upvotes: 479

Heschoon
Heschoon

Reputation: 3019

As said in the documentation:

Must: The clause (query) must appear in matching documents.

Should: The clause (query) should appear in the matching document. In a boolean query with no must clauses, one or more should clauses must match a document. The minimum number of should clauses to match can be set using the minimum_should_match parameter.

In other words, results will have to be matched by all the queries present in the must clause ( or match at least one of the should clauses if there is no must clause.

Since you want your results to satisfy all the queries, you should use must.


You can indeed use filters inside a boolean query.

Upvotes: 29

TautrimasPajarskas
TautrimasPajarskas

Reputation: 2796

Since this is a popular question, I would like to add that in Elasticsearch version 2 things changed a bit.

Instead of filtered query, one should use bool query in the top level.

If you don't care about the score of must parts, then put those parts into filter key. No scoring means faster search. Also, Elasticsearch will automatically figure out, whether to cache them, etc. must_not is equally valid for caching.

Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

Also, mind that "gte": "now" cannot be cached, because of millisecond granularity. Use two ranges in a must clause: one with now/1h and another with now so that the first can be cached for a while and the second for precise filtering accelerated on a smaller result set.

Upvotes: 29

Related Questions