07、ES实战:多条件查询

多条件设置
//多条件设置
MatchPhraseQueryBuilder mpq1 = QueryBuilders
                .matchPhraseQuery("pointid","W3.UNIT1.10LBG01CP301");
MatchPhraseQueryBuilder mpq2 = QueryBuilders
                .matchPhraseQuery("inputtime","2016-07-21 00:00:01");
QueryBuilder qb2 = QueryBuilders.boolQuery().must(mpq1).must(mpq2);

实现多条件查询
public class EsBool{
   
     
    public void BoolSearch(TransportClient client){
   
     
        //多条件设置
        MatchPhraseQueryBuilder mpq1 = QueryBuilders
            .matchPhraseQuery("pointid","W3.UNIT1.10LBG01CP301");
        MatchPhraseQueryBuilder mpq2 = QueryBuilders
            .matchPhraseQuery("inputtime","2016-07-21 00:00:01");
        QueryBuilder qb2 = QueryBuilders.boolQuery()   
                                .must(mpq1)   
                                .must(mpq2);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(qb2);
        //System.out.println(sourceBuilder.toString());

        //查询建立
        SearchRequestBuilder responsebuilder = client
                .prepareSearch("pointdata").setTypes("pointdata");
        SearchResponse myresponse=responsebuilder
                    .setQuery(qb2)
                    .setFrom(0).setSize(50)
                    .addSort("inputtime", SortOrder.ASC)
                    //.addSort("inputtime", SortOrder.DESC)
                    .setExplain(true).execute().actionGet();
            SearchHits hits = myresponse.getHits();
            for(int i = 0; i < hits.getHits().length; i++) {
   
     
                System.out.println(hits.getHits()[i].getSourceAsString());

            }
    }
}

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: