# Can't search on date field

**URL:** https://discuss.elastic.co/t/cant-search-on-date-field/8784
**Category:** Elasticsearch
**Created:** [August 19, 2012, 3:24pm UTC](https://discuss.elastic.co/t/cant-search-on-date-field/8784 "2012-08-19T15:24:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Mary\_Hamlin](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@Mary\_Hamlin](https://discuss.elastic.co/u/Mary_Hamlin)
#### Post date: [August 19, 2012, 3:24pm UTC](https://discuss.elastic.co/t/cant-search-on-date-field/8784/1 "2012-08-19T15:24:10Z")

</div>

Hello. New elasticsearch user here. I'm sure you will be seeing many more  
posts from me as I muddle my way along on the way to a solid understanding  
of the product. Mostly I've just been lurking the forums, trying to piece  
together different pieces of information. Currently I'm playing around in a  
sandbox environment, trying to learn about indexing and mapping and sharding  
and everything else that there is to learn about, and trying not to get  
overwhelmed in the process by how much there actually is to learn. Anyway,  
on to my first question...

So I have an index that has a field called post\_date that is a date field.  
From the index mapping...

post\_date: {  
format: dateOptionalTime  
type: date  
}

So I have a bunch of documents with dates in the standard date format, e.g.  
"2012-07-15T14:12:12", and I was trying to search on documents in a certain  
date range. I have used many variations of queries that I got from reading  
the documentation and forum posts but none of them seem to be working. By  
that I mean that when I search the index on the date range, the number of  
hits that I get back are the total number of documents in the index, so it  
doesn't seem to be applying my filter.

So here is the final query that I ended up with:

{  
"query": {  
"constant\_score": {  
"filter": {  
"numeric\_range": {  
"post\_date": {  
"lt": "2012-07-16",  
"gte": "2012-07-01"  
}  
}  
}  
}  
}  
}

I know from the data in the index that there are dates both in this range  
and not in this range, so I would expect to have it return only the dates in  
that range. However, as I said, it's returning everything.

So does anyone have an idea about why this isn't working and ultimately how  
I can write the query so that it does what I would expect?

Many thanks in advance.

--  
View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Can-t-search-on-date-field-tp4021882.html](http://elasticsearch-users.115913.n3.nabble.com/Can-t-search-on-date-field-tp4021882.html)  
Sent from the ElasticSearch Users mailing list archive at [Nabble.com](http://Nabble.com).

--

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [August 24, 2012, 6:48am UTC](https://discuss.elastic.co/t/cant-search-on-date-field/8784/2 "2012-08-24T06:48:40Z")

</div>

Hi Mary

> So I have an index that has a field called post\_date that is a date field.  
> From the index mapping...
> 
> post\_date: {  
> format: dateOptionalTime  
> type: date  
> }
> 
> By that I mean that when I search the index on the date range, the number of  
> hits that I get back are the total number of documents in the index, so it  
> doesn't seem to be applying my filter.

By the looks of what you've posted, your query should work, which  
probably means that you're doing something else wrong that you haven't  
mentioned so far.

I suggest gisting a complete curl recreation (including index creation,  
indexing a few docs, and the query) which will help us see the issue.

ta

clint

--

---

<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:15am UTC](https://discuss.elastic.co/t/cant-search-on-date-field/8784/3 "2017-07-06T03:15:14Z")

</div>


