Hello everyone,
I tried something and I think it's worth sharing
I'm trying to write a script to automatically create a new dashboard according to a specific user (for that, it is the search that changes).
More precisely, as a dashboard is made of visualizations, we need to create the visualizations in the first place. I was blocked at this step, creating the visualization directly via a CURL -XPOST request to ElasticSearch.
Because in order to do the final dashboard, we need to :
- Write the search(es)
- Create the visualization(s)
- Create the dashboard made of visualizations
Schematically :
Input : Username --> Myscript --> Dashboard of the user
To make things clear, a visualization is only a JSON document in the path (in my case, but surely in yours too) :
'http://localhost:9200/.kibana/visualization/*'
In ElasticSearch, and Kibana reads it to display it.
So it's simple, add a new visualization as you add a new document through the ElasticSearch API.
To do that, and to know what the visualization you want to create looks like, you can create it using Kibana web interface; and once you validate and it's added in ElasticSearch, you see the ElasticSearch document.
Let's say we created a Pie Chart using Kibana named "Test1" : we request it in ElasticSearch to see the document.
curl -XGET 'http://localhost:9200/.kibana/visualization/Test1'
You should have this kind of result :
{
"_index":".kibana",
"_type":"visualization",
"_id":"Test1",
"_version":1,
"found":true,
"_source":{
"title":"Test1",
"visState":"{\"aggs\":[{\"id\":\"1\",\"params\":{},\"schema\":\"metric\",\"type\":\"count\"},{\"id\":\"2\",\"params\":{\"field\":\"type.raw\",\"order\":\"desc\",\"orderBy\":\"1\",\"size\":10},\"schema\":\"segment\",\"type\":\"terms\"}],\"listeners\":{},\"params\":{\"addLegend\":true,\"addTooltip\":true,\"isDonut\":false,\"shareYAxis\":true},\"title\":\"New Visualization\",\"type\":\"pie\"}",
"uiStateJSON":"{}",
"description":"",
"version":1,
"kibanaSavedObjectMeta":{
"searchSourceJSON":"{\"index\":\"logstash-*\",\"query\":{\"query_string\":{\"analyze_wildcard\":true,\"query\":\"YOUR KIBANA DISCOVER SEARCH HERE\"}},\"filter\":[]}"
}
}
}
So, then, we want to create a similar document into ElasticSearch directly, without using Kibana, as we would need it in a script. The query looks like this :
curl -XPOST 'http://localhost:9200/.kibana/visualization/Test2' -d '
{"title":"Test2","visState":"{\"aggs\":[{\"id\":\"1\",\"params\":{},\"schema\":\"metric\",\"type\":\"count\"},{\"id\":\"2\",\"params\":{\"field\":\"type.raw\",\"order\":\"desc\",\"orderBy\":\"1\",\"size\":10},\"schema\":\"segment\",\"type\":\"terms\"}],\"listeners\":{},\"params\":{\"addLegend\":true,\"addTooltip\":true,\"isDonut\":false,\"shareYAxis\":true},\"title\":\"New Visualization\",\"type\":\"pie\"}","uiStateJSON":"{}","description":"","version":1,"kibanaSavedObjectMeta":{"searchSourceJSON":"{\"index\":\"logstash-*\",\"query\":{\"query_string\":{\"analyze_wildcard\":true,\"query\":\"YOUR KIBANA DISCOVER SEARCH HERE\"}},\"filter\":[]}"}}
'
Note that we used the Source part of the first query to make the POST.
That's it ! You can check the Pie Chart has been well created in Kibana
Have a good day, I hope this has been useful.
Tony