Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch index last update time

Tags:

Is there a way to retrieve from ElasticSearch information on when a specific index was last updated? My goal is to be able to tell when it was the last time that any documents were inserted/updated/deleted in the index. If this is not possible, is there something I can add in my index modification requests that will provide this information later on?

like image 823
dchar Avatar asked Dec 02 '14 17:12

dchar


People also ask

What is Elasticsearch refresh interval?

By default, Elasticsearch periodically refreshes indices every second, but only on indices that have received one search request or more in the last 30 seconds.

How do I set timestamp in Elasticsearch?

If you're running Elasticsearch version 6.5 or newer, you can use the index. default_pipeline settings to create a timestamp field for an index. This can be accomplished by using the Ingest API and creating a pipeline at the time your index is created.

What does refresh index do in Elasticsearch?

A refresh makes recent operations performed on one or more indices available for search. For data streams, the API runs the refresh operation on the stream's backing indices.

Can we update index in Elasticsearch?

For little changes in Index or index settings you can use update API where you can update index settings ( No of replicas, refresh interval etc.) . Also, you can update documents and add field using update API in Elasticsearch.


1 Answers

You can get the modification time from the _timestamp

To make it easier to return the timestamp you can set up Elasticsearch to store it:

curl -XPUT "http://localhost:9200/myindex/mytype/_mapping" -d'
{
  "mytype": {
      "_timestamp": {
          "enabled": "true",
          "store": "yes"
      }
  }
}'

If I insert a document and then query on it I get the timestamp:

 curl -XGET 'http://localhost:9200/myindex/mytype/_search?pretty' -d '{
>  fields : ["_timestamp"],
>    "query": {
>     "query_string": { "query":"*"}
>    }
> }'
{
   "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
     "total" : 1,
     "max_score" : 1.0,
     "hits" : [ {
       "_index" : "myindex",
       "_type" : "mytype",
       "_id" : "1",
       "_score" : 1.0,
       "fields" : {
        "_timestamp" : 1417599223918
      }
    } ]
  }
}

updating the existing document:

curl -XPOST "http://localhost:9200/myindex/mytype/1/_update" -d'
{
  "doc" : {
      "field1": "data",
      "field2": "more data"
  },
  "doc_as_upsert" : true
}'

Re-running the previous query shows me an updated timestamp:

  "fields" : {
    "_timestamp" : 1417599620167
  }
like image 183
Olly Cruickshank Avatar answered Oct 02 '22 07:10

Olly Cruickshank