Elastic search range dates


(melasticsearch) #1

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?


(melasticsearch) #2

no one?


(Adrien Grand) #3

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> wrote:

no one?

--
View this message in context:
http://elasticsearch-users.115913.n3.nabble.com/Elastic-search-range-dates-tp4046565p4046854.html
Sent from the ElasticSearch Users mailing list archive at 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.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/1388486560274-4046854.post%40n3.nabble.com
.
For more options, visit 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5s3iiZaDgfvwkbgU9O5vxFJDLHJ_w7kQadcsDvaZfCNA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


(melasticsearch) #4

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.


(system) #5