Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested aggregation using a loop #62

Closed
vipulbhavsar94 opened this issue Sep 29, 2016 · 4 comments
Closed

Nested aggregation using a loop #62

vipulbhavsar94 opened this issue Sep 29, 2016 · 4 comments

Comments

@vipulbhavsar94
Copy link

Suppose if I have a list of aggregations which I want to nest in an array, how should I go about being able to do that since it expects a parameter for the nested aggregations which means that I'll have to put all the aggregations in the single expression itself and I can't do it by iterating over the loop and inserting the nested aggregations somehow, any other way to achieve this?

@danpaz
Copy link
Owner

danpaz commented Sep 29, 2016

Not sure I followed. Can you show me the final desired elasticsearch query you're aiming for?

@vipulbhavsar94
Copy link
Author

vipulbhavsar94 commented Sep 30, 2016

The final query I want to achieve is this :
{ "aggregations": { "location_wise": { "terms": { "field": "xAxis" }, "aggs": { "agg_terms_name": { "terms": { "field": "name" }, "aggs": { "agg_terms_class": { "terms": { "field": "class" } } } } } }, "stats": { "stats": { "field": "number of schools" } } } }
I know I can achieve this as :
var body = new BodyBuilder() .aggregation('terms', "xAxis", 'location_wise',null, agg=>agg.aggregation('terms','name',null,null, agg=>agg.aggregation('terms','class'))) .aggregation('stats', 'number of schools', 'stats') .build();

But if I have the list of aggregations to be nested in an array like this:
[{ "aggregationType:"terms", "aggregationField:"name" }, { "aggregationType:"terms", "aggregationField:"class" }]

If these were multiple aggregations, I could have simply used multi line expressions and traversed through the array, but I can't do so in case of nested aggregations, any other way to achieve this?

@danpaz
Copy link
Owner

danpaz commented Oct 1, 2016

I see, so you'd like to programatically generate arbitrarily nested aggregations from an array of aggregation definitions. Though this is more of a javascript question and not really related to the bodybuilder library, I played around with it a I think something like this will work:

var statements = [{ 
  "aggregationType":"terms", 
  "aggregationField":"name" 
}, { 
  "aggregationType":"terms", 
  "aggregationField":"class" 
}, { 
  "aggregationType":"terms", 
  "aggregationField":"grades" 
}]

new Bodybuilder()
  .aggregation('terms', "xAxis", 'location_wise', null, (agg) => {
    var index = 0;
    var nestedAgg;
    function makeNestedAgg(aggBuilder) {
      if (!statements[index]) return nestedAgg;
      var type = statements[index].aggregationType;
      var field = statements[index].aggregationField;
      index++;
      return aggBuilder.aggregation(type, field, (agg) => makeNestedAgg(nestedAgg = agg));
    }
    return makeNestedAgg(agg);
  }) 
  .aggregation('stats', 'number of schools', 'stats') 
  .build();

@danpaz danpaz closed this as completed Oct 1, 2016
@vipulbhavsar94
Copy link
Author

Thanks, that works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants