你的分享就是我们的动力 ---﹥

elastic按小时统计当日数据

elastic按小时统计当天数据
要实现这样一个功能
select count(1),小时 from user where createDate=今天 group by 每小时
原本以为很简单的一个聚合很容易找到相关资料,没想到花了好几个小时

首先建一个测试的index和type,分别为ksearch_test和user,
建立mapping

http://ip:port/ksearch_test/user/_mapping  PUT

{
  "user":{
    "properties":{
      "name":{
        "type":"string",
        "index":"not_analyzed" //不分词
      },
      "createDate":{
        "type":"date",
        "index":"not_analyzed"
      }
    }
  }
}



实现聚合,按每小时统计当天数据:
http://ip:port/ksearch_test/user/_search  POST
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "date_histogram": {          //es提供的时间处理函数
        "field": "createDate",     //需要聚合的字段名字
        "interval": "hour",        //按小时group by
        "format": "yyyy-MM-dd HH", //返回值格式化,HH大写,不然不能区分上午、下午
        "min_doc_count": 0,        //为空填充0
        "extended_bounds": {       //需要填充0的范围
          "min": "2016-12-19 01",
          "max": "2016-12-19 23"
        }
      }
    }
  },
  "query": { //这就是where
    "filtered": {
      "filter": {
        "range": {
          "createDate": {
            "gte": "2016-12-19T00:00:00.000",
            "lt": "2016-12-20T00:00:00.000"
          }
        }
      }
    }
  }
}


使用spring-data-elasticsearch在java中实现
@SuppressWarnings("deprecation")
	@Test
	public void group_by_day(){
		String indexName = "ksearch_test3";
		String typeName = "ks_user_trace";
		
		//QueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.queryFilter(QueryBuilders.rangeQuery("create_time").gte("2016-11-19T01:00:00.000").lt("2016-12-19T00:00:00.000")));
		QueryBuilder queryBuilder = QueryBuilders.queryFilter(QueryBuilders.rangeQuery("create_time").gte("2016-11-19T01:00:00.000").lt("2016-12-19T00:00:00.000"));
	
		
		AbstractAggregationBuilder aggregation = AggregationBuilders.dateHistogram("group_by_day").field("create_time").interval(DateHistogramInterval.DAY).format("yyyy-MM-dd").minDocCount(0L).extendedBounds("2016-11-19", "2016-12-19");
		
		SearchRequestBuilder reqBuilder = client.prepareSearch(indexName)
				.setTypes(typeName)
				.setQuery(queryBuilder)
				.addAggregation(aggregation);
				
		
		SearchResponse resp = reqBuilder.execute().actionGet();
		Aggregations aggs = resp.getAggregations();
		InternalHistogram agg = aggs.get("group_by_day");
		List<Bucket> buckets =  (List<Bucket>) agg.getBuckets();
		
		for(Bucket bucket:buckets){
			Long docCount = bucket.getDocCount();
			String date = bucket.getKeyAsString();
			
			System.out.println(date+":"+docCount);
		}
	}