[FEAT] Filter aggregations
Closed this issue · 2 comments
jelle-blaauw commented
Describe the enhancement
I'd like to be able to do filter aggregations, like so:
"aggs": {
"my-agg-name": {
"filter": {
"bool": {
"must": [{
"nested": {
"path": "someField",
"query": {
"bool": {
"must": [{
"exists": { "field": "someField" }
}, {
"term": {"someField.somePath": "someValue"}
}]
}
}
}
}]
}
}
},
...
}
Why is this needed?
It's an ElasticSearch feature, so it should be in this lib as well.
How do you think it should be done?
I think an extension to AggQuery
can be made which uses a BoolQuery
as the "body".
Will you be able to help with a pull request?
Maybe.
jelle-blaauw commented
I was actually able to come up with this:
class FilterQuery : ESQuery(name = "filter")
class FilterAgg(
block: (FilterQuery.() -> ESQuery)
) : AggQuery("filter") {
init {
val config = FilterQuery()
val query = block.invoke(config)
config[query.name] = query
put(name, config)
}
}
which can be used as follows:
agg("my-agg-name", FilterAgg {
bool {
must(
nested {
path = "someField"
query = bool {
must(
exists("someField"),
terms("someField.somePath", "someValue"),
)
}
}
)
}
})
It can probably improved, and there still needs to be a clean way to retrieve the results, but at least it works.
jillesvangurp commented
Cool thanks! That looks like it should work. When I find some time, I'll squeeze in a pull request. Might be a while because I'm super busy right now.