Need help to create a Python script to create Index on Cluster helath API

Hi Experts ,

I am new to python , I want to create an index in ES on cluster health API . I got the following code but it's not providing me output the way I want .

import json
import requests
stats = requests.get('http://localhost:9200/_cluster/health').json()
abc=stats['cluster_name']
bulk_data = ''
bulk_data += '{"index": {"_index": "cluster", "_type": "clust"}}\n'
bulk_data += json.dumps(abc) + '\n'
response = requests.post('http://localhost:9200/_bulk', data=bulk_data)

Above script is able to create an index but when I am checking the data I got nothing , even in kibana when I select _timestamp filed I got nothing . Not sure what I am doing wrong . In kibana I want to see all the fields like .

"cluster_name": "elasticsearch",
"status": "yellow",
"timed_out": false,
"number_of_nodes": 1,
"number_of_data_nodes": 1,
"active_primary_shards": 92,
"active_shards": 92,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 92,
"number_of_pending_tasks": 0,
"number_of_in_flight_fetch": 0

Please help

I tried your script but it didn't output anything to ES, in part because you need to use _index and _type in the bulk request, but then the data that was being passed into that bulk request was only the cluster name, which is not valid json.

This is what you want;

import json
import requests
stats = requests.get('http://localhost:9200/_cluster/health').json()
bulk_data = ''
bulk_data += '{"index": {"_index": "cluster", "_type": "cluster"}}\n'
bulk_data += json.dumps(stats) + '\n'
response = requests.post('http://localhost:9200/_bulk', data=bulk_data)

Do you mind me asking what you're doing this for?

Script works Mark

But when I check this index in kibana I got nothing in discover tab. Can we add a current date field along with the above script so that I can select to create index on that date field, or is there any other way to do it.