danpaz / bodybuilder

An elasticsearch query body builder :muscle:

Home Page:http://bodybuilder.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

issue related to aggregation

swetarai763 opened this issue · comments

i need to understand this clause which is written by using bodybuilder

body.aggregation('terms', underfined, 'terms', elasticAggregationHelper.termOptions('type','_count'));

I am not able to understand this termOptions and not able to find it on bodybuilder site
could you please guide me @danpaz
Thanks

The last parameter is for nested aggregation (agg with-in an agg /sub agg).
A simple example would be this. Let's say I'm generating an aggregation for tags, but with-in that aggregation I want to aggregation the publishers of those tags.

You can think of it as a sub-query

I'd do something like this :

bodybuilder()
.aggregation('terms', 'tags.keyword', { size: 10 }, 'top-tags', (a1) => {
      return a1.aggregation('terms', 'publishers.keyword', { size: 10 }, 'top-publishers')
    }).build()
{
  "aggs": {
    "top-tags": {
      "terms": {
        "field": "tags.keyword",
        "size": 10
      },
      "aggs": {
        "top-publishers": {
          "terms": {
            "field": "publishers.keyword",
            "size": 10
          }
        }
      }
    }
  }
}