# Date query

**URL:** https://discuss.elastic.co/t/date-query/8757
**Category:** Elasticsearch
**Created:** [August 16, 2012, 9:38am UTC](https://discuss.elastic.co/t/date-query/8757 "2012-08-16T09:38:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Ashutosh\_Sharma](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@Ashutosh\_Sharma](https://discuss.elastic.co/u/Ashutosh_Sharma)
#### Post date: [August 16, 2012, 9:38am UTC](https://discuss.elastic.co/t/date-query/8757/1 "2012-08-16T09:38:53Z")

</div>

Hi All,

I created an index with the field name Logtime with the value "2012-08-14  
13:03:05,681". Now, I want to create a count query specifying the date  
range on the specified Logtime field. I am running below query as below:  
curl -XGET  
'[http://localhost:9200/txlog/\_search?pretty=1&search\_type=count](http://localhost:9200/txlog/_search?pretty=1&search_type=count)' -d '  
{  
"query" : {  
"range" : {  
"LOGTIME" : {  
"lt" : "2012-08-14 13:03:05,681"  
"gt" : "2012-08-14 13:03:05,681"  
}  
}  
}  
}'  
The result of above query is :  
{  
"took" : 4,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 0,  
"max\_score" : 0.0,  
"hits" : []  
}  
Mapping is as follows:  
$ curl -XGET '[http://localhost:9200/txlog/\_mapping](http://localhost:9200/txlog/_mapping)'  
{"txlog":{"transaction":{"properties":{"json":{"dynamic":"true","properties":{"API":{"type":"string"},"GLOBALID":{"type":"string"},"LOGTIME":{"type":"string"}}}}}}}

Even, if run the below command:  
curl -XGET '[http://localhost:9200/txlog/\_search?q=LOGTIME:](http://localhost:9200/txlog/_search?q=LOGTIME:)"2012-08-14  
13:03:06,441"'  
it says: curl: (52) Empty reply from server  
However, I can see the document with the specified logtime.

Is the stored logtime format is correct? Should I stored the logtime in  
another format? I want to keep the second information as part of the  
logtime. Please help...

Thanks  
Ashutosh Sharma

--

---

<div class="post-metadata">

### Author: ![stalinist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stalinist/32/1143_2.png) [@stalinist](https://discuss.elastic.co/u/stalinist)
#### Post date: [August 16, 2012, 5:38pm UTC](https://discuss.elastic.co/t/date-query/8757/2 "2012-08-16T17:38:33Z")

</div>

I think it's better to use timestamp. Then you could simply use comparison operators.

---

<div class="post-metadata">

### Author: ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)
#### Post date: [August 17, 2012, 9:16am UTC](https://discuss.elastic.co/t/date-query/8757/3 "2012-08-17T09:16:13Z")

</div>

Hi Ashutosh,

I think the reason why the range query isn't working is because the  
LOGTIME field is of type string.  
If you set the field LOGTIME to type date and use the correct date  
format (be default UTC, but this can be changed)  
then range queries should work. Take a look at the date type documentation:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Martijn

On 16 August 2012 11:38, Ashutosh Sharma [ashu1979@gmail.com](mailto:ashu1979@gmail.com) wrote:

> Hi All,
> 
> I created an index with the field name Logtime with the value "2012-08-14  
> 13:03:05,681". Now, I want to create a count query specifying the date range  
> on the specified Logtime field. I am running below query as below:  
> curl -XGET '[http://localhost:9200/txlog/\_search?pretty=1&search\_type=count](http://localhost:9200/txlog/_search?pretty=1&search_type=count)'  
> -d '  
> {  
> "query" : {  
> "range" : {  
> "LOGTIME" : {  
> "lt" : "2012-08-14 13:03:05,681"  
> "gt" : "2012-08-14 13:03:05,681"  
> }  
> }  
> }  
> }'  
> The result of above query is :  
> {  
> "took" : 4,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 0,  
> "max\_score" : 0.0,  
> "hits" :   
> }  
> Mapping is as follows:  
> $ curl -XGET '[http://localhost:9200/txlog/\_mapping](http://localhost:9200/txlog/_mapping)'  
> {"txlog":{"transaction":{"properties":{"json":{"dynamic":"true","properties":{"API":{"type":"string"},"GLOBALID":{"type":"string"},"LOGTIME":{"type":"string"}}}}}}}
> 
> Even, if run the below command:  
> curl -XGET '[http://localhost:9200/txlog/\_search?q=LOGTIME:](http://localhost:9200/txlog/_search?q=LOGTIME:)"2012-08-14  
> 13:03:06,441"'  
> it says: curl: (52) Empty reply from server  
> However, I can see the document with the specified logtime.
> 
> Is the stored logtime format is correct? Should I stored the logtime in  
> another format? I want to keep the second information as part of the  
> logtime. Please help...
> 
> Thanks  
> Ashutosh Sharma
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<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, 3:16am UTC](https://discuss.elastic.co/t/date-query/8757/4 "2017-07-06T03:16:06Z")

</div>


