# Elasticsearch - Merge date histogram aggregation

**URL:** <https://discuss.elastic.co/t/elasticsearch-merge-date-histogram-aggregation/294228>\
**Category:** Elasticsearch\
**Created:** [January 13, 2022, 5:11am UTC](https://discuss.elastic.co/t/elasticsearch-merge-date-histogram-aggregation/294228 "2022-01-13T05:11:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyun](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hyun/32/97630_2.png) [@hyun](https://discuss.elastic.co/u/hyun)\
**Post date:** [January 13, 2022, 5:11am UTC](https://discuss.elastic.co/t/elasticsearch-merge-date-histogram-aggregation/294228/1 "2022-01-13T05:11:44Z")

</div>

**Query**

```auto
GET /_search
{
  "size" : 0,
  "query" : {
    "ids" : {
      "types" : [],
      "values" : ["someId1", "someId2", "someId3" ...]
    }
  },
  "aggregations" : {
    "how_to_merge" : {
      "date_histogram" : {
        "script" : {
          "inline" : "doc['date'].values"
        },
        "interval" : "1y",
        "format" : "yyyy"
      }
    }
  }
}

```

**Result**

```auto
{
   ...
   "aggregations": {
      "how_to_merge": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key_as_string": "2006",
               "key": 1136073600000,
               "doc_count": 1
            },
            {
               "key_as_string": "2007",
               "key": 1167609600000,
               "doc_count": 2
            },
            {
               "key_as_string": "2008",
               "key": 1199145600000,
               "doc_count": 3
            }
         ]
      }
   }
}

```

I want to merge "2006" and "2007"

And change key name to "TMP"

So result must like this:

```auto
{
   ...
   "aggregations": {
      "how_to_merge": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key_as_string": "TMP",
               "key": 1136073600000,
               "doc_count": 3 <----------- 2006(1) + 2007(2)
            },
            {
               "key_as_string": "2008",
               "key": 1199145600000,
               "doc_count": 3
            }
         ]
      }
   }
}

```

In the case of term queries, if I write a script query as shown below, it works normally, but the date histogram query does not change the key value.

The query below is a script query in which the key value in term aggregation changes SOMETHING1 and SOMETHING2 to TMP and merges.

```auto
def param = new groovy.json.JsonSlurper().parseText(
    '{\"SOMETHING1\": \"TMP\", \"SOMETHING2\": \"TMP\"}'
); 
def data = doc['my_field'].values; 
def list = [];
if (!doc['my_field'].empty) {
    for (x in data) { 
        if (param[x] != null) { 
            list.add(param[x]); 
        } else {
            list.add(x);
        }
    } 
}; 
return list;

```

How can I write a script query when I want to change the key value to the value I want and combine doc\_count in the data histogram query?

Thanks for your help and comments!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 13, 2022, 6:23am UTC](https://discuss.elastic.co/t/elasticsearch-merge-date-histogram-aggregation/294228/2 "2022-01-13T06:23:41Z")

</div>

That's typically something to do on the client side. Not in Elasticsearch itself.

---

<div class="post-metadata">

**Author:** ![hyun](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hyun/32/97630_2.png) [@hyun](https://discuss.elastic.co/u/hyun)\
**Post date:** [January 13, 2022, 9:10am UTC](https://discuss.elastic.co/t/elasticsearch-merge-date-histogram-aggregation/294228/3 "2022-01-13T09:10:27Z")

</div>

@dadoonet Term query works properly. Is there a way to query the date?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 13, 2022, 10:30am UTC](https://discuss.elastic.co/t/elasticsearch-merge-date-histogram-aggregation/294228/4 "2022-01-13T10:30:50Z")

</div>

That's another question right?

But yes you can use a `range` query.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 10, 2022, 10:31am UTC](https://discuss.elastic.co/t/elasticsearch-merge-date-histogram-aggregation/294228/5 "2022-02-10T10:31:43Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
