# Date histogram error

**URL:** https://discuss.elastic.co/t/date-histogram-error/295155
**Category:** Elasticsearch
**Created:** [January 23, 2022, 6:31pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155 "2022-01-23T18:31:33Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 23, 2022, 6:31pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155/1 "2022-01-23T18:31:33Z")

</div>

Hello, dear community.  
Have an error with date histogram.  
I have a cryptocurrency price base with a 5 minute interval (4 million records). Fields:

```auto
            {
                "_source": {
                    "time": 1357828800000,
                    "priceUsd": 13.89
                }
            },
            {
                "_source": {
                    "time": 1357831800000,
                    "priceUsd": 14.05
                }
            }

```

I want to make the output interval 1 day by this request:

```auto
POST /coin_charts/_search
{
    "aggs": {
        "bitcoin": {
            "date_histogram": {
                "field": "time",
                "fixed_interval": "1d"
            }
        }
    }
}

```

And i get this answer:

```auto
"error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "[date_histogram] unknown field [fixed_interval], parser not found"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "[date_histogram] unknown field [fixed_interval], parser not found"
    },

```

What am I doing wrong? Everything was done according to the official tutorial. Help please 😟

---

<div class="post-metadata">

### Author: ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)
#### Post date: [January 23, 2022, 6:47pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155/2 "2022-01-23T18:47:28Z")

</div>

Which version of Elasticsearch are you using?

---

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 23, 2022, 6:58pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155/3 "2022-01-23T18:58:47Z")

</div>

5.6.9

---

<div class="post-metadata">

### Author: ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)
#### Post date: [January 23, 2022, 7:01pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155/4 "2022-01-23T19:01:55Z")

</div>

That's it, `fixed_interval` was implemented only in 7.2, you are using a very old version.

You should use `interval`, according to the [documentation for your version](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-bucket-datehistogram-aggregation.html).

---

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 23, 2022, 11:13pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155/5 "2022-01-23T23:13:09Z")

</div>

Thank you for answer a lot. Update really helps me.  
But maybe you can help with date\_histogram? it doesn't work properly ☹  
I use this request:

```auto
    "aggs": {
        "bitcoin": {
            "date_histogram": {
                "field": "date",
                "calendar_interval": "1w"
            }
        }
    }

```

But result without interval, the same as in the database (excluding the specified interval).

```auto
...
            {
                "_source": {
                    "time": 1357035900000,
                    "priceUsd": 13.44,
                    "date": "2013-01-01T10:25:00.000Z"
                }
            },
            {
                "_source": {
                    "time": 1357036200000,
                    "priceUsd": 13.44,
                    "date": "2013-01-01T10:30:00.000Z"
                }
            },
...

```

Records in my database every 5 minutes. And even after filtering by an interval, it gives me the same records from the database. Maybe I'm doing something wrong?  
I will be very grateful for the answer.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [January 24, 2022, 4:52am UTC](https://discuss.elastic.co/t/date-histogram-error/295155/6 "2022-01-24T04:52:58Z")

</div>

Can you please explain what you are trying to achieve at a high level? Aggregation allow you to perform calculations over your data, e.g. provide a date histogram with the maximum and minimum price per day or week. Is that what you are looking to do?

---

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 24, 2022, 8:35pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155/7 "2022-01-24T20:35:07Z")

</div>

No, I thought otherwise ☹

I have a database with 100k records and I want only every 50th record to be displayed.

That is, a database with a 5-minute timeframe, bitcoin price rates. I need to display points for the chart for the last year/month/week. At the same time, so that there are no more than 1000 results. Since the graphic chart cannot render more points.

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [January 24, 2022, 11:53pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155/8 "2022-01-24T23:53:00Z")

</div>

Is there some difference from the previous topic?

> [@Getting every 2nd element from \_source](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123/4):
>
> So the true problem is the necessary size for query could be hundreds of thousands and too wasteful. Then you can use [Date histogram aggregation | Elasticsearch Guide [7.16] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html) with [Top hits aggregation | Elasticsearch Guide [7.16] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html). Top hit with size=1 and sort by date, you can get last (or first) documents in the date-histo buckets.

If you need some last metrics other than last document, you can use top metrics aggregation sorted by time stamp.

> **[Top metrics aggregation | Elasticsearch Guide \[master\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-metrics-top-metrics.html)**

---

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 25, 2022, 9:40pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155/9 "2022-01-25T21:40:31Z")

</div>

Unfortunately, there are problems with that. I thought that I need to use other methods.  
It turns out that what you recommended in that thread is what you need. But it doesn't work right for me.  
For example, i have 5 records:

```auto
PUT /coin_charts/_search
            {
                "_source": {
                    "priceUsd": 45000,
                    "time": 1642716000000, //Just in case, I will clarify that I am aware of the same milliseconds. I just added a date field in a new clean database in order to experiment with it.
                    "date": "2022-01-01T16:23:09.55"
                }
            },
            {
                "_source": {
                    "priceUsd": 43000,
                    "time": 1642716000000,
                    "date": "2022-01-02T16:23:09.55"
                }
            },
            {
                "_source": {
                    "priceUsd": 40000,
                    "time": 1642716000000,
                    "date": "2022-01-03T16:23:09.55"
                }
            },
            {
                "_source": {
                    "priceUsd": 38000,
                    "time": 1642716000000,
                    "date": "2022-01-04T16:23:09.55"
                }
            },
            {
                "_source": {
                    "priceUsd": 36000,
                    "time": 1642716000000,
                    "date": "2022-01-05T16:23:09.55"
                }
            }

```

And for interval i'm use:

```auto
POST /coin_charts/_doc/_search
{
  "aggs": {
    "bitcoin": {
      "date_histogram": {
        "field": "date",//I tried to use first a field with milliseconds(time), it didn't work, then I decided to add a field with a date
        "fixed_interval": "2d" 
      }
    }
  }
}

```

I thought this should only print two results from my database (1 and 3), but it prints all 5 results. Maybe I'm wrote the request incorrectly?

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [January 25, 2022, 11:41pm UTC](https://discuss.elastic.co/t/date-histogram-error/295155/10 "2022-01-25T23:41:41Z")

</div>

Try using tophits or top metrics aggregation with date histogram aggregation, as I said.

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [January 26, 2022, 12:26am UTC](https://discuss.elastic.co/t/date-histogram-error/295155/11 "2022-01-26T00:26:40Z")

</div>

There are two types of aggregation: bucket  
aggregations and metrics aggregation. Please see the documents to get detail. Bucket aggregation such as date histogram bucket aggregation only divide all documents into some buckets and did not select them. You can use metrics aggregations such as top hits or top metrics aggregation to retrieve some value or documents from each packets.

---

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 26, 2022, 1:11am UTC](https://discuss.elastic.co/t/date-histogram-error/295155/12 "2022-01-26T01:11:54Z")

</div>

I finally figured it out, thanks for the help 🙃

---

<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 23, 2022, 1:12am UTC](https://discuss.elastic.co/t/date-histogram-error/295155/13 "2022-02-23T01:12:46Z")

</div>

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