05、ES实战:范围查询-时间范围查询示例

Java 范围查询(时间范围查询示例)

RangeQueryBuilder 构建范围查询条件
可以使用 RangeQueryBuilder 作为时间范围查询。
示例如下:

public class EsRange{
   
     
    public void RangeSearch(TransportClient client){
   
     
        //时间范围的设定
        RangeQueryBuilder rangequerybuilder = QueryBuilders
                    .rangeQuery("inputtime")
                    .from("2016-7-21 00:00:01").to("2016-7-21 00:00:03");
        //生成DSL查询语句
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(rangequerybuilder);

        //System.out.println(sourceBuilder.toString());

        //查询建立
        SearchRequestBuilder responsebuilder = client
                                .prepareSearch("pointdata")
                                .setTypes("pointdata");
        SearchResponse myresponse=responsebuilder
                    .setQuery(QueryBuilders.boolQuery() 
                    .must(QueryBuilders.matchPhraseQuery //must表示and                    
                        ("pointid","W3.UNIT1.10LBG01CP301")
                    )
                    .must(rangequerybuilder))
                    .setFrom(0).setSize(50) //分页
                    .setExplain(true)
                    .execute()
                    .actionGet();
        SearchHits hits = myresponse.getHits();
        for(int i = 0; i < hits.getHits().length; i++) {
   
     
            System.out.println(hits.getHits()[i].getSourceAsString());
        }
    }
}

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