简体   繁体   中英

ElasticSearch Multi Term Query With Java High-Level REST Client

The java-high-level-rest-client provides a method to search on elasticsearch using a term that Shown below its code

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); 
sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy")); 

But I want to do a query on multiple fields like this: "user"="kimchy" and/or "city"="london".

I see the multi-search query and multi-match query but they don't do what I want.

Thanks for the help!!

try this:

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder
    .filter(QueryBuilders.termQuery("user", "kimchy"))
    .filter(QueryBuilders.termQuery("city", "london"));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(boolQueryBuilder);

the filter/must/should method on BoolQueryBuilder , depends on which context you want, if you want "or", you can use should .

One can try this.

BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
            foreach (var term in (s + ',' + t).Split(','))
            {
                booleanQuery.add(new TermQuery(new Term("content", term)), BooleanClause.Occur.SHOULD);
            }
            //booleanQuery.add(new TermQuery(new Term("content", s)), BooleanClause.Occur.MUST);
            //booleanQuery.add(new TermQuery(new Term("content", t)), BooleanClause.Occur.MUST);
            TopDocs hits = searcher.search(booleanQuery.build(), int.MaxValue);

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