Creating kibana visualizations dynamically

Hi,

I am using elasticsearch 2.1.0 and kibana 4.3.0.
To create visualizations dynamically, first I created it manually.
Then I exported the same visualization .json file from Settings--Objects--Visualizations.

The .json is as follows:

[
  {
    "_id": "company",
    "_type": "visualization",
    "_source": {
      "title": "company",
      "visState": "{\"type\":\"pie\",\"params\":{\"shareYAxis\":true,\"addTooltip\":true,\"addLegend\":true,\"isDonut\":false},\"aggs\":[{\"id\":\"1\",\"type\":\"count\",\"schema\":\"metric\",\"params\":{}},{\"id\":\"2\",\"type\":\"terms\",\"schema\":\"segment\",\"params\":{\"field\":\"company\",\"size\":5,\"order\":\"desc\",\"orderBy\":\"1\"}}],\"listeners\":{}}",
      "uiStateJSON": "{}",
      "description": "",
      "version": 1,
      "kibanaSavedObjectMeta": {
        "searchSourceJSON": "{\"index\":\"t0hen\",\"query\":{\"query_string\":{\"query\":\"*\",\"analyze_wildcard\":true}},\"filter\":[]}"
      }
    }
  }
]

Then I deleted the above visualization and reposted it again to .kibana index

POST http://xx.xx.xx.xx:9200/.kibana/visualization/company
{
	"title": "company",
	"visState": {
		"type": "pie",
		"params": {
			"shareYAxis": true,
			"addTooltip": true,
			"addLegend": true,
			"isDonut": false
		},
		"aggs": [{
			"id": "1",
			"type": "count",
			"schema": "metric",
			"params": {}
		}, {
			"id": "2",
			"type": "terms",
			"schema": "segment",
			"params": {
				"field": "company",
				"size": 5,
				"order": "desc",
				"orderBy": "1"
			}
		}],
		"listeners": {}
	},
	"uiStateJSON": {},
	"description": "",
	"version": 1,
	"kibanaSavedObjectMeta": {
		"searchSourceJSON": {
			"index": "t0hen",
			"query": {
				"query_string": {
					"query": "*",
					"analyze_wildcard": true
				}
			},
			"filter": []
		}
	}
}

But, I am getting following error:

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse [visState]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse [visState]",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "unknown property [type]"
    }
  },
  "status": 400
}

Can anybody please help.

Hi @aviral_srivastava,

when posting the document to Elasticsearch, make sure to keep the visState field as one string with properly escaped nested quotes. From your sample POST body it looks like you've tries to post that field as an object while Elasticsearch expects a string.

Thanks @weltenwort

You saved my time.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.

For anyone else coming upon this thread, we now have an API for creating saved objects such as visualizations. See Kibana visualization and foreach? for more info.

CJ