不考虑年份的ElasticSearch日期直方图聚合

o3imoua4  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(346)

我想聚集数据的月份和日期,我尝试与日期直方图聚合,但它考虑到完整的日期对象。
我有如下数据:

{
          "created" : "2020-09-26",
          "id" : 6,
          "name" : "Halum khamu"      
      },
      {
          "created" : "2021-09-26",
          "id" : 7,
          "name" : "new new"
      },
      {
          "created" : "2020-10-07",
          "id" : 8,
          "name" : "Halum new"
      }

我使用这个查询:

GET /regulations/_search/
{
  "aggs": {
    "news_over_time": {
      "date_histogram": {
        "field": "created",
        "calendar_interval": "day",
        "keyed": true,
        "format": "yyy-MM-dd",
        "min_doc_count": 1
      }
    }
  }
}

它为我返回数据:

"buckets" : {
        "2020-09-26" : {
          "key_as_string" : "2020-09-26",
          "key" : 1600300800000,
          "doc_count" : 1
        },
        "2021-09-26" : {
          "key_as_string" : "2021-09-26",
          "key" : 1601337600000,
          "doc_count" : 1
        },
        "2020-10-07" : {
          "key_as_string" : "2020-10-07",
          "key" : 1632873600000,
          "doc_count" : 1
        }
      }

但我的预期产出将不考虑今年:

"buckets" : {
        "09-26" : {
          "key_as_string" : "09-26",
          "key" : 1600300800000,
          "doc_count" : 2
        },
        "10-07" : {
          "key_as_string" : "10-07",
          "key" : 1632873600000,
          "doc_count" : 1
        }
      }

我该怎么做?

aemubtdh

aemubtdh1#

没有直接的方法来解决这个问题,但这应该可以完成工作:

GET regulations/_search
{
  "size": 0,
  "aggs": {
    "day_histogram": {
      "scripted_metric": {
        "init_script": "state.day_map = [:];",
        "map_script": """
          def created = doc.created.value;

          def month = created.getMonthOfYear();
          def day = created.getDayOfMonth();

          def key = String.format('%02d', new def[] { month })  
                    + '-' + 
                    String.format('%02d', new def[] { day });

          if (state.day_map.containsKey(key)) {
            state.day_map[key] += 1;
          } else {
            state.day_map[key] = 1;
          }
        """,
        "combine_script": "return state.day_map",
        "reduce_script": "return states"
      }
    }
  }
}

顺从的

{
  ...
  "aggregations":{
    "day_histogram":{
      "value":[
        {
          "09-26":2,
          "10-07":1
        }
      ]
    }
  }
}

编辑——实际上有一个更简单的方法:

GET regulations/_search
{
  "size": 0,
  "aggs": {
    "day_histogram": {
      "terms": {
        "script": {
          "source": "doc.created.value.monthOfYear + '-' + doc.created.value.dayOfMonth"
        },
        "size": 10
      }
    }
  }
}

相关问题