ElasticSearch - how to exclude a filter from aggregates?

I filtered the query with three filters: "query": "iphone", color:5, category:10, brand:25 .

How can I get the number of products in each brand with color:5 and category:10 ?

In Solr, I did it like this:

 fq={!tag=tag_facet_brand}facet_brand:"564"&facet.field={!ex=tag_facet_brand}facet_brand 

How can I exclude 1 filter from the aggregation context? (I cannot use global because I am losing query:iphone , I cannot use post_filter - due to performance).

+6
source share
2 answers

We are just moving from SOLR, and we are facing the same problem.

You can try the global bucket and then add a query filter for each bucket:

 "filter" : { "query" : { "query_string" : { "query" : "iPhone" } } } 

You should end up with something like:

 { "size": 10, "query": { "query_string": { "query": "iphone" } }, "filter": { "bool": { "must": [ { "term": { "brand": "564" } }, { "term": { "color": "5" } }, { "term": { "category": "10" } } ] } }, "aggs": { "all": { "global": {}, "aggs": { "keyword": { "filter": { "bool": { "must": [ { "query": { "query_string": { "query": "iphone" } } }, { "term": { "color": "5" } }, { "term": { "category": "10" } } ] } }, "aggs": { "brand": { "terms": { "field": "brand" } } } } } } } } 
+5
source

In addition to @Lionel Sengkouvanh's answer, here is the equivalent of the java API:

 SearchRequestBuilder request = client .prepareSearch(INDEX_NAME) .setQuery(theQuery) ... .addAggregation( AggregationBuilders.global("all").subAggregation( AggregationBuilders.filter("keyword").filter(filter).subAggregation( AggregationBuilders.terms("brand").field("brand") ) ) ); 
+2
source

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


All Articles