# Undocumented behaviour: Term query on date field becomes a range query

**URL:** <https://discuss.elastic.co/t/undocumented-behaviour-term-query-on-date-field-becomes-a-range-query/300113>\
**Category:** Elasticsearch\
**Created:** [March 19, 2022, 9:36pm UTC](https://discuss.elastic.co/t/undocumented-behaviour-term-query-on-date-field-becomes-a-range-query/300113 "2022-03-19T21:36:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![whatgeorgemade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whatgeorgemade/32/103246_2.png) [@whatgeorgemade](https://discuss.elastic.co/u/whatgeorgemade)\
**Post date:** [March 19, 2022, 9:36pm UTC](https://discuss.elastic.co/t/undocumented-behaviour-term-query-on-date-field-becomes-a-range-query/300113/1 "2022-03-19T21:36:40Z")

</div>

I was speaking with a colleague recently and I mentioned I use a `term` query over a `date` field to find matches on a certain day, like this:

```auto
{
  "query": {
    "term": {
      "@timestamp": {
        "value": "2021-10-08"
      }
    }
  }
}

```

That query returns all documents with an `@timestamp` on 8th October 2021.

They were surprised by this, so we looked at the docs but couldn't find any reference to this behaviour. I had a suspicion that Elasticsearch was rewriting this query as a `range`, which was confirmed after finding [this line of code](https://github.com/elastic/elasticsearch/blob/b592d2bf01e06d438f6c337b8a4d71a9dac8ebfd/server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java#L490) in the Elasticsearch source. The query effectively becomes this:

```auto
{
  "query": {
    "range": {
      "@timestamp": {
        "gte": "2021-10-08",
        "lte": "2021-10-08"
      }
    }
  }
}

```

The `gte` rounds down, the `lte` rounds up, so the range is for the full day.

Missing day components are replaced with `01`, so omitting that finds all documents on the first of the month, like this query:

```auto
{
  "query": {
    "term": {
      "@timestamp": {
        "value": "2021-10"
      }
    }
  }
}

```

Is this feature of the `term` query well-known? I can't remember how I stumbled on this behaviour but have been using it for a while. There's no reference to it in the docs for the term query or date field type.

George.

---

<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:** [April 16, 2022, 9:37pm UTC](https://discuss.elastic.co/t/undocumented-behaviour-term-query-on-date-field-becomes-a-range-query/300113/2 "2022-04-16T21:37:07Z")

</div>

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