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] top_hits aggregation is not implemented #56

Closed NikkyAI closed 1 year ago

NikkyAI commented 1 year ago

Describe the enhancement

missing aggregation: top_hits

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

How do you think it should be done?


class TopHitsAggConfig : JsonDsl() {
    var aggSize by property<Long>("size") // can't redefine Map.size sadly
    var from by property<Long>()
    var sort by property<List<Any>>()
}

fun TopHitsAggConfig.sort(block: SortBuilder.() -> Unit) {
    val builder = SortBuilder()
    block.invoke(builder)
    sort = builder.sortFields
}

class TopHitsAgg(block: (TopHitsAggConfig.() -> Unit)? = null) : AggQuery("top_hits") {
    init {
        val config = TopHitsAggConfig()
        block?.invoke(config)
        put(name, config)
    }
}
@Serializable
data class TopHitsAggregationResult(
    val hits: List<SearchResponse.Hit>,
)
fun Aggregations?.topHitResult(name: String, json: Json = DEFAULT_JSON): TopHitsAggregationResult =
    getAggResult(name, json)

Will you be able to help with a pull request?

probably will open a pull request once enough stuff accumulates