How to get long value of date in result set of a search query

Hello there,

My question :
When I make a search query of indexed element, i want to parse the result
of date object value into a Java Object. To make that properly, and because
I use the Jest API, I need that the Json output format to be in Long, and
not in the formated String way.
Can I configure the query or the index to get the result in long value (as
date are stored in long value).

I need to keep the Date Object mapping and not a long one because I use
some specific aggregation in some query, and it's date, so I want to use
the date Object...
The problem don't appear when you use date aggregation like date_histogram,
because in result, you got the key in both formated way (Long and String).

Here an example to detail :

Mapping of the index :
PUT /testindex/test/_mapping
{
"test": {
"properties": {
"date": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
}

I use the format to put element.

then I do some search query :
GET /testindex/test/_search
{
"query": {
"match_all": {}
}
}

result are this way :
{
"took": 0,
...

},
"hits": {
...
"hits": [
{
"_index": "testindex",
"_type": "test",
"_id": "...",
"_score": 1,
"_source": {
"date": "2014-09-25T11:24:36",
}
}
]
}
}

but I want a date like "date": 1411509600000.
After what I will use Jest or other way to get the value and create a Date
Java Object with the long value.

For instance :
new
Date(resultElasticsearch.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray().get(0).getAsJsonObject().get("_source").getAsJsonObject().get("date").getAsLong());

--
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/0ad08992-10e0-46d4-8190-270b95d5c305%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hi,

In the results the _source contains the JSON you provided at index time. If
you want to get the long value of the date, you can use a script_fields :

{
"query": {
"match_all": {}
},
"script_fields": {
"dateAsLong": {
"script": "doc['date'].value"
}
},
"aggs": {
"messages_by_time": {
"date_histogram": {
"field": "date",
"interval": "day"
}
}
}
}

It seems a little odd to me to index a date field as a formatted string and having to retrieve it as a long because of the framework you use. Maybe I suggest to try to stick to only one type and look after Jest to convert the type as needed.

-- Tanguy

Le jeudi 2 octobre 2014 10:35:45 UTC+2, bernar...@gmail.com a écrit :

Hello there,

My question :
When I make a search query of indexed element, i want to parse the result
of date object value into a Java Object. To make that properly, and because
I use the Jest API, I need that the Json output format to be in Long, and
not in the formated String way.
Can I configure the query or the index to get the result in long value (as
date are stored in long value).

I need to keep the Date Object mapping and not a long one because I use
some specific aggregation in some query, and it's date, so I want to use
the date Object...
The problem don't appear when you use date aggregation like
date_histogram, because in result, you got the key in both formated way
(Long and String).

Here an example to detail :

Mapping of the index :
PUT /testindex/test/_mapping
{
"test": {
"properties": {
"date": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
}

I use the format to put element.

then I do some search query :
GET /testindex/test/_search
{
"query": {
"match_all": {}
}
}

result are this way :
{
"took": 0,
...

},
"hits": {
...
"hits": [
{
"_index": "testindex",
"_type": "test",
"_id": "...",
"_score": 1,
"_source": {
"date": "2014-09-25T11:24:36",
}
}
]
}
}

but I want a date like "date": 1411509600000.
After what I will use Jest or other way to get the value and create a Date
Java Object with the long value.

For instance :
new
Date(resultElasticsearch.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray().get(0).getAsJsonObject().get("_source").getAsJsonObject().get("date").getAsLong());

--
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/e62b5d15-21f7-4645-9103-0369e34a31e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thanks a lot for the answer.

I'm not totaly agree with you, for me , operation's retrieve should be
decorrelated from the way that you give the indexed value.
But as you had mentionned, and that was my main confusion, the _source
contains the JSON provided. I was expecting that the returned value can be
formated, or correspond to the format configuration gived in the mapping.

That's limit a lot the querying aspect. For me all that matters is the fact
that you manipulate a Date type, you can put a long or a string formated
date (in sevral way) value, both has to be indexed in the same way, and
then when you extract the value, determine the way that the format will be
in output.

Date is a special case, it's not a "primitive" one, It has median way to
store it (long), and I don't think that Elasticsearch manages it as a
"Type" but only as a special String, or Long Type.

(My hope is not the reference, and certainly I don't understand the main
keys of Elasticsearch)

To finish , as you advice, I will use only Long formated date (but that
will be hard and not really proper).

--
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/2a10cc90-f2d2-4809-a52f-47e98afdc139%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.