- two sample documents
post /aggstest/test/1
{ "categories": [ { "type": "book", "words": [ {"word":"storm","count":277}, {"word":"pooh","count":229} ] }, { "type": "magazine", "words": [ {"word":"vibe","count":100}, {"word":"sunny","count":50} ] } ] }
post /aggstest/test/2
{ "categories": [ { "type": "book", "words": [ {"word":"rain","count":160}, {"word":"jurassic park","count":150} ] }, { "type": "megazine", "words": [ {"word":"tech","count":200}, {"word":"homes","count":30} ] } ] }
- aggs query
get /aggstest/test/_search
{ "size": 0, "query": { "filtered": { "filter": { "bool": { "must": [ { "term": { "categories.type": "book" } }, { "term": { "categories.words.word": "storm" } } ] } } } }, "aggs": { "filtered": { "filter": { "bool": { "must": [ { "term": { "categories.type": "book" } } ] } }, "aggs": { "book_category": { "terms": { "field": "categories.words.word", "size": 10 } } } } }, "post_filter": { "term": { "categories.type": "book" } } }
result
{ "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0, "hits": [] }, "aggregations": { "filtered": { "doc_count": 1, "book_category": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "pooh", "doc_count": 1 }, { "key": "storm", "doc_count": 1 }, { "key": "sunny", "doc_count": 1 }, { "key": "vibe", "doc_count": 1 } ] } } } }
========================
expected aggs result set should not include "sunny" , "vibe" because it's "magazine" type.
i used filter query , post_filter, couldn't "book" type aggs result.
all filters apply (in-query , in-aggregation) still return whole categories
document. , document, contains 4 words, scope aggregation. hence 4 buckets. far understand, way manipulate buckets on server-side introduced reducers in version 2.0 of elasticsearch.
what may use changing mapping categories
nested object. hence you'll able query them independently , aggregate accordingly using nested aggregation. changing object type nested requires reindexing.
also please note post-filters not applied aggregation whatsoever. used filter original query without affecting aggregation when need aggregate on wider scope returned hits.
and 1 more thing, if have filter in query there's no need put in aggregation, scope filtered.
Comments
Post a Comment