# Elastic search range dates

**URL:** <https://discuss.elastic.co/t/elastic-search-range-dates/14992>\
**Category:** Elasticsearch\
**Created:** [December 22, 2013, 5:17pm UTC](https://discuss.elastic.co/t/elastic-search-range-dates/14992 "2013-12-22T17:17:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![melasticsearch](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@melasticsearch](https://discuss.elastic.co/u/melasticsearch)\
**Post date:** [December 22, 2013, 5:17pm UTC](https://discuss.elastic.co/t/elastic-search-range-dates/14992/1 "2013-12-22T17:17:07Z")

</div>

I have created an Elastic search index from a Mongo database.  
The documents in Mongo have the following structure:

```
{
    "_id" : ObjectId("525facace4b0c1f5e78753ea"),
    "time" : ISODate("2013-10-17T09:23:56.131Z"),
    "type" : "A",
    "url" : "www.google.com",
    "name" : "peter",
}

```

The index was created (apparently) without any problems.  
Now, I am trying to use Elastic Search to retrieve the documents in the index between two dates. I have read that I have to use range queries, but I have tried many times things like

```
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "peter").type(Type.PHRASE).minimumShouldMatch("99%");
LocalDateTime toLocal = new LocalDateTime(2013,12,18, 0, 0);
Date to = toLocal.toDate();
LocalDateTime fromLocal = new LocalDateTime(2013,12,17, 0, 0);
Date from = fromLocal.toDate();
RangeQueryBuilder queryDate = QueryBuilders.rangeQuery("time").to(to).from(from);
FilterBuilder filterDate = FilterBuilders.queryFilter(queryDate);		

srb = esH.client.prepareSearch("my_index");
srb.setQuery(queryBuilder);
srb.setFilter(filterDate);
sr = srb.execute().actionGet();

```

and I get 0 hits although there should be many results. I have tried to enter strings instead of dates, but same results.

When I perform a basic query without filters such as:

```
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "peter").type(Type.PHRASE).minimumShouldMatch("99%");
		
SearchRequestBuilder srb = esH.client.prepareSearch("my_index");
rb.setQuery(queryBuilder);
SearchResponse sr = srb.execute().actionGet();

```

I get hits with that look like this:

```
{
    "_index" : "my_index",
    "_type" : "type",
    "_id" : "5280d3c2e4b05e95aa703e34",
    "_score" : 1.375688, "_source" : {"type":["A"],"time":["Mon Nov 11 13:55:30 CET 2013"],"name":["peter"]}
}

```

Where the field `time` does not have the format `ISODate("2013-10-17T09:23:56.131Z")`anymore.

To sum up, what would be the Java code (and types) for querying between two dates (and times), taking into account the format?

---

<div class="post-metadata">

**Author:** ![melasticsearch](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@melasticsearch](https://discuss.elastic.co/u/melasticsearch)\
**Post date:** [December 31, 2013, 10:42am UTC](https://discuss.elastic.co/t/elastic-search-range-dates/14992/2 "2013-12-31T10:42:39Z")

</div>

no one?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [December 31, 2013, 1:50pm UTC](https://discuss.elastic.co/t/elastic-search-range-dates/14992/3 "2013-12-31T13:50:41Z")

</div>

Your query looks good. However, the way your hit is formatted in the  
results of the second query seems to indicate that your dates are indexed  
as strings instead of dates. How did you get your data from mongo to  
Elasticsearch?

On Tue, Dec 31, 2013 at 11:42 AM, melasticsearch \<  
[melasticsearch@mailinator.com](mailto:melasticsearch@mailinator.com)\> wrote:

> no one?
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Elastic-search-range-dates-tp4046565p4046854.html](http://elasticsearch-users.115913.n3.nabble.com/Elastic-search-range-dates-tp4046565p4046854.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> 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/1388486560274-4046854.post%40n3.nabble.com](https://groups.google.com/d/msgid/elasticsearch/1388486560274-4046854.post%40n3.nabble.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Adrien Grand

--  
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/CAL6Z4j5s3iiZaDgfvwkbgU9O5vxFJDLHJ\_w7kQadcsDvaZfCNA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5s3iiZaDgfvwkbgU9O5vxFJDLHJ_w7kQadcsDvaZfCNA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![melasticsearch](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@melasticsearch](https://discuss.elastic.co/u/melasticsearch)\
**Post date:** [January 3, 2014, 9:27am UTC](https://discuss.elastic.co/t/elastic-search-range-dates/14992/4 "2014-01-03T09:27:31Z")

</div>

Thanks for the reply.  
I am pretty sure that the dates are indexed as dates and not as strings, since the hits are of the type:

"\_score" : 1.375688, "\_source" : {"type":["A"],"time":["Mon Nov 11 13:55:30 CET 2013"],"name":["peter"]}

where the field "time" has already the format of a date. It seems to parse/format automatically from ISODate("2013-10-17T09:23:56.131Z") to dates such as the one in the sample.

---

<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, 1:58am UTC](https://discuss.elastic.co/t/elastic-search-range-dates/14992/5 "2017-07-06T01:58:32Z")

</div>


