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

**URL:** <https://discuss.elastic.co/t/how-to-get-long-value-of-date-in-result-set-of-a-search-query/20042>\
**Category:** Elasticsearch\
**Created:** [October 2, 2014, 8:35am UTC](https://discuss.elastic.co/t/how-to-get-long-value-of-date-in-result-set-of-a-search-query/20042 "2014-10-02T08:35:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernard\_drigs](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@bernard\_drigs](https://discuss.elastic.co/u/bernard_drigs)\
**Post date:** [October 2, 2014, 8:35am UTC](https://discuss.elastic.co/t/how-to-get-long-value-of-date-in-result-set-of-a-search-query/20042/1 "2014-10-02T08:35:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tanguy\_Leroux](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguy_leroux/32/121066_2.png) [@Tanguy\_Leroux](https://discuss.elastic.co/u/Tanguy_Leroux)\
**Post date:** [October 2, 2014, 9:19am UTC](https://discuss.elastic.co/t/how-to-get-long-value-of-date-in-result-set-of-a-search-query/20042/2 "2014-10-02T09:19:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bernard\_drigs](https://avatars.discourse-cdn.com/v4/letter/b/8e7dd6/32.png) [@bernard\_drigs](https://discuss.elastic.co/u/bernard_drigs)\
**Post date:** [October 2, 2014, 3:21pm UTC](https://discuss.elastic.co/t/how-to-get-long-value-of-date-in-result-set-of-a-search-query/20042/3 "2014-10-02T15:21:42Z")

</div>

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

---

<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, 12:58am UTC](https://discuss.elastic.co/t/how-to-get-long-value-of-date-in-result-set-of-a-search-query/20042/4 "2017-07-06T00:58:37Z")

</div>


