0

In ElasticSearch, I have some nested data structured as follows:

{
  "hello_metadata": {
    "stuff": "123",
    "apple": "23423"
  },
  "aksdjj_metadata": {
    "stuff": "ppppppp",
    "apple": "ddddddd"
  }
}

I need to be able to add new fields to the mapping of all objects that match a certain phrase, in this case "_metadata".

For example, if I wanted to add a new field named "cheese" of type "text" to all objects containing the phrase "_metadata", then I would end up with:

{
  "hello_metadata": {
    "stuff": "123",
    "apple": "23423",
    "cheese": "akjsdadad"
  },
  "aksdjj_metadata": {
    "stuff": "ppppppp",
    "apple": "ddddddd",
    "cheese": "L"
  }
}

Is there any way to do this using the [Update Mapping API][1]?

I cannot re-create the Index, I must UPDATE it so I do not believe I can use the Dynamic Templates. [1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html#indices-put-mapping

1 Answer 1

0

You can use update mapping.

  1. Create the new field

PUT demo-index/_mapping

 {
      "properties": {
        "hello_metadata": {
          "type": "nested",
          "properties": {
            "cheese": {
              "type": "text"
            }
          }
        }
      }
    }
  1. Update your document, using query if you want to target specific ones (containing _metadata for example).
Sign up to request clarification or add additional context in comments.

2 Comments

This is not what I meant. I need to use the Update mapping API to update the mappings of all nested fields that contain _metadata, using something like a wildcard or regex. I do not want to update the actual documents.
I am not sure to understand. The mappings is the same for all documents in a same index. You mean update fields of documents containg _metadata?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.