11
votes

Can someone explain to me what the difference between must_not and filter is in elasticsearch?

E.g. here (taken from elasticsearch definitive guide), why isn't must_not also used for the range?

{
    "bool": {
        "must":     { "match": { "title": "how to make millions" }},
        "must_not": { "match": { "tag":   "spam" }},
        "should": [
            { "match": { "tag": "starred" }}
        ],
        "filter": {
          "range": { "date": { "gte": "2014-01-01" }} 
        }
    }
}

Specifically looking at this documentation, it appears to me that they are exactly the same:

filter: The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.

must_not: The clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned.

2
Basically, filter = must but without scoring and must_not = !must (or !filter)Val
I thought so too, but the second documentation suggests that both filter and must_not are executed in the filter context without scoring?schneida
It makes no sense to use scoring for a must_not since documents are excluded from the search and hence canot be scoredVal
Can you explain how would you create the same must_not constraint as above by using filter instead of must_not?Val
Absolutely right, filter = must but without scoring, nothing more.Val

2 Answers

13
votes

The filter is used when the matched documents need to be shown in the result, while must_not is used when the matched documents will not be shown in the results. For further analysis:

filter:

  1. It is written in Filter context.
  2. It does not affect the score of the result.
  3. The matched query results will appear in the result.
  4. Exact match based, not partial match.

must_not:

  1. It is written again on the same filter context.
  2. Which means it will not affect the score of the result.
  3. The documents matched with this condition will NOT appear in the result.
  4. Exact match based.

Tabular comparision

6
votes

Basically, filter = must but without scoring.

must_not expresses a condition that MUST NOT be met, while filter (and must) express conditions that MUST be met in order for a document to be selected.