# How do date searches work

**URL:** <https://discuss.elastic.co/t/how-do-date-searches-work/14387>\
**Category:** Elasticsearch\
**Created:** [November 13, 2013, 5:00pm UTC](https://discuss.elastic.co/t/how-do-date-searches-work/14387 "2013-11-13T17:00:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias\_Gotzke](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Matthias\_Gotzke](https://discuss.elastic.co/u/Matthias_Gotzke)\
**Post date:** [November 13, 2013, 5:00pm UTC](https://discuss.elastic.co/t/how-do-date-searches-work/14387/1 "2013-11-13T17:00:25Z")

</div>

I am trying to store/search dates in ES but am experiencing some  
difficulties. It seems a few things are going on there which i do not fully  
understand yet.

When searching for a specific field with a _query\_string_ query then i  
everything behaves as expected. E.g. searching for "_created:[20120101 to  
20150101]_" finds the document.

When searching without specifying the field "_[20120101 to 20150101]_" it  
does only seem to perform a generic prefix match query and thus it returns  
the second document in the database only not the one indexed using the unix  
timestamp.

Is there anyway to let lucene or ES do all 3 types of range queries ? E.g.  
perform a string match,perform a date range and perform a number match  
using some default date formats ?

Any help is greatly appreciated.

Regards,  
Matthias Goetzke

I included some test data below for use in _sense_ etc ..:

_POST /test/file/3_  
{  
"name":"my file",  
}

_PUT test/file/\_mapping_

{  
"file": {  
"properties": {  
"created": {  
"type": "date",  
"format": "yyyyMMdd"  
},  
"name": {  
"type": "string"  
}  
}  
}  
}

Indexing documents like:

_POST /test/file/1_  
{  
"name":"my file",  
"created": 1384359659000  
}  
_POST /test/file/2_  
{  
"name":"my file",  
"created": "20120101"  
}

I would expect to be able to search using a date range query then:

_POST /test/file/\_search_  
{  
"query": {  
"query\_string": {  
"query": "[20120101 TO 20140101]"  
}  
}  
}

But this is not working .. what does work is WITH the explicit field:

_POST /test/file/\_search_  
{  
"query": {  
"query\_string": {  
"query": "created:[20120101 TO 20140101]"  
}  
}  
}

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [November 14, 2013, 6:02pm UTC](https://discuss.elastic.co/t/how-do-date-searches-work/14387/2 "2013-11-14T18:02:13Z")

</div>

Hello Matthias,

I think this is because query\_strings searches the "\_all" field by default,  
and that's not a date field, so date matches won't work. So maybe you can  
throw in the "created" field specifically. Something like:

{  
"query": {  
"query\_string": {  
"query": "created:[20120101 TO 20140101]",  
"fields": ["\_all", "created"]  
}  
}  
}

## Best regards, Radu

Performance Monitoring \* Log Analytics \* Search Analytics  
Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

On Wed, Nov 13, 2013 at 9:00 AM, Matthias Götzke [mgt576@gmail.com](mailto:mgt576@gmail.com) wrote:

> I am trying to store/search dates in ES but am experiencing some  
> difficulties. It seems a few things are going on there which i do not fully  
> understand yet.
> 
> When searching for a specific field with a _query\_string_ query then i  
> everything behaves as expected. E.g. searching for "_created:[20120101 to  
> 20150101]_" finds the document.
> 
> When searching without specifying the field "_[20120101 to 20150101]_" it  
> does only seem to perform a generic prefix match query and thus it returns  
> the second document in the database only not the one indexed using the unix  
> timestamp.
> 
> Is there anyway to let lucene or ES do all 3 types of range queries ? E.g.  
> perform a string match,perform a date range and perform a number match  
> using some default date formats ?
> 
> Any help is greatly appreciated.
> 
> Regards,  
> Matthias Goetzke
> 
> I included some test data below for use in _sense_ etc ..:
> 
> _POST /test/file/3_  
> {  
> "name":"my file",  
> }
> 
> _PUT test/file/\_mapping_
> 
> {  
> "file": {  
> "properties": {  
> "created": {  
> "type": "date",  
> "format": "yyyyMMdd"  
> },  
> "name": {  
> "type": "string"  
> }  
> }  
> }  
> }
> 
> Indexing documents like:
> 
> _POST /test/file/1_  
> {  
> "name":"my file",  
> "created": 1384359659000  
> }  
> _POST /test/file/2_  
> {  
> "name":"my file",  
> "created": "20120101"  
> }
> 
> I would expect to be able to search using a date range query then:
> 
> _POST /test/file/\_search_  
> {  
> "query": {  
> "query\_string": {  
> "query": "[20120101 TO 20140101]"  
> }  
> }  
> }
> 
> But this is not working .. what does work is WITH the explicit field:
> 
> _POST /test/file/\_search_  
> {  
> "query": {  
> "query\_string": {  
> "query": "created:[20120101 TO 20140101]"  
> }  
> }  
> }
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:06am UTC](https://discuss.elastic.co/t/how-do-date-searches-work/14387/3 "2017-07-06T02:06:57Z")

</div>


