Sorting on a date field

You have two options, first is to have a numeric (long) type for the field,
second is to have a date type, which expects (by default) an ISO formatted
date string.

If you use the Java API, you have two options:

  1. If you want the field to be numeric, then just add the "milliseconds
    since epoch" value (Date.getTime()) to your Map of values.
  2. If you want the field to be a date type, then either provide the
    formatted string yourself, or, pass a Date instance as the value of the
    field, it will automatically be formatted to its ISO format and indexed.

In both options, you don't need to explicitly set the mappings. It will be
auto detected. I say, since you already have a Date object, to index it
(which will, in turn, be formatted as ISO formatted string, and detected as
such in the date type).

Once you have index it, you will be able to sort it by just adding it as
sort field in the search API.

Regarding the resolution, I am not sure I understand what you mean by
missing resolution, but make sure the data you index does actually offer the
resolution you expected (the Date instance you get, does it have that
resolution?).

-shay.banon

On Thu, Nov 11, 2010 at 12:27 AM, John Chang jchangkihtest2@gmail.comwrote:

I'm afraid I must not be understanding your advice. I guess I need more
specifics about how you want me to (A) map, (B) index and (C) search the
doc.

I read your response and then....

I tried mapping the field this way:
"receivedDate" : {
"index_name": "receivedDate",
"type": "date",
"index": "analyzed",
"store": "yes",
"term_vector": "no",
"boost": 1.0,
"omit_norms": "false",
"omit_term_freq_and_positions": "false"
}

I then tried these combinations (data.getReceivedDateInUTC() returns a
java.util.Date):

Map<String, Object> document = new HashMap<String, Object>();
document.put("receivedDate", data.getReceivedDateInUTC());

When I searched on the doc with a script:
.script("doc['" + sortField + "'].value")
it worked, but lacked precision with dates close in time (under ~ 3 min).

1.1)
I indexed the doc as in 1 above, and I searched on the doc with a sort:
.sort(sortField)
I got SearchPhaseExecutionException

Map<String, Object> document = new HashMap<String, Object>();
document.put("receivedDate", data.getReceivedDateInUTC().getTime());

I could not index the doc; I got:
java.lang.IllegalArgumentException: Invalid format: "1204351200000" is
malformed at "0000"

Didn't expect this to work, but was taking a guess because nothing else
did.

Map<String, Object> document = new HashMap<String, Object>();
document.put("receivedDate",
Long.toString(data.getReceivedDateInUTC().getTime()));

I could not index the doc; I got:
java.lang.IllegalArgumentException: Invalid format: "1204351200000" is
malformed at "0000"
(same as #2 above)

Your response mentioned a date string: "The resolution of the long value is
based on the date string passed." Hence this test.

Map<String, Object> document = new HashMap<String, Object>();
document.put("receivedDate", data.getReceivedDateInUTC().toString());

I could not index the doc; I got:
org.elasticsearch.index.mapper.MapperParsingException: Failed to parse
[receivedDate]

As in 3 above, this was a test based on my understanding of a date string
from your response.

Thanks for your help.

View this message in context:
http://elasticsearch-users.115913.n3.nabble.com/Sorting-on-a-date-field-tp1873287p1879190.html
Sent from the Elasticsearch Users mailing list archive at Nabble.com.