# Composite date aggregation not consistent

**URL:** https://discuss.elastic.co/t/composite-date-aggregation-not-consistent/172666
**Category:** Elasticsearch
**Created:** [March 17, 2019, 10:03am UTC](https://discuss.elastic.co/t/composite-date-aggregation-not-consistent/172666 "2019-03-17T10:03:16Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![morlokdk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/morlokdk/32/42184_2.png) [@morlokdk](https://discuss.elastic.co/u/morlokdk)
#### Post date: [March 17, 2019, 10:03am UTC](https://discuss.elastic.co/t/composite-date-aggregation-not-consistent/172666/1 "2019-03-17T10:03:16Z")

</div>

Hi, i using elastic 6.3.2 and have issue with date\_aggregation and timezones

This is me aggregation

```
"aggs": {
  "histograms2": {
    "date_histogram": {
      "field": "createdAt",
      "interval": "hour",
      "format": "YYYY_MM_dd:HH",
      "time_zone": "+03:00"
    }
  },
  "histograms": {
    "composite": {
      "size": 1000,
      "sources": [
        {
          "agg_0": {
            "date_histogram": {
              "field": "createdAt",
              "interval": "hour",
              "format": "YYYY_MM_dd:HH",
              "time_zone": "+03:00"
            }
          }
        }
      ]
    }
  }
}

```

and result is

```
"aggregations": {
  "histograms": {
    "after_key": {
      "agg_0": "2019_03_13:11"
    },
    "buckets": [
      {
        "key": {
          "agg_0": "2019_03_13:10"
        },
        "doc_count": 7
      },
      {
        "key": {
          "agg_0": "2019_03_13:11"
        },
        "doc_count": 1
      }
    ]
  },
  "histograms2": {
    "buckets": [
      {
        "key_as_string": "2019_03_13:13",
        "key": 1552471200000,
        "doc_count": 7
      },
      {
        "key_as_string": "2019_03_13:14",
        "key": 1552474800000,
        "doc_count": 1
      }
    ]
  }
}

```

As you can see "string" keys are different. Me objects have dates "2019-03-13T10:23:30.992Z" until "2019-03-13T11:23:30.992Z".

Why keys are not same?

UPDATE: if i change aggs to daily aggregation result is also different

```
"aggregations": {
  "histograms": {
    "after_key": {
      "agg_0": "2019_03_12"
    },
    "buckets": [
      {
        "key": {
          "agg_0": "2019_03_12"
        },
        "doc_count": 8
      }
    ]
  },
  "histograms2": {
    "buckets": [
      {
        "key_as_string": "2019_03_13",
        "key": 1552424400000,
        "doc_count": 8
      }
    ]
  }
}

```

Date key 2019\_03\_13 seems to be correct, but not 2019\_03\_12

---

<div class="post-metadata">

### Author: ![clement\_tourriere](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clement_tourriere/32/479_2.png) [@clement\_tourriere](https://discuss.elastic.co/u/clement_tourriere)
#### Post date: [April 9, 2019, 3:35pm UTC](https://discuss.elastic.co/t/composite-date-aggregation-not-consistent/172666/2 "2019-04-09T15:35:54Z")

</div>

Hello,

it seems indeed to be a bug with the `format` parameter in date\_histogram from composite aggregation that performs a wrong rounding of the date.

The full reproduction:

```
# Create index
PUT test_date_histo
{
  "settings": {
    "number_of_shards": 5
  },
  "mappings": {
    "_doc": {
      "properties": {
        "date": {
          "type": "date"
        }
      }
    }
  }
}

# Index some data
POST test_date_histo/_doc
{
  "date": "2019-03-01T00:01:00+00:00"
}

# Perform a comparative query
GET test_date_histo/_search
{
  "size": 0,
  "aggs": {
    "date_histo": {
      "date_histogram": {
        "field": "date",
        "interval": "day",
        "time_zone": "+02:00",
        "format": "yyyy-MM-dd"
      }
    },
    "composite_date_histo": {
      "composite": {
        "sources": [
          {
            "date_histo": {
              "date_histogram": {
                "field": "date",
                "interval": "day",
                "time_zone": "+02:00",
                "format": "yyyy-MM-dd"
              }
            }
          }
        ]
      }
    }
  }
}

# Without format parameter in composite, epoch date is the same between date_histogram and composite date_histogram
GET test_date_histo/_search
{
  "size": 0,
  "aggs": {
    "date_histo": {
      "date_histogram": {
        "field": "date",
        "interval": "day",
        "time_zone": "+02:00",
        "format": "yyyy-MM-dd"
      }
    },
    "composite_date_histo": {
      "composite": {
        "sources": [
          {
            "date_histo": {
              "date_histogram": {
                "field": "date",
                "interval": "day",
                "time_zone": "+02:00"
              }
            }
          }
        ]
      }
    }
  }
}
```

---

<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: [May 7, 2019, 3:35pm UTC](https://discuss.elastic.co/t/composite-date-aggregation-not-consistent/172666/3 "2019-05-07T15:35:58Z")

</div>

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