【问题标题】:Date aggregation - elasticsearch日期聚合 - elasticsearch
【发布时间】:2017-12-28 16:21:06
【问题描述】:

我最近开始研究弹性搜索,我在日期聚合方面遇到了问题。

映射:

{
  "posts": {
    "mappings": {
        "facebook": {
            "properties": {
                "post_created_time": {
                    "type": "date"
                },
                "post_description": {
                    "type": "text"
                },
                "post_image": {
                    "type": "text"
                }
            }
        }
    }
  }
}

来源:

"hits": [
        {
            "_index": "posts",
            "_type": "facebook",
            "_id": "1027753751227740806",
            "_score": 1,
            "_source": {
                "post_created_time": 1436737816,
                "post_description": "captain's log: sailed a sea of booze after sundown with a new crew. bonds cemented. four more days of r&r before the next voyage. july the 5th, 2015 #captainslog #litely",
                "post_image": "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/11419297_845398318885282_1471458983_n.jpg?ig_cache_key=MTAyNzc1Mzc1MTIyNzc0MDgwNg%3D%3D.2"
            }
        },
        {
            "_index": "posts",
            "_type": "facebook",
            "_id": "1030858912926085173",
            "_score": 1,
            "_source": {
                "post_created_time": 1436218427,
                "post_description": "wanna go for a ride? #?",
                "post_image": "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/1739201_510171422480872_883567488_n.jpg?ig_cache_key=MTAzMDg1ODkxMjkyNjA4NTE3Mw%3D%3D.2"
            }
        }
]

我尝试过的查询:

{  
 "size":0,
 "aggs":{  
     "metrics_by_day":{  
     "date_histogram":{  
        "field":"post_created_time",
        "interval":"day"
     }
   }
 }
}

上述查询的输出:

"aggregations": {
    "metrics_by_day": {
        "buckets": [
            {
                "key_as_string": "1970-01-17",
                "key": 1382400000,
                "doc_count": 2
            }
        ]
    }
}

预期输出为:

"aggregations": {
    "metrics_by_day": {
        "buckets": [
            {
                "key_as_string": "2015-07-12",
                "doc_count": 1
            },
            {
                "key_as_string": "2015-07-06",
                "doc_count": 1
            }
        ]
    }
}

任何建议将不胜感激

【问题讨论】:

    标签: elasticsearch aggregation date-range elasticsearch-5 elasticsearch-aggregation


    【解决方案1】:

    您的映射和聚合中的日期格式存在问题。您的日期类型的格式是 "format" : "epoch_second" (https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html),您必须指定 DateHistogramm 聚合 "format" : "yyyy-MM-dd" (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html) 的日期格式

    以下是更正后的映射:

    {
    "posts": {
        "mappings": {
            "facebook": {
                "properties": {
                    "post_created_time": {
                        "type": "date",
                         "format":"epoch_second"
                    },
                    "post_description": {
                        "type": "text"
                    },
                    "post_image": {
                        "type": "text"
                    }
                }
            }
        }
      }
    }
    

    还有你的聚合:

    {  
     "size":0,
     "aggs":{  
         "metrics_by_day":{  
         "date_histogram":{  
            "field":"post_created_time",
            "interval":"day",
            "format" : "yyyy-MM-dd"
         }
       }
     }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 2018-01-09
      • 1970-01-01
      相关资源
      最近更新 更多