简体   繁体   中英

ElasticSearch. How to produce “$in” with QueryBuilders

I have code:

List<String> values = new ArrayList<>();
BoolQueryBuilder queryBuilder = boolQuery();
queryBuilder.must(matchQuery("field", values));

I need search in Elastic like this query in mongo:

{
     field : {'$in' : values}
}

How to do this with Elastic? My code search in last item in List values.

UPDATE (found solution myself, thanks all):

queryBuilder.must(termsQuery("field", values));

Check the terms filter: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-filter.html

{
    "constant_score" : {
        "filter" : {
            "terms" : { "user" : ["kimchy", "elasticsearch"]}
        }
    }
}

使用“ termsQuery”代替“ matchQuery”:

queryBuilder.must(termsQuery("field", values));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM