mbuhot / eskotlin

Elasticsearch Query DSL for Kotlin
MIT License
136 stars 28 forks source link

Support multiple [must, should, filter] #7

Closed AndreasVolkmann closed 7 years ago

AndreasVolkmann commented 7 years ago

Hi there,

I just tried building a bool query like so:

fun makeQuery(terms: Map<String, String>) = mbuhot.eskotlin.query.compound.bool {
    terms.forEach { (key, value) ->
        must {
            match {
                key to {
                    query = value
                    fuzziness = Fuzziness.AUTO
                }
            }
        }
    }
}

However, it only renders the last match in the Map, instead of all.

I changed the BoolData.must() to the following to make it work:

fun must(f: () -> QueryBuilder) {
    must = must?.plus(f()) ?: listOf(f())
}
mbuhot commented 7 years ago

@goreRatzete: Here's how to achieve that with the current API

val query = bool {
    must = terms.map { (key, value) ->
        match {
            key to {
                query = value
                fuzziness = Fuzziness.AUTO
            }
        }
    }
}