es按时间段统计总数

SearchRequestBuilder searchRequestBuilder = client.prepareSearch().setIndices("ishangzu-service-contract-log-2017.07.10")

.setTypes("service-contract-log").setSearchType(SearchType.COUNT);

DateHistogramBuilder agg = AggregationBuilders.dateHistogram("by_time").field("@timestamp").interval(60000);

//日期过滤

BoolQueryBuilder bool=QueryBuilders.boolQuery();

BoolQueryBuilder dateQuery=QueryBuilders.boolQuery();

dateQuery.must(QueryBuilders.rangeQuery("@timestamp").from("1499616000000"));

dateQuery.must(QueryBuilders.rangeQuery("@timestamp").to("1499688000000"));

bool.must(dateQuery);

//filter agg

FilterAggregationBuilder filter=AggregationBuilders.filter("filter").filter(dateQuery).subAggregation(agg);

searchRequestBuilder.addAggregation(filter);

System.out.println(searchRequestBuilder);

//统计

SearchResponse searchResponse=null;

try {

searchResponse = searchRequestBuilder.get();

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(searchResponse);

posted on 2017-07-11 15:38  lovlos  阅读(7845)  评论(0编辑  收藏  举报