# How could I append time stamp range within my elasticsearch query?

**URL:** https://discuss.elastic.co/t/how-could-i-append-time-stamp-range-within-my-elasticsearch-query/61914
**Category:** Elasticsearch
**Created:** [September 30, 2016, 10:19am UTC](https://discuss.elastic.co/t/how-could-i-append-time-stamp-range-within-my-elasticsearch-query/61914 "2016-09-30T10:19:20Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Kulasangar\_Gowrisang](https://avatars.discourse-cdn.com/v4/letter/k/ce73a5/32.png) [@Kulasangar\_Gowrisang](https://discuss.elastic.co/u/Kulasangar_Gowrisang)
#### Post date: [September 30, 2016, 10:19am UTC](https://discuss.elastic.co/t/how-could-i-append-time-stamp-range-within-my-elasticsearch-query/61914/1 "2016-09-30T10:19:21Z")

</div>

I'm trying perform an elasticsearch query as a `POST` request in order pull data from the index which I created. The data which is in the index is, a table from MySQL DB, configured though `logstash`.

Here is my request and the `JSON` body:

[http://localhost:9200/response\_summary/\_search](http://localhost:9200/response_summary/_search)

Body:

```
{
   "query": {
       "query_string": {
           "query": "transactionoperationstatus:\"charged\" AND api:\"payment\" AND operatorid:\"XL\" AND userid:*test AND time:\"2015-05-27*\" AND responsecode:(200+201)"
       }
   },
    "aggs": {
      "total": {
          "terms": {
              "field": "userid"
          },
   "aggs": {
      "total": {
          "sum": {
              "script": "Double.parseDouble(doc['chargeamount'].value)"
          }
        }
    }
  }
 }
}

```

In the above `JSON` body, I'm in need to append the `timestamp` into the `query_string` in order get the data from the index within a date range. I tried adding at the end of the query as:

`AND timestamp:[2015-05-27T00:00:00.128Z+TO+2015-05-27T23:59:59.128Z]"`

Where am I going wrong? Any help would be appreciated.

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [October 1, 2016, 1:52pm UTC](https://discuss.elastic.co/t/how-could-i-append-time-stamp-range-within-my-elasticsearch-query/61914/2 "2016-10-01T13:52:54Z")

</div>

Generally I avoid query string because it is fairly brittle - if the query\_string is coming from a user that you trust and you just want to add an extra condition then the right thing to do is to wrap the query\_string in a `bool` query and add both the `query_string` and a `range` query as `must` clauses to the bool query.

---

<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: [July 5, 2017, 10:15pm UTC](https://discuss.elastic.co/t/how-could-i-append-time-stamp-range-within-my-elasticsearch-query/61914/3 "2017-07-05T22:15:37Z")

</div>


