How to change the mappings of an index in python itself?

I want to change the type of text to keyword of a csv file that I'm uploading in Elasticsearch using the Elastic Search client in Python. But I'm getting this error:
elasticsearch.BadRequestError: BadRequestError(400, 'illegal_argument_exception', 'mapper [source] cannot be changed from type [text] to [keyword]').
Can anyone help me with this?

It is not possible to update the mapping of an existing field. If the mapping is set to the wrong type, re-creating the index with updated mapping and re-indexing is the only option available.

Except for supported [mapping parameters] you can’t change the mapping or field type of an existing field. Changing an existing field could invalidate data that’s already indexed.

I'm not sure on how you mapping is.

You can try this option. Modify your mapping to add an keyword sub-field to your existing field and try.

PUT index/_mapping
  "properties": {
    "your_text_field": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword"

Even in that case, if you modify the mapping, you will have to reindex the existing documents...

Unless you define a runtime field. See Runtime fields | Elasticsearch Guide [8.5] | Elastic

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