使用Java高级REST客户端进行ElasticSearch长期查询

时间:2019-10-05 07:37:02

标签: java elasticsearch

java-high-level-rest-client提供了一种使用其代码下方显示的术语在Elasticsearch上进行搜索的方法

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

但是我想对多个字段进行查询,例如:“ user” =“ kimchy”和/或“ city” =“ london”。

我看到了multi-search查询和多重匹配查询,但它们没有满足我的要求。

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

尝试一下:

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

filter/must/should上的BoolQueryBuilder方法取决于您想要的上下文,如果要“或”,则可以使用should

答案 1 :(得分:0)

可以尝试一下。

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);