Yeah, sorry, I see I did not properly open up my index code above. Shouldn't post when too tired..
I believe I do index long and it used to work.. But I can certainly use the workaround you suggested. Thanks.
It just seems a bit of a waste if ES stores the date as long, then every time I query ES converts it to a string, gives me the string, I parse it to long..
Here is some better example code to illustrate my question:
public class DateTest {
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
Client client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
//create index and mapping if needed
IndicesExistsResponse res = client.admin().indices().prepareExists("doc_test_index").execute().actionGet();
if (!res.isExists()) {
CreateIndexRequestBuilder reqBuilder = client.admin().indices().prepareCreate("doc_test_index");
String testSchema = "{\n" +
" \"properties\": {\n" +
" \"stuff\": {\n" +
" \"type\": \"text\",\n" +
" \"index\": \"analyzed\"\n" +
" },\n" +
" \"doc_date\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"strict_date_optional_time||epoch_millis\",\n" +
" \"store\": true\n" +
" }\n" +
" }\n" +
"}";
reqBuilder.addMapping("test_mapping", testSchema);
reqBuilder.execute().actionGet();
//insert example data
Map<String, Object> map = new HashMap<>();
map.put("stuff", "just something");
map.put("doc_date", System.currentTimeMillis());
IndexResponse response = client.prepareIndex("doc_test_index", "test_mapping", "1")
.setSource(map)
.get();
}
SearchResponse scrollResp = client.prepareSearch()
.setIndices("doc_test_index")
.setTypes("test_mapping")
.addStoredField("doc_date")
.setQuery(QueryBuilders.matchAllQuery())
.setSize(10).execute().actionGet();
for (SearchHit hit : scrollResp.getHits().getHits()) {
Map<String, SearchHitField> fields = hit.getFields();
Object docDate = fields.get("doc_date").value();
System.out.println(docDate);
}
}
}
Sorry for the lenghty spam but that hopefully better illustrates my question.
For me this prints out (from the system.out in the for loop for results)
2016-12-11T10:01:41.749Z
Now if I run curl:
curl -XGET 'http://localhost:9200/doc_test_index/test_mapping/1'
I get:
{"_index":"doc_test_index","_type":"test_mapping","_id":"1","_version":1,"found":true,"_source":{"doc_date":1481450501749,"stuff":"just something"}}
It seems to me that I store the value as long, curl gets it as long, Java API used to get is as long but now gets it as formatted date string.
So this is what I was wondering. Is this some change in the Java API, and Is there some way to get the long without going through all the conversions?
For now, I guess I will go with the proposed parsing so I get it to work but would be nice to understand what changed..
Thanks,
T