Correct way to filter a query using Elasticsearch? (filter with filter)

I am trying to work if there is a difference between “filters” and “filtered queries” in Elasticsearch.

The two query examples below return the same results when they run against my index.

Are they really different from each other in any subtle way?

Is there a reason someone prefers each other in different situations?

DSL gives one top level query , and one top level filter :

 GET /index/type/_search?_source { "query": { "multi_match": { "query": "my dog has fleas", "fields": ["name", "keywords"] } }, "filter": { "term": {"status": 2} } } 

DSL only gives the top level query using the filtered construct:

 GET /index/type/_search?_source { "query": { "filtered": { "query": { "multi_match": { "query": "my dog has fleas", "fields": ["name", "keywords"] } }, "filter": { "term": {"status": 2} } } } } 
+6
source share
1 answer

The first example is post_filter , which is not optimal in terms of performance. Filtered queries are preferable since filters will run before queries. Typically, you want your filters to run first, since scoring documents are more expensive than just a logical pass / crash. This way your result set is cut out before you run your query. With post_filter, your query is run first, the entire result is set, and then the filter is applied to the results.

The top-level filter directive was deprecated in 1.0 and was renamed post_filter to clarify its purpose and use.

the top-level filter parameter in the search was renamed to post_filter to indicate that it should not be used as the main method of filtering search results (a filtered query is used instead), but only for filtering the results AFTER faces / aggregates were calculated.

http://www.elastic.co/guide/en/elasticsearch/reference/current/_search_requests.html

+11
source

Source: https://habr.com/ru/post/983981/


All Articles