How to query to Elasticsearch in Grafana

16,491

I figured it out.

First, you need to go to Data Sources in Grafana, and choose Elasticsearch.

Data sources

enter image description here You put your index name here. And most importantly, you need a field that can tell time. I used date field. I made a simple sample data like the below. (Look at the date field.) sample

Go to Grafana dashboard. And in the query, you will see that Date Histogram will be automatically set to date. Choose the metric that you want.

enter image description here

You will see something like this on your graph.

enter image description here

I know my data, field, and so on is not ideal graph material. But I focused on how, not what. Please think of this as 'hello world' for Elastic and Grafana. Hope this helps someone and save their time and energy.

Share:
16,491
Jin Lee
Author by

Jin Lee

Wassup World Blog : cookiethecat2020.blogspot.com YouTube Channel : https://www.youtube.com/channel/UCHXFo0QdaXmrQKYgB6CzBHw

Updated on June 04, 2022

Comments

  • Jin Lee
    Jin Lee almost 2 years

    I have the following data in Elasticsearch.(Since data is a little bit long, I've attached it at the bottom.) I am trying to figure out how to query in order to show something on a graph in Grafana.

    I know my data in elasticsearch is not suitable for graphs, but I don't have any other sample data, and I just want to learn how to work this query. I've done something like this with mysql and grafana. (The data wasn't suitable for a graph, but I managed to make it like the below image) Mysql and Grafana

    I want to do the above with Elasticsearch. Since they use Lucene query(different from Mysql), I read some information on that. But I have no clues on what to put where. I want to show maybe metascore, or votes (please look at the Json at the bottom) on a graph.

    Elasticsearch and Grafana

    I used Postman.
    enter image description here Here is the JSON type data. (I want to use 'votes' or 'metascore' to appear on the graph)

    {
        "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
                    }
                }
            ]
        }
    }
    

    If this data is impossible to be displayed on a graph, then could you give me some tips on what I can put in the query and metric fields below so that I can work this when I have other sample data. I need an example like 'metascore' should go in this field, and you need @ or _ and put index here, and type there, etc)

    enter image description here