-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
Comments
Not sure I followed. Can you show me the final desired elasticsearch query you're aiming for? |
The final query I want to achieve is this : But if I have the list of aggregations to be nested in an array like this: 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? |
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(); |
Thanks, that works. |
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?
The text was updated successfully, but these errors were encountered: