Get data from only the latest Elastic index

I’m using Grafana 8.2.6 (using Docker image grafana/grafana-oss:8.2.6).

I have a series of indexes in Elastic, myindex-YYYY.MM.DD. In a Grafana panel, I want to read data only from the latest such index each time (I want to show a table with current data). I have created a datasource [myindex-]YYYY.MM.DD with pattern Daily, but this reads from all indexes. I can’t find out whether limiting to the latest index should be done in the data source or in the panel options.

An alternative could be to filter the documents so that I get only those whose @timestamp equals the max @timestamp, but I can’t figure out this either. I can get the max @timestamp with this:

GET /myindex-*/_search
{
  "size": 0,
  "aggs": {
    "max_timestamp": { "max": { "field": "@timestamp" } }
  }
}

I’d need to save the result in a variable and use it in another query, but I can’t find a way to do this in Grafana.

I’ve answered my own question about this in stackoverflow.

1 Like