我该如何查询?
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))