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] _delete_by_query not implemented #59

Closed jord-nijhuis closed 1 year ago

jord-nijhuis commented 1 year ago

Describe the enhancement

The _delete_by_query is not implemented yet.

See https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html#docs-delete-by-query-api-request

Why is this needed?

A useful method of deleting a large set of documents without having to remove them individually.

How do you think it should be done?

A deleteBy or deletebyQuery function on the SearchClient (and perhaps on the IndexRepository that allows users to delete documents based on a query. I believe a large part of the searchDsl could be reused.

For the moment, I am using this extension function, but this is quite limited and I have only implemented everyting necessairy for my own purposes:

    private suspend fun SearchClient.deleteByQuery(target: String?, block: (SearchDSL.() -> Unit)? = null): JsonObject {
        val dsl = SearchDSL()
        block?.invoke(dsl)

        return this.restClient.post {
            path(target, "_delete_by_query")

            val rawJson = dsl.json()
            if (rawJson.isNotBlank()) {
                rawBody(rawJson)
            }
        }.parseJsonObject()
    }

Will you be able to help with a pull request? Given that this is my first time working with Elasticsearch (and this library, which works great by the way!), I doubt I can be of much assistance. Perhaps if this feature still missing after a while that I will try to tackle it.

jillesvangurp commented 1 year ago

Thanks, I will consider adding this. Useful to have indeed. A work around is doing this with the bulk dsl and a deep paging query instead.