Grafana中如何查询到Elasticsearch

How to query to Elasticsearch in Grafana

我在 Elasticsearch 中有以下数据。(由于数据有点长,我将其附加在底部。)我正在尝试弄清楚如何查询以便在 Grafana 中的图表上显示某些内容。

我知道我在 elasticsearch 中的数据不适合图表,但我没有任何其他示例数据,我只是想了解如何处理此查询。我用 mysql 和 grafana 做过类似的事情。 (数据不适合图表,但我设法使它像下图一样)

我想用 Elasticsearch 做上面的事情。由于他们使用 Lucene 查询(不同于 Mysql),我阅读了一些相关信息。但我不知道把什么放在哪里。我可能想展示 metascore,或 votes(请查看 Json底部)在图表上。

我用的是 Postman。
这是 JSON 类型的数据。 (我想用'votes'或'metascore'出现在图表上)

{
    "took": 113,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 8,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "dzb6AGoBnuCYnlTWZNmc",
                "_score": 1,
                "_source": {
                    "name": "Justice League",
                    "genre": "Action",
                    "summary": "Fueled by his restored faith in humanity and inspired by Superman's selfless act, Bruce Wayne enlists the help of his newfound ally, Diana Prince, to face an even greater enemy",
                    "yearofrelease": 201,
                    "metascore": 45,
                    "votes": 275122,
                    "rating": 6.6
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "name": "Justice League1",
                    "genre": "Action1",
                    "summary": "Fueled by his restored faith in humanity and inspired by Superman's selfless act, Bruce Wayne enlists the help of his newfound ally, Diana Prince, to face an even greater enemy",
                    "yearofrelease": 201,
                    "metascore": 45,
                    "votes": 275122,
                    "rating": 6.6
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "name": "Justice League",
                    "genre": "Action",
                    "summary": "Fueled by his restored faith in humanity and inspired by Superman's selfless act, Bruce Wayne enlists the help of his newfound ally, Diana Prince, to face an even greater enemy",
                    "yearofrelease": 2011,
                    "metascore": 45,
                    "votes": 275122,
                    "rating": 6.6
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "hDYYAWoBnuCYnlTWyNmZ",
                "_score": 1,
                "_source": {
                    "name": "Thor Ragnarok",
                    "genre": "Action",
                    "summary": "Thor is imprisoned on the planet Sakaar, and must race  against time to return to Asgard and stop Ragnarök, the destruction of his world, at the hands of the powerful and ruthless villain Hela",
                    "yearofrelease": 2017,
                    "metascore": 74,
                    "votes": 374270,
                    "rating": 7.9
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "hTYYAWoBnuCYnlTWyNmZ",
                "_score": 1,
                "_source": {
                    "name": "Infinity War",
                    "genre": "Sci-Fi",
                    "summary": "The Avengers and their allies must be willing to sacrifice all in an attempt to defeat the powerful Thanos before his blitz of devastation and ruin puts an end to the universe",
                    "yearofrelease": 2018,
                    "metascore": 68,
                    "votes": 450856,
                    "rating": 8.6
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "hzYYAWoBnuCYnlTW-9mV",
                "_score": 1,
                "_source": {
                    "name": "Thor Ragnarok",
                    "genre": "Action",
                    "summary": "Thor is imprisoned on the planet Sakaar, and must race  against time to return to Asgard and stop Ragnarök, the destruction of his world, at the hands of the powerful and ruthless villain Hela",
                    "yearofrelease": 2017,
                    "metascore": 74,
                    "votes": 374270,
                    "rating": 7.9
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "iDYYAWoBnuCYnlTW-9mV",
                "_score": 1,
                "_source": {
                    "name": "Infinity War",
                    "genre": "Sci-Fi",
                    "summary": "The Avengers and their allies must be willing to sacrifice all in an attempt to defeat the powerful Thanos before his blitz of devastation and ruin puts an end to the universe",
                    "yearofrelease": 2018,
                    "metascore": 68,
                    "votes": 450856,
                    "rating": 8.6
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "iTYYAWoBnuCYnlTW-9mV",
                "_score": 1,
                "_source": {
                    "name": "Christopher Robin",
                    "genre": "Comedy",
                    "summary": "A working-class family man, Christopher Robin, encounters his childhood friend Winnie-the-Pooh, who helps him to  rediscover the joys of life",
                    "yearofrelease": 2018,
                    "metascore": 60,
                    "votes": 9648,
                    "rating": 7.9
                }
            }
        ]
    }
}

如果此数据无法显示在图表上,那么您能否给我一些提示,告诉我可以在下面的查询和指标字段中输入什么,以便我在有其他示例数据时可以处理此问题。我需要一个像 'metascore' 这样的例子应该放在这个字段中,你需要 @ 或 _ 并在此处放置索引,然后在此处键入,等等)

我明白了。

首先,您需要转到 Grafana 中的数据源,然后 选择 Elasticsearch

你把你的 index 名字放在这里。最重要的是,您需要一个可以报时的字段。我使用了日期字段。我做了一个简单的示例数据,如下所示。 (查看 日期 字段。)

转到 Grafana 仪表板。并且在查询中,您会看到 Date Histogram 将自动设置为 date。选择所需的指标。

您会在图表上看到类似这样的内容。

我知道我的数据、字段等不是理想的图表material。但我专注于如何,而不是什么。请将其视为 Elastic 和 Grafana 的 'hello world'。希望这可以帮助某人并节省他们的时间和精力。