jillesvangurp / kt-search

Multi platform kotlin client for Elasticsearch & Opensearch with easily extendable Kotlin DSLs for queries, mappings, bulk, and more.
MIT License
95 stars 23 forks source link

[FEAT] Filter aggregations #67

Closed jelle-blaauw closed 1 year ago

jelle-blaauw commented 1 year ago

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 1 year ago

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 1 year ago

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.