如何进行这样的查询?
REST API 查询
{ "query": { "must_not": [ {"match": {"foreignId": 1}}, {"match": {"foreignId": 2}}, ... ] }
Kotlin 代码
fun searchWithExclude(foreignIdsForMustNot: List<Int>) {
val q = QueryBuilders.boolQuery()
.mustNot(QueryBuilders.matchQuery("id", foreignIdsForMustNot[0]))
.mustNot(QueryBuilders.matchQuery("id", foreignIdsForMustNot[1]))
...
}
searchWithExclude(listOf(1, 2, ...))
最佳答案
我会使用 terms
查询来这样做,这样您就可以简单地在参数中传递列表:
val q = QueryBuilders.boolQuery()
.mustNot(QueryBuilders.termsQuery("id", foreignIdsForMustNot))
关于java - Elasticsearch:如何调用与数组长度不一样的Java高级REST客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58390088/