# DSL Query and aggregation on timestamp field

**URL:** <https://discuss.elastic.co/t/dsl-query-and-aggregation-on-timestamp-field/158894>\
**Category:** Elasticsearch\
**Created:** [November 30, 2018, 11:05am UTC](https://discuss.elastic.co/t/dsl-query-and-aggregation-on-timestamp-field/158894 "2018-11-30T11:05:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khaled\_Saidi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khaled_saidi/32/98636_2.png) [@Khaled\_Saidi](https://discuss.elastic.co/u/Khaled_Saidi)\
**Post date:** [November 30, 2018, 11:05am UTC](https://discuss.elastic.co/t/dsl-query-and-aggregation-on-timestamp-field/158894/1 "2018-11-30T11:05:17Z")

</div>

Hi everybody,

I try to write a query that return the number of documents gathered by day.  
My timestamp field is like :

`2018-11-22T08:52:53.933Z`

I try this query :

```
{
    "size": 0,
    "sort": { 
        "@timestamp": "desc" 
    },
    "aggs": {
        "group_by_index": {
            "terms": { 
                "field": "@timestamp",
                "format": "yyy-MM-d",
                "size": 10000 
            }
        }
    }
}

```

And the result is :

```
.....
"aggregations": {
    "group_by_index": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 1542876773933,
          "key_as_string": "2018-11-22",
          "doc_count": 34
        },
        {
          "key": 1542622429233,
          "key_as_string": "2018-11-19",
          "doc_count": 28
        },
        {
          "key": 1542970886679,
          "key_as_string": "2018-11-23",
          "doc_count": 16
        },
        {
          "key": 1542622429214,
          "key_as_string": "2018-11-19",
          "doc_count": 14
        },
        {
          "key": 1542643132128,
          "key_as_string": "2018-11-19",
          "doc_count": 14
        },
.....

```

My expected result is :

```
....
  {
              "key": 1542970886679,
              "key_as_string": "2018-11-23",
              "doc_count": 23
            },
  {
              "key": 1542970886679,
              "key_as_string": "2018-11-22",
              "doc_count": 42
            },
  {
              "key": 1542970886679,
              "key_as_string": "2018-11-21",
              "doc_count": 36
            },
....

```

So i have 2 questions :

- how to do to have the expected result ?
- Why the sort desc clause seems to not work ?

Thanks a lot for your help.

BR

---

<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:** [November 30, 2018, 11:47am UTC](https://discuss.elastic.co/t/dsl-query-and-aggregation-on-timestamp-field/158894/2 "2018-11-30T11:47:56Z")

</div>

Use a date histogram instead

---

<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:** [December 28, 2018, 11:48am UTC](https://discuss.elastic.co/t/dsl-query-and-aggregation-on-timestamp-field/158894/3 "2018-12-28T11:48:06Z")

</div>

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