# How do I set an index-level metadata field using the Elasticsearch-DSL Python client?

**URL:** <https://discuss.elastic.co/t/how-do-i-set-an-index-level-metadata-field-using-the-elasticsearch-dsl-python-client/347142>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [November 14, 2023, 5:08pm UTC](https://discuss.elastic.co/t/how-do-i-set-an-index-level-metadata-field-using-the-elasticsearch-dsl-python-client/347142 "2023-11-14T17:08:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wpm/32/110146_2.png) [@wpm](https://discuss.elastic.co/u/wpm)\
**Post date:** [November 14, 2023, 5:08pm UTC](https://discuss.elastic.co/t/how-do-i-set-an-index-level-metadata-field-using-the-elasticsearch-dsl-python-client/347142/1 "2023-11-14T17:08:59Z")

</div>

I am using the Elasticsearch-DSL Python client to create a new index that stores objects called `IndexedDocument` that are subclasses of `elasticsearch_dsl.Document`.

```auto
index_name = "my-index"
index = elasticsearch_dsl.Index(index_name)
IndexedDocument.init(index_name)
dict(index.get_settings())
{'my-index': {'settings': {'index': {'routing': {'allocation': {'include': {'_tier_preference': 'data_content'}}},
    'number_of_shards': '1',
    'provided_name': 'my-index',
    'creation_date': '1699980696153',
    'number_of_replicas': '1',
    'uuid': '7dT2dzvjT0elqJ_APSUqHQ',
    'version': {'created': '8500003'}}}}}

```

I want to add metadata to the index, a key called "my\_key" with a value "my value" that I can read back from the index.  
Here is what I have tried with Elasticsearch-DSL and the errors I've gotten.

```auto
index.put_settings(my_key="my value")
Traceback (most recent call last):
  File "/Users/bill.mcneill/anaconda3/envs/Lingua/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3548, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-10-b032164530ed>", line 1, in <module>
    index.put_settings(my_key="my value")
  File "/Users/bill.mcneill/anaconda3/envs/Lingua/lib/python3.10/site-packages/elasticsearch_dsl/index.py", line 517, in put_settings
    return self._get_connection(using).indices.put_settings(
  File "/Users/bill.mcneill/anaconda3/envs/Lingua/lib/python3.10/site-packages/elasticsearch/_sync/client/utils.py", line 402, in wrapped
    return api(*args, **kwargs)
TypeError: IndicesClient.put_settings() got an unexpected keyword argument 'my_key'

```

I also tried passing in dictionaries since that seems the most similar to what you do with the REST API.

```auto
index.put_settings({"my_key": "my value"})
Traceback (most recent call last):
  File "/Users/bill.mcneill/anaconda3/envs/Lingua/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3548, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-ef4da1979ed6>", line 1, in <module>
    index.put_settings({"my_key": "my value"})
  File "/Users/bill.mcneill/anaconda3/envs/Lingua/lib/python3.10/site-packages/elasticsearch_dsl/index.py", line 517, in put_settings
    return self._get_connection(using).indices.put_settings(
AttributeError: 'dict' object has no attribute 'indices'

```

I get the same error for `index.put_settings({"index":{"my_key": "my value"}})` and `index.put_settings({"settings":{"index":{"my_key": "my value"}}})`.

How do I do this?

---

<div class="post-metadata">

**Author:** ![Quentin\_Pradet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quentin_pradet/32/94192_2.png) [@Quentin\_Pradet](https://discuss.elastic.co/u/Quentin_Pradet)\
**Post date:** [November 15, 2023, 6:12am UTC](https://discuss.elastic.co/t/how-do-i-set-an-index-level-metadata-field-using-the-elasticsearch-dsl-python-client/347142/2 "2023-11-15T06:12:22Z")

</div>

Hello! I see how that can be confusing.

TL;DR: the correct syntax is `index.put_settings(settings={"my_key": "my value"})`.

Let's see how I came up with this answer. The [elasticsearch\_dsl.Index.put\_settings()](https://elasticsearch-dsl.readthedocs.io/en/latest/api.html#elasticsearch_dsl.Index.put_settings) docs state:

> Any additional keyword arguments will be passed to `Elasticsearch.indices.put_settings` unchanged.

So let's try to make this work using only elasticsearch-py first. Looking at the [elasticsearch.indices.put\_settings()](https://elasticsearch-py.readthedocs.io/en/v8.11.0/api.html#elasticsearch.client.IndicesClient.put_settings), you can see that this API does not accept arbitrary index settings, instead they need to be passed as the `settings` parameter:

```python
from elasticsearch import Elasticsearch

es = Elasticsearch("http://localhost:9200")
es.indices.create(index="test-index")
print(es.indices.get_settings(index="test-index")) # prints number_of_replicas: 1
es.indices.put_settings(index="test-index", body={"number_of_replicas": 2})
print(es.indices.get_settings(index="test-index")) # prints number_of_replicas: 2

```

With that knowledge, we can go back to using elasticsearch-dsl, also using the `settings` keyword argument:

```python
from elasticsearch_dsl import Document, Index, Text, connections

connections.create_connection(hosts="http://localhost:9200")

class IndexedDocument(Document):
    title = Text()

index_name = "my-index"
index = Index(index_name)
IndexedDocument.init(index_name)

print(index.get_settings()) # prints number_of_replicas: 1
index.put_settings(settings={"number_of_replicas": 2})
print(index.get_settings()) # prints number_of_replicas: 1

```

I hope that helps!

(By the way, I was able to infer from your traceback that you're using a recent 8.x version of elasticsearch-py and elasticsearch-dsl, but please make sure to mention it next time.)

---

<div class="post-metadata">

**Author:** ![wpm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wpm/32/110146_2.png) [@wpm](https://discuss.elastic.co/u/wpm)\
**Post date:** [November 15, 2023, 5:34pm UTC](https://discuss.elastic.co/t/how-do-i-set-an-index-level-metadata-field-using-the-elasticsearch-dsl-python-client/347142/3 "2023-11-15T17:34:23Z")

</div>

That works. The call is being made. Thanks.

However, I get an HTTP 400 error with the message "'unknown setting [index.my\_key] please check that any required plugins are installed, or check the breaking changes documentation for removed settings')".

Is there a special place in the index settings where I'm allowed to write arbitrary key/value pairs? Some "metadata" section? (I thought this was possible, but now I can't find documentation about it.)

_Elasticsearch server and elasticsearch-dsl are both version 8.11.0._

---

<div class="post-metadata">

**Author:** ![Quentin\_Pradet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quentin_pradet/32/94192_2.png) [@Quentin\_Pradet](https://discuss.elastic.co/u/Quentin_Pradet)\
**Post date:** [November 16, 2023, 5:05am UTC](https://discuss.elastic.co/t/how-do-i-set-an-index-level-metadata-field-using-the-elasticsearch-dsl-python-client/347142/4 "2023-11-16T05:05:41Z")

</div>

Not sure, to be honest. Maybe you had in mind the [\_meta mapping field](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-meta-field.html)? You can use the put mapping/get mapping APIs like this:

```auto
index.put_mapping(meta={"my_key": "my value"})
assert index.get_mapping()[index_name]["mappings"]["_meta"] == {"my_key": "my value"}

```

Or you can use the [DSL mapping support](https://elasticsearch-dsl.readthedocs.io/en/latest/api.html#mappings) which allows you to update the `_meta` field too.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 14, 2023, 5:05am UTC](https://discuss.elastic.co/t/how-do-i-set-an-index-level-metadata-field-using-the-elasticsearch-dsl-python-client/347142/5 "2023-12-14T05:05:49Z")

</div>

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