最简单的方法是定义一个专用的星期几字段,该字段仅包含每个文档的星期几,然后对该字段进行术语聚合。
如果出于某种原因你不想(或不能)这样做,这里有一个技巧可以帮助你得到你想要的。基本思想是定义一个"date.raw"
字符串子字段,使用标准分析器进行分析,以便为一周中的每一天创建术语。然后,您可以汇总这些术语以获得计数,使用include仅包含您想要的术语。
这是我用于测试的映射:
PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"properties": {
"msg": {
"type": "string"
},
"date": {
"type": "date",
"format": "E, dd MMM yyyy",
"fields": {
"raw": {
"type": "string"
}
}
}
}
}
}
}
和一些示例文档:
POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"msg": "hello","date": "Wed, 11 Mar 2015"}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"msg": "hello","date": "Tue, 10 Mar 2015"}
{"index":{"_index":"test_index","_type":"doc","_id":3}}
{"msg": "hello","date": "Mon, 09 Mar 2015"}
{"index":{"_index":"test_index","_type":"doc","_id":4}}
{"msg": "hello","date": "Wed, 04 Mar 2015"}
以及聚合和结果:
POST /test_index/_search?search_type=count
{
"aggs":{
"docs_by_day":{
"terms":{
"field": "date.raw",
"include": "mon|tue|wed|thu|fri|sat|sun"
}
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"docs_by_day": {
"buckets": [
{
"key": "wed",
"doc_count": 2
},
{
"key": "mon",
"doc_count": 1
},
{
"key": "tue",
"doc_count": 1
}
]
}
}
}
这是所有代码:
http://sense.qbox.io/gist/0292ddf8a97b2d96bd234b787c7863a4bffb14c5