# Elasticsearch date range queries with ignore year

**URL:** https://discuss.elastic.co/t/elasticsearch-date-range-queries-with-ignore-year/46693
**Category:** Elasticsearch
**Created:** [April 7, 2016, 1:57pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-queries-with-ignore-year/46693 "2016-04-07T13:57:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![George](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@George](https://discuss.elastic.co/u/George)
#### Post date: [April 7, 2016, 1:57pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-queries-with-ignore-year/46693/1 "2016-04-07T13:57:20Z")

</div>

Hello everybody,

I have a use case where I need to search in es for a date field.  
However, this is not only a simple range but I wanna ignore the year.

Hence I saw this in the es docu:

{  
"range" : {  
"born" : {  
"gte": "01/01/2012",  
"lte": "2013",  
"format": "dd/MM/yyyy||yyyy"  
}  
}  
}

and I tried this:  
{  
"range" : {  
"born" : {  
"gte": "01/01",  
"lt": "02/01",  
"format": "dd/MM"  
}  
}  
}

I had a document with a range date field which was set to 01.01.2014 but the above query didn't match my document.

Can anybody point me in some direction? Basically I wanna search for date fields but ignoring the year.

PS. I'm using es 2.X

Many thanks!

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 7, 2016, 2:19pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-queries-with-ignore-year/46693/2 "2016-04-07T14:19:26Z")

</div>

IMO, if you are searching for a month, index a month. If you are searching for a day, index a day.

A date in elasticsearch is indexed as `ms` since epoch. So you can not really do that kind of range query.

That being said, you could compute an aggregation using [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script) and then add a sub aggregation [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-metrics-top-hits-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-metrics-top-hits-aggregation.html). It would may be give you something close to what you are looking for but not exactly what you asked for.

My 2 cents

---

<div class="post-metadata">

### Author: ![George](https://avatars.discourse-cdn.com/v4/letter/g/71e660/32.png) [@George](https://discuss.elastic.co/u/George)
#### Post date: [April 7, 2016, 2:35pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-queries-with-ignore-year/46693/3 "2016-04-07T14:35:21Z")

</div>

Hi David,

Thanks for the info.  
I was thinking about the indexing part but I thought it would be easier if es query api already has this functionality to ignore the year.  
However the aggregation part looks interesting too, so thanks for point that out.

Regards!

---

<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, 11:01pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-queries-with-ignore-year/46693/4 "2017-07-05T23:01:30Z")

</div>


