简体   繁体   English

从 Elasticsearch 数据源获取 Grafana 中的最新元素

[英]Get most recent element in Grafana from an Elasticsearch data source

In the Grafana Query editor, I'm trying to replicate the following elasticsearch DSL query:在 Grafana 查询编辑器中,我尝试复制以下 elasticsearch DSL 查询:

{
  "query": {
    "match": {
        "hostname": "A"
    }
  },
    "sort": [
    { "utctimestamp": "desc" }
  ],
  "size": 1
}

It gives me most recent document in the index which has a field "hostname" with a value of "A".它为我提供了索引中的最新文档,其中包含一个值为“A”的字段“主机名”。

I can get the value of the risk_num field from the most recent element of my elasticsearch index with the following query :我可以使用以下查询从我的risk_num索引的最新元素中获取risk_num字段的值:

在此处输入图片说明

However, if i add a filter like hostname: "A" in the "Query" field, it will work if the most recent document in the index matches the 'A' hostname, but if the most recent document in the index has another hostname (let's say "B"), it will return the datetime of the document with the "B" hostname, and a risk_num value of null.但是,如果我在“查询”字段中添加像hostname: "A"这样的过滤器,如果索引中的最新文档与“A”主机名匹配,它将起作用,但如果索引中的最新文档具有另一个主机名(假设“B”),它将返回带有“B”主机名的文档的日期时间,并且 risk_num 值为 null。

Example : I have the two following documents saved in my ES index :示例:我的 ES 索引中保存了以下两个文档:

{
    "_source": {
        "hostname": "B",
        "risk_num": 20,
        "utctimestamp": "2020-10-21T12:12:34.356Z"
    }
},
{
    "_source": {
        "hostname": "A",
        "risk_num": 10,
        "utctimestamp": "2020-10-21T12:11:03.489Z"
    }
}

The document with hostname B is more recent that the document with hostname A.主机名 B 的文档比主机名 A 的文档更新。

Request for hostname B is OK:对主机名 B 的请求是可以的:

在此处输入图片说明

Request for hostname A returns null (expected: 10):对主机名 A 的请求返回 null(预期:10):

在此处输入图片说明

Any idea how I can get the last document of an index satisfying a certain condition in Grafana ?知道如何在 Grafana 中获取满足特定条件的索引的最后一个文档吗?

I think I got it working correctly by setting the "Min Doc Count" option to 1:我想我通过将“Min Doc Count”选项设置为 1 使其正常工作:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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