@asymmetrik/elastic-querybuilder
Advanced tools
Comparing version 0.9.5 to 0.9.6
@@ -161,2 +161,49 @@ const QueryBuilder = require('../src/index'); | ||
test('should build multiple nested aggregations with the same path', () => { | ||
const query = new QueryBuilder() | ||
.query('match_all') | ||
/** | ||
* Note this is not the ideal way to build these, you should just | ||
* add the locations.state aggregation inside the first nesting. However, | ||
* sometimes these are built dynamically and from config files, and may not | ||
* be structured in a way that makes that easy, so building should be aware | ||
* that there is already an agg at the same path and merge the aggregations | ||
* instead of overriding them. | ||
*/ | ||
.aggs('nested', { path: 'locations' }, builder => builder | ||
.aggs('terms', 'locations.city') | ||
) | ||
.aggs('nested', { path: 'locations' }, builder => builder | ||
.aggs('terms', 'locations.state') | ||
) | ||
.build(); | ||
expect(query).toEqual({ | ||
from: 0, | ||
size: 15, | ||
query: { | ||
match_all: {} | ||
}, | ||
aggs: { | ||
locations: { | ||
nested: { | ||
path: 'locations' | ||
}, | ||
aggs: { | ||
'locations.city': { | ||
terms: { | ||
field: 'locations.city' | ||
} | ||
}, | ||
'locations.state': { | ||
terms: { | ||
field: 'locations.state' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
test('should allow filters to be added to a normal aggregation', () => { | ||
@@ -163,0 +210,0 @@ const query = new QueryBuilder() |
{ | ||
"name": "@asymmetrik/elastic-querybuilder", | ||
"version": "0.9.5", | ||
"version": "0.9.6", | ||
"description": "A query builder for Elasticsearch.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -160,3 +160,9 @@ const invariant = require('./invariant'); | ||
collection[getAggName(type, field)] = agg; | ||
const name = getAggName(type, field); | ||
// Handle nested aggregations with the same path, which ends up being the same name | ||
if (nested.aggs && collection[name] && collection[name].aggs) { | ||
Object.assign(collection[name].aggs, nested.aggs); | ||
} else { | ||
collection[name] = agg; | ||
} | ||
}; | ||
@@ -163,0 +169,0 @@ |
155050
2013