需求是在按照某一个时间字段统计在一个时间区间中,以天为单位统计时间单位内的记录条数:
使用的Solr file type是:org.apache.solr.schema.TrieDateField
查询url是:
http://10.1.7.41:8983/solr/search4totalpay_shard1_replica1/select?
fl=op_time&q=entity_id%3A00000241
&rows=0&wt=json&indent=true&facet=true&facet.range=op_time
&f.op_time.facet.range.start=2014-01-01T00:00:00Z
&f.op_time.facet.range.end=NOW
&f.op_time.facet.range.gap=%2B1DAY
&facet.mincount=1&facet.query=entity_id:00000241
参数说明:
- facet.range:facet区间统计使用的字段
- f.op_time.facet.range.gap: 时间区间的跨度,可以使用“+1DAY”或者“+1MONTH”这样的关键字来标示区间的大小,这样有一个好处是在服务端自动会用每个时间段的开始和结束边界来统计,例如:“+1DAY”会从每天的0点到次日的24点结束的区间
得到的结果如下:
{
"responseHeader":{
"status":0,
"QTime":911,
"params":{
"f.op_time.facet.range.end":"NOW",
"facet":"true",
"fl":"op_time",
"facet.mincount":"1",
"indent":"true",
"facet.query":"entity_id:00000241",
"f.op_time.facet.range.start":"2014-01-01T00:00:00Z",
"q":"entity_id:00000241",
"facet.range":"op_time",
"f.op_time.facet.range.gap":"+1DAY",
"wt":"json",
"rows":"0"}},
"response":{"numFound":7460,"start":0,"maxScore":6.956293,"docs":[]
},
"facet_counts":{
"facet_queries":{
"entity_id:00000241":7460},
"facet_fields":{},
"facet_dates":{},
"facet_ranges":{
"op_time":{
"counts":[
"2014-04-30T00:00:00Z",59,
"2014-05-01T00:00:00Z",117,
"2014-05-02T00:00:00Z",91,
"2014-05-03T00:00:00Z",75,
"2014-05-04T00:00:00Z",55,
"2014-05-05T00:00:00Z",52,
"2014-05-06T00:00:00Z",75,
"2014-05-07T00:00:00Z",60,
"2014-05-08T00:00:00Z",57,
"2014-05-09T00:00:00Z",75
],
"gap":"+1DAY",
"start":"2014-01-01T00:00:00Z",
"end":"2016-01-20T00:00:00Z"}},
"facet_intervals":{},
"facet_heatmaps":{}}}