Try to add Visualization with Kibana API

Hello,

I have some difficulties to add a new visualization with Kibana API.

I have the following pie.json:

{
"attributes": {
	"title": "[eCommerce] Sales by Gender2",
	"visState": "{  

"title":"[eCommerce] Sales by Gender2",
"type":"pie",
"params":{
"type":"pie",
"addTooltip":true,
"addLegend":true,
"legendPosition":"right",
"isDonut":true,
"labels":{
"show":true,
"values":true,
"last_level":true,
"truncate":100
}
},
"aggs":[
{
"id":"1",
"enabled":true,
"type":"count",
"schema":"metric",
"params":{

     }
  },
  {  
     \"id\":\"2\",
     \"enabled\":true,
     \"type\":\"terms\",
     \"schema\":\"segment\",
     \"params\":{  
        \"field\":\"customer_gender\",
        \"size\":5,
        \"order\":\"desc\",
        \"orderBy\":\"1\",
        \"otherBucket\":false,
        \"otherBucketLabel\":\"Other\",
        \"missingBucket\":false,
        \"missingBucketLabel\":\"Missing\"
     }
  }

]
}",
"uiStateJSON": "{}",
"description": "",
"version": 1,
"kibanaSavedObjectMeta": {
"searchSourceJSON": ""
}
}
}

(which is an exact copy of [eCommerce] Sales by Gender - generated by Kibana wizard - except that the title is modified)

I'm executing this curl command:

curl -X POST -H 'Content-Type: application/json' -d '@pie.json' -H 'kbn-xsrf:true' http://localhost:5601/api/saved_objects/visualization

That returns :
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2488 100 1325 100 1163 3329 2922 --:--:-- --:--:-- --:--:-- 6251{"type":"visualization","id":"e1254720-d0b7-11e9-b4d8-fd6505e6383d","attributes":{"title":"[eCommerce] Sales by Gender2","visState":"{ "title":"[eCommerce] Sales by Gender2", "type":"pie", "params":{ "type":"pie", "addTooltip":true, "addLegend":true, "legendPosition":"right", "isDonut":true, "labels":{ "show":true, "values":true, "last_level":true, "truncate":100 } }, "aggs":[ { "id":"1", "enabled":true, "type":"count", "schema":"metric", "params":{ } }, { "id":"2", "enabled":true, "type":"terms", "schema":"segment", "params":{ "field":"customer_gender", "size":5, "order":"desc", "orderBy":"1", "otherBucket":false, "otherBucketLabel":"Other", "missingBucket":false, "missingBucketLabel":"Missing" } } ]}","uiStateJSON":"{}","description":"","version":1,"kibanaSavedObjectMeta":{"searchSourceJSON":""}},"references":,"migrationVersion":{"visualization":"7.3.1"},"updated_at":"2019-09-06T15:06:18.642Z","version":"WzM5LDVd"}

(which seems ok)

Then I try to display my new visualization:

But when I click on "[eCommerce] Sales by gender2", nothing happens. ( "whereas [eCommerce] Sales by gender" works)

I checked under Dev tools, i'm getting both visualizations (the original, and the one that I inserted):

GET .kibana_1/_doc/visualization:ed8436b0-b88b-11e8-a6d9-e546fe2bba5f

{
"_index" : ".kibana_1",
"_type" : "_doc",
"_id" : "visualization:ed8436b0-b88b-11e8-a6d9-e546fe2bba5f",
"_version" : 1,
"_seq_no" : 5,
"_primary_term" : 1,
"found" : true,
"_source" : {
"visualization" : {
"title" : "[eCommerce] Sales by Gender",
"visState" : """{"title":"[eCommerce] Sales by Gender","type":"pie","params":{"type":"pie","addTooltip":true,"addLegend":true,"legendPosition":"right","isDonut":true,"labels":{"show":true,"values":true,"last_level":true,"truncate":100}},"aggs":[{"id":"1","enabled":true,"type":"count","schema":"metric","params":{}},{"id":"2","enabled":true,"type":"terms","schema":"segment","params":{"field":"customer_gender","size":5,"order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","missingBucket":false,"missingBucketLabel":"Missing"}}]}""",
"uiStateJSON" : "{}",
"description" : "",
"version" : 1,
"kibanaSavedObjectMeta" : {
"searchSourceJSON" : """{"query":{"query":"","language":"kuery"},"filter":,"indexRefName":"kibanaSavedObjectMeta.searchSourceJSON.index"}"""
}
},
"type" : "visualization",
"references" : [
{
"name" : "kibanaSavedObjectMeta.searchSourceJSON.index",
"type" : "index-pattern",
"id" : "ff959d40-b880-11e8-a6d9-e546fe2bba5f"
}
],
"migrationVersion" : {
"visualization" : "7.3.1"
},
"updated_at" : "2019-09-04T15:37:51.641Z"
}
}

GET .kibana_1/_doc/visualization:e1254720-d0b7-11e9-b4d8-fd6505e6383d

{
"_index" : ".kibana_1",
"_type" : "_doc",
"_id" : "visualization:e1254720-d0b7-11e9-b4d8-fd6505e6383d",
"_version" : 1,
"_seq_no" : 39,
"_primary_term" : 5,
"found" : true,
"_source" : {
"visualization" : {
"title" : "[eCommerce] Sales by Gender2",
"visState" : """{ "title":"[eCommerce] Sales by Gender2", "type":"pie", "params":{ "type":"pie", "addTooltip":true, "addLegend":true, "legendPosition":"right", "isDonut":true, "labels":{ "show":true, "values":true, "last_level":true, "truncate":100 } }, "aggs":[ { "id":"1", "enabled":true, "type":"count", "schema":"metric", "params":{ } }, { "id":"2", "enabled":true, "type":"terms", "schema":"segment", "params":{ "field":"customer_gender", "size":5, "order":"desc", "orderBy":"1", "otherBucket":false, "otherBucketLabel":"Other", "missingBucket":false, "missingBucketLabel":"Missing" } } ]}""",
"uiStateJSON" : "{}",
"description" : "",
"version" : 1,
"kibanaSavedObjectMeta" : {
"searchSourceJSON" : ""
}
},
"type" : "visualization",
"references" : ,
"migrationVersion" : {
"visualization" : "7.3.1"
},
"updated_at" : "2019-09-06T15:06:18.642Z"
}
}

Mine does not have any references, and other tags are empty (those tags being "outside" the attributes block)? Does this explain why I cannot display my visualization? How can i modify my json to make it work? Do I have to insert a new index template and link to it to make the whole thing work?

Regards,

Julien

A visualization object will need to point to an index pattern, hence the reference to that index pattern. If that is missing the object will not work, so that is what you need to add to your visualization:
"type" : "visualization",
"references" : [
{
"name" : "kibanaSavedObjectMeta.searchSourceJSON.index",
"type" : "index-pattern",
"id" : "ff959d40-b880-11e8-a6d9-e546fe2bba5f"
}