2

I have an Elasticsearch index which has _timestamp populated on every record. Using Marvel or curl I can get the _timestamp in the "fields" part of the result for example:

GET index/type/_search?fields=_timestamp,_source
{
   "took": 11,
   "timed_out": false,
   "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
   },
   "hits": {
      "total": 116888,
      "max_score": 1,
      "hits": [
         {
            "_index": "index",
            "_type": "type",
            "_id": "mXJdWqSLSfykbMtChiCRjA",
            "_score": 1,
            "_source": {
               "results": "example",
            },
            "fields": {
               "_timestamp": 1443618319514
            }
         },...

However when doing a search using the Java API I cant get it to return the _timestamp.

SearchRequestBuilder builder= client.prepareSearch(index)
            .addFacet(facet)
            .setFrom(start)
            .setSize(limit);
SearchResponse response = builder.execute().actionGet();

Can anyone tell me how to ask for _timestamp too?

1 Answer 1

1

You simply need to use the setFields() method like this:

SearchRequestBuilder builder= client.prepareSearch(index)
            .setType(type)
            .addFacet(facet)
            .setFields("_timestamp")          <--- add this line
            .setFrom(start)
            .setSize(limit);
SearchResponse response = builder.execute().actionGet();
Sign up to request clarification or add additional context in comments.

9 Comments

.setFields is not an available method in my version of ES however I used addFields instead and it still did not return the timestamp. Any other ideas? SearchRequestBuilder buider = client.prepareSearch(index) .addFacet(facet) .addFields("_timestamp", "_source") .setFrom(start) .setSize(limit); return builder;
And what version is that?
Elasticsearch version 1.1.1. When I added .addFields('_timestamp') then the source disappeared. But when I used .addFields("timestamp", "_source") the source returns again but still no time stamp, even though it is being stored.
1.1.1, ouch :) It's weird, though, because I could find the setFields method in the 1.1.1 release.
Unfortunately 1.1.1 :( Yes but that is for the GetRequestBuilder whereas im using the SearchRequestBuilder
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.