简体   繁体   中英

Add _timestamp to Elasticsearch results using Java API

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM