How to create a default index pattern via python api

Hi,
I saw How to create a default index pattern via kibana api that used Kibana API.
I wanted to use python, but didn't know what uri should I use.

header = {
    "kbn-xsrf": "true",
    'Content-Type': 'application/json'
}
uri = ?
query = json.dumps({"attributes":{"defaultIndex": "myindex"}})
r = requests.post(uri, headers=header, data=query).json()

I'm using both Elastic and Kibana locally on Windows.
Thanks

The API for creating an index pattern is described here.

So it would be something like https://my-kibana:port/api/saved_objects/index-pattern/my-pattern.

Let me know if this helps.

1 Like

all the endpoints that i presented in my post can easily be used directly with urlllib.

1 Like

It didn't.
(Just to clarify: I know how to create an index pattern; I want to set an index pattern as the default).

The header and query stayed same, just changing the uri:

HEADERS = {
    "kbn-xsrf": "true",
    'Content-Type': 'application/json'
}
query = json.dumps({'attributes':{'defaultIndex': 'my_index_pattern'}})
r = requests.post(uri, headers=HEADERS, data=query).json()
uri = "http://localhost:5601/s/api/saved_objects/index-pattern/my_index_pattern"
Error: {'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}
uri = "http://localhost:9200/s/api/saved_objects/index-pattern/my_index_pattern"
Error: {'error': 'no handler found for uri [/s/api/saved_objects/index-pattern/stock_tweets] and method [POST]'}
uri = "http://localhost:5601/s/api/saved_objects/config/7.8.0"
Error: {'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}
uri = "http://localhost:9200/s/api/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/s/api/saved_objects/config/7.8.0] and method [POST]'}
uri = "http://localhost:5601/api/saved_objects/config/7.8.0"
Error: {'statusCode': 409, 'error': 'Conflict', 'message': '[config:7.8.0]: version conflict, document already exists (current version [2]): [version_conflict_engine_exception] [config:7.8.0]: version conflict, document already exists (current version [2]), with { index_uuid="O0QpGH7BSIqBdpyq9r3vCA" & shard="0" & index=".kibana_2" }'}
uri = "http://localhost:9200/api/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/api/saved_objects/config/7.8.0] and method [POST]'}
uri = "http://localhost:5601/.kibana/api/saved_objects/config/7.8.0"
Error: {'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}
uri = "http://localhost:9200/.kibana/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/.kibana/saved_objects/config/7.8.0] and method [POST]'}
uri = "http://localhost:5601/.kibana/saved_objects/config/7.8.0"
Error: {'statusCode': 404, 'error': 'Not Found', 'message': 'Not Found'}
uri = "http://localhost:9200/.kibana/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/.kibana/saved_objects/config/7.8.0] and method [POST]'}
uri = "http://localhost:9200/.kibana/config/7.8.0"
Error: {'error': {'root_cause': [{'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [.kibana_2] as the final mapping would have more than 1 type: [_doc, config]'}], 'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [.kibana_2] as the final mapping would have more than 1 type: [_doc, config]'}, 'status': 400}
uri = "http://localhost:9200/.kibana/api/saved_objects/config/7.8.0"
Error: {'error': 'no handler found for uri [/.kibana/api/saved_objects/config/7.8.0] and method [POST]'}

Will u explain how should I use

/s/<some space>/api/saved_objects/config/7.8.0

in Python? (Similar to those I've written)

if you re-read what i had written in the post you would see that you need to call

http://kibana:5601/s/<some space>/api/saved_objects/index-pattern/dummy_index_pattern
http://kibana:5601/s/<some space>/api/saved_objects/index-pattern/7.8.0

you do not need to call an elasticsearch endpoint.

regardless of the language you use to write an API call the above URL is what you should be using.
<some space> is whatever space you have created. assuming you have created a space. if not do so or remove /s/<some space> from the URL. it all depends on what you are trying to do. In my case, i made a default index for specific spaces. As well, i am using 7.8.0 i do not know what version of kibana you have.

I'm using 7.8.0 too.

I tried

HEADERS = {
    "kbn-xsrf": "true",
    'Content-Type': 'application/json'
}
query = json.dumps({'attributes':{'defaultIndex': 'my_index_pattern'}})
uri = "http://localhost:5601/api/saved_objects/index-pattern/7.8.0"
r = requests.post(uri, headers=HEADERS, data=query).json()

Error: {'statusCode': 400, 'error': 'Bad Request', 'message': 'mapping set to strict, dynamic introduction of [defaultIndex] within [index-pattern] is not allowed: [strict_dynamic_mapping_exception] mapping set to strict, dynamic introduction of [defaultIndex] within [index-pattern] is not allowed'}

But I have not set the dynamic field to false or strict.
What should I do?

Btw, you said

/s/<some space>/api/saved_objects/config/7.8.0

not

/s/<some space>/api/saved_objects/index-pattern/7.8.0

(I don't know if the two are the same or not)

I noticed something odd with the version when I exported my_index_pattern:

{"attributes": ... ,"migrationVersion":{"index-pattern":"7.6.0"},"references":[],"type":"index-pattern","version":"WzIxOTYsMV0="}

But I was using 7.8.0 all along! Is that the problem?!

sorry you are right try without index-pattern and i think you have version 7,6,0. please confirm that and use the correct version with the call.

Elastic is 7.8.0, couldn't find anything about Kibana (I downloaded them at the same time around 3 weeks ago). The only thing I could find was " Welcome to Stack Management 7.8.0". Ok?

And about my question,
I tried this:

HEADERS = {
    "kbn-xsrf": "true",
    'Content-Type': 'application/json'
}
query = json.dumps({'attributes':{'defaultIndex': 'my_index_pattern'}})
uri = "http://localhost:9200/.kibana_4/saved_objects/7.8.0"
r = requests.post(uri, headers=HEADERS, data=query).json()

And the response was:

{'_index': '.kibana_4', '_type': 'saved_objects', '_id': '7.8.0', '_version': 3, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1}

But it's not the default one yet.
What am I doing wrong?

I was getting this error when I used .kibana, so I switched to .kibana_4:

{'error': {'root_cause': [{'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [.kibana_3] as the final mapping would have more than 1 type: [_doc, saved_objects]'}], 'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [.kibana_3] as the final mapping would have more than 1 type: [_doc, saved_objects]'}, 'status': 400}

Why do I have different .kibana s?! (.kibana, .kibana_1 to 4)

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