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

Add support for script based sorting in SortBuilder #131

Closed yonghee12 closed 2 months ago

yonghee12 commented 2 months ago

Implements #130

This PR introduces support for script-based sorting in SortBuilder, in addition to the existing field name-based sorting functionality.

With this change, you can now sort documents using custom scripts.

For example:

SearchDSL().apply {
    sort {
        addScript(
            order = SortOrder.ASC,
            type = "number",
            source = "doc['field_name'].value * params.factor;",
            lang = "painless",
            params = mapOf("factor" to 1.1),
        )
        add(
            field = "field_name",
            order = SortOrder.DESC,
            mode = SortMode.AVG,
        )
    }
}