# How to filter by date range in quarter using elasticsearch

**URL:** https://discuss.elastic.co/t/how-to-filter-by-date-range-in-quarter-using-elasticsearch/21728
**Category:** Elasticsearch
**Created:** [January 20, 2015, 11:28am UTC](https://discuss.elastic.co/t/how-to-filter-by-date-range-in-quarter-using-elasticsearch/21728 "2015-01-20T11:28:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![wasaeshoaib](https://avatars.discourse-cdn.com/v4/letter/w/e56c9b/32.png) [@wasaeshoaib](https://discuss.elastic.co/u/wasaeshoaib)
#### Post date: [January 20, 2015, 11:28am UTC](https://discuss.elastic.co/t/how-to-filter-by-date-range-in-quarter-using-elasticsearch/21728/1 "2015-01-20T11:28:26Z")

</div>

I have an index /testdate/daterange with the following mapping

```
PUT /testdate/daterange/_mapping
{
  "daterange": {
    "properties": {
      "text":{
        "type": "string"
      },
      "date": {
        "type": "date",
        "format": "dateOptionalTime"
      }
    }
  }
}

```

And there are some documents e.g

```
PUT /testdate/daterange/1
{
  "text": "This Quater",
  "date": "2015-01-20T07:22:15+00:00"
}
PUT /testdate/daterange/2
{
  "text": "Last Quater",
  "date": "2015-11-20T07:22:15+00:00"
}

```

I want to search using a filter on the date field for documents from this  
the last quarter. Something like the following

```
GET /testdate/daterange/_search
{
  "filter":{
    "range": {
      "date": {
        "gt": "now-1Q/Q",
        "lt": "now/Q"
      }
    }
  }
}

```

Elasticsearch only supports Joda date formats  
([http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html))  
which does not have any support for quarter.

Also if I try something like

```
GET /testdate/daterange/_search
{
  "filter":{
    "range": {
      "date": {
        "gt": "now-3M/3M",
        "lt": "now/3M"
      }
    }
  }
}

```

I get an error

```
ElasticsearchParseException[rounding `/` can only be used on single 

```

unit types

So how can I achieve this?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/2208cf81-1553-4115-866d-65f782433759%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2208cf81-1553-4115-866d-65f782433759%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![singrahu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/singrahu/32/12985_2.png) [@singrahu](https://discuss.elastic.co/u/singrahu)
#### Post date: [February 20, 2017, 4:24pm UTC](https://discuss.elastic.co/t/how-to-filter-by-date-range-in-quarter-using-elasticsearch/21728/2 "2017-02-20T16:24:24Z")

</div>

Support for `quarter` (or `q`) is not present yet in ElasticSearch. An open issue is available for this: [https://github.com/elastic/elasticsearch/issues/15612](https://github.com/elastic/elasticsearch/issues/15612).  
Also we cannot specify multiple of a date unit to round down to nearest one, so `now-3M/M` will work, but not `now-3M/3M`.

---

<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:03pm UTC](https://discuss.elastic.co/t/how-to-filter-by-date-range-in-quarter-using-elasticsearch/21728/3 "2017-07-05T22:03:04Z")

</div>


