# Search by datetime range query

**URL:** <https://discuss.elastic.co/t/search-by-datetime-range-query/282463>\
**Category:** Elasticsearch\
**Created:** [August 25, 2021, 9:07am UTC](https://discuss.elastic.co/t/search-by-datetime-range-query/282463 "2021-08-25T09:07:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rehannali](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rehannali/32/57714_2.png) [@rehannali](https://discuss.elastic.co/u/rehannali)\
**Post date:** [August 25, 2021, 9:07am UTC](https://discuss.elastic.co/t/search-by-datetime-range-query/282463/1 "2021-08-25T09:07:34Z")

</div>

Hi, I have issue related to search through `datetime` range query. I tried to search some data which is oldest after a certain period. I read documentation and try to make query and I'm unable to get any of the data.

```auto
GET /my-index/_search
{
  "query": {
    "range": {
      "datetime": {
        "lt": "now-3d/d"
      }
    }
  }
}

```

It is working fine if I use only `now` in query.

```auto
GET /my-index/_search
{
  "query": {
    "range": {
      "datetime": {
        "lt": "now"
      }
    }
  }
}

```

The actual response if I use with `now` and I have enough data of last week so it should work.

```auto
{
        "_index" : "my-index",
        "_type" : "_doc",
        "_id" : "96234123102",
        "_score" : 1.0,
        "_source" : {
          "service" : "ONEZAPP",
          "description" : "CUSTOMER PORTAL FUND TRANSFER",
          "service_id" : 547,
          "oneload_transaction_type_id" : 2,
          "status" : "SUCCESS",
          "datetime" : "2021-08-24T10:16:09.000Z"
        }
      }

```

How should I search or delete records using `datetime` search so I can do further operations on it?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 25, 2021, 9:30am UTC](https://discuss.elastic.co/t/search-by-datetime-range-query/282463/2 "2021-08-25T09:30:10Z")

</div>

You can use the explain API to figure out what the date was rounded to... as usual a full reproduction would help here a lot... try this snippet as an example

```auto
# not in result set
PUT test/_doc/1
{
  "@timestamp" : "2021-08-22T12:34:56.789Z"
}

GET test/_mapping

GET test/_search
{
  "query": {
    "range": {
      "@timestamp": {
        "lt": "now-3d/d"
      }
    }
  }
}

GET test/_explain/1
{
  "query": {
    "range": {
      "@timestamp": {
        "lt": "now-3d/d"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![rehannali](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rehannali/32/57714_2.png) [@rehannali](https://discuss.elastic.co/u/rehannali)\
**Post date:** [August 25, 2021, 9:51am UTC](https://discuss.elastic.co/t/search-by-datetime-range-query/282463/3 "2021-08-25T09:51:21Z")

</div>

Hi @spinscale , Thanks for your swift response. As I have mentioned all payload which is available on elasticsearch index. I have `datetime` field which has a date type. If I only use `now` it worked but not with `now-3d/d`.

I have mentioned below my mapping. I have record of the last 7 days and still, it returns nothing.

```auto

{
  "my-index" : {
    "mappings" : {
      "properties" : {
        "datetime" : {
          "type" : "date"
        },
      }
    }
  }
}

```

---

<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:** [September 22, 2021, 9:51am UTC](https://discuss.elastic.co/t/search-by-datetime-range-query/282463/4 "2021-09-22T09:51:59Z")

</div>

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