How to Use Sub Aggregation in Elastic Search
Posted By : Amit Patel | 31-Jul-2019
Aggregations-
In ElasticSearch the aggregations' framework helps provide aggregated data based on a search query. And it's based on simple building blocks that are called aggregations, that can be composed in order to build complex summaries of the data.
An aggregation can be seen as a unit-of-work where he builds analytic information over a set of documents. The context of the execution of aggregation defines what this document set is (e.g. a top-level aggregation executes within the context of the executed query/filters or bool query of the search request).
Filter context-
In the filter context, a query clause answers the question “Does this document match this query clause?” The answer is a simple Yes or N. No scores are calculated. Filter context is generally used for filtering structured data.
Routinely used filters will be cached automatically by Elasticsearch, to increase the speed of performance.
Whenever a query clause is passed to a filter parameter the filter context is in effect, such as the filter or must_not parameters in the bool query, the filter parameter in the constant_score query, or the filter aggregation.
Filter Aggregation-
In filter aggregation, defines a single bucket of all the documents in the current document set context that match a specified filter. Frequently this will be used to narrow down the current aggregation context to a specific set of documents.
Example of sub aggregation-
GET lsdata/_search { "size": 0, "aggs": { "current": { "filter": { "bool": { "must":[ { "term": { "groupId": { "value": 696327 } } }, { "nested": { "path": "types", "query": { "bool": { "must": [ { "terms": { "types.id": [ "5" ] } } ] } } } }, { "range": { "creationDate": { "from": "now-30d/d", "to": "now", "include_lower": false, "include_upper": true } } } ] } }, "aggs": { "typeId": { "terms": { "field": "typeId", "size": 10 } } } }, "previous": { "filter": { "bool": { "must":[ { "term": { "groupId": { "value": 696327 } } }, { "nested": { "path": "types", "query": { "bool": { "must": [ { "terms": { "types.id": [ "5" ] } } ] } } } }, { "range": { "creationDate": { "from": "now-60d/d", "to": "now-30d/d", "include_lower": false, "include_upper": true } } } ] } }, "aggs": { "typeId": { "terms": { "field": "typeId", "size": 10 } } } } } }
Response-
{ "took" : 154, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 646580, "max_score" : 0.0, "hits" : [ ] }, "aggregations" : { "current" : { "doc_count" : 10, "typeId" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : 5, "doc_count" : 10 } ] } }, "previous" : { "doc_count" : 7, "typeId" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : 5, "doc_count" : 7 } ] } } } }
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Amit Patel
Amit Patel is having good knowledge of java,have expertise in hibernate.