Import a visualisation using API

Hi all,

I have exported a visualisation in Kibana, using the export button under settings. This has given me a JSON file. The name of the visualisation is Transaction-count.

I have then deleted the visualisation from Kibana.

I would now like to import the visualisation back into Kibana using the API.

If I type
curl -XPUT "http://localhost:9200/.kibana/visualization/Transaction-count" -d I get the following error;

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}}

If I manually import the visualisation via the web GUI, it works fine.

If I then type;
curl XGET "http://localhost:9200/.kibana/visualization/Transaction-count" it gives me the following JSON;

{"_index":".kibana","_type":"visualization","_id":"Transaction-count","_version":1,"found":true,"_source":{"title":"Transaction count","visState":"{"title":"Transaction count","type":"metric","params":{"handleNoResults":true,"fontSize":"120"},"aggs":[{"id":"1","type":"count","schema":"metric","params":{}}],"listeners":{}}","uiStateJSON":"{}","description":"","savedSearchId":"Transactions","version":1,"kibanaSavedObjectMeta":{"searchSourceJSON":"{"filter":[]}"}}}

If I then type;
curl -XGET "http://localhost:9200/.kibana/visualization/Transaction-count" -d '{"_index":".kibana","_type":"visualization","_id":"Transaction-count","_version":1,"found":true,"_source":{"title":"Transaction count","visState":"{"title":"Transaction count","type":"metric","params":{"handleNoResults":true,"fontSize":"120"},"aggs":[{"id":"1","type":"count","schema":"metric","params":{}}],"listeners":{}}","uiStateJSON":"{}","description":"","savedSearchId":"Transactions","version":1,"kibanaSavedObjectMeta":{"searchSourceJSON":"{"filter":[]}"}}}'

I receive the following output;

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"Field [_index] is a metadata field and cannot be added inside a document. Use the index API request parameters."}],"type":"mapper_parsing_exception","reason":"Field [_index] is a metadata field and cannot be added inside a document. Use the index API request parameters."},"status":400}

Running version 2.3.3 of Elastic and 4.5.1 of Kibana

What am I doing wrong?

If you are looking to manually import, your going to need manipulate the request to Elasticsearch a bit.

The export looks something like this:

[
  {
    "_id": "My-New-Visualization",
    "_type": "visualization",
    "_source": {
      "title": "My New Visualization",
      "visState": "{...}",
      "uiStateJSON": "{}",
      "description": "",
      "savedSearchId": "Kibana",
      "version": 1,
      "kibanaSavedObjectMeta": {
        "searchSourceJSON": "{\"filter\":[]}"
      }
    }
  }
]

To index this document, you would need to extract the contents of _source and create a document with the id containing the old document id (_id).

This leaves me with an updated JSON file (my-viz.json) containing:

{
  "title": "My New Visualization",
  "visState": "{...}",
  "uiStateJSON": "{}",
  "description": "",
  "savedSearchId": "Kibana",
  "version": 1,
  "kibanaSavedObjectMeta": {
    "searchSourceJSON": "{\"filter\":[]}"
  }
}

Now I just need to index that file.

curl -X PUT --data @my-viz.json 'http://localhost:9200/.kibana/visualization/My-New-Visualization'

If you are going to be automatic this, it would be best to use your programming language of choice and parse the export, then using the bulk API to insert all visualizations at once.

1 Like

Hey Tyler,

Thanks for coming back to me. I appreciate your help.

It looks like the long term solution would definitely be to parse the JSON and make the required amendments.

Many thanks
Stephen

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