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.
A filter in Elasticsearch is all about applying some conditions inside the query that are used to narrow down the matching result set.
The difference between query filters and report filtersFilters you apply to the query definition are called query filters. You use query filters to reduce the amount of data retrieved from the data source.
The bool query is a go-to query because it allows you to construct an advanced query by chaining together several simple ones. The results must match the queries in this clause. If you have multiple queries, every single one must match. Acts as an and operator.
Term queryedit. Returns documents that contain an exact term in a provided field. You can use the term query to find documents based on a precise value such as a price, a product ID, or a username. Avoid using the term query for text fields.
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:
must_not:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With