The document source contains the content you sent over the API, so you see the long value in return when you sent a long. In Java, you can extract those values by hit.sourceAsMap().get(...)
Accessing a field of a specific type over the Java API is controlled by the value conversions of Elasticsearch mapping specification. So you will see the expected date format because of the date type in the mapping.
If you index dates before Jan 1, 1970:, e.g.
field("doc_date", -9184838400000 )
you will get negative long
applying toEpochMilli()
as specified by the java.time.Instant
class.
SearchResponse response = client.prepareSearch()
.setQuery(matchAllQuery())
.addStoredField("_source")
.addStoredField("doc_date")
.execute().actionGet()
println response
response.hits.hits.each { hit ->
println hit.sourceAsMap().get("doc_date") // numeric
println hit.fields.get("doc_date").value() // date string
Instant instant = Instant.parse(hit.fields.get("doc_date").value())
long millis = instant.toEpochMilli() // parsed date
println millis
}
This will give a result like
{"took":1,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"test","_type":"test","_id":"1","_score":1.0,"_source":{"doc_date":-9184838400000},"fields":{"doc_date":["1678-12-11T00:00:00.000Z"]}}]}}
Note the difference between _source
and doc_date
.