Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
buildSharedQuery(query: Object) {
const modifiedQuery = super.buildSharedQuery(query)
/**
* Modify query to perform aggregation on unique users,
* to avoid duplicate counts of multiple work histories or education items
* for the same user
**/
const pieces = nestedField.split(".")
if (pieces.length !== 3) {
throw new Error(`Assumed three pieces but found ${pieces.length}`)
}
const cardinality = CardinalityMetric("count", "user_id")
const aggsContainer = AggsContainer(countName, { reverse_nested: {} }, [
cardinality
])
const termsBucket = TermsBucket(
nestedField,
nestedField,
{},
aggsContainer
)
const nestedBucket = NestedBucket(
"inner",
`${pieces[0]}.${pieces[1]}`,
termsBucket
)
return modifiedQuery.setAggs(
AggsContainer(
function ReverseNestedTermsBucket(key, field, options) {
const reverseNestedAgg = AggsContainer(REVERSE_NESTED_AGG_KEY, {
reverse_nested: {}
})
return TermsBucket(key, field, options, reverseNestedAgg)
}
cardinality
])
const termsBucket = TermsBucket(
nestedField,
nestedField,
{},
aggsContainer
)
const nestedBucket = NestedBucket(
"inner",
`${pieces[0]}.${pieces[1]}`,
termsBucket
)
return modifiedQuery.setAggs(
AggsContainer(
`${this.uuid}`,
{
filter: {}
},
[nestedBucket]
)
)
}