2

I want to ask that, how to change field names in elasticsearch index. I mean,

"_source": {
"name_of_field": "lorem"
}

change "name_of_field" to "new_name"

2
  • 1
    the both answer solves the question but the exact match one by escoder. I'm not able to select both so I ve selected escoder's answer. But i ve upvoted yours. Thanks for answering. Commented Jan 8, 2021 at 6:22
  • sure no worries :) Commented Jan 8, 2021 at 8:31

2 Answers 2

2

As mentioned by @Elasticsearch Ninja you can use the alias, you can also use update by query API

Adding a working example

Index Mapping:

{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      }
    }
  }
}

Index Data:

{
    "name":"John"
}

Update by Query API

POST /_update_by_query

{
  "query": { 
    "bool": {
        "must_not": {
            "exists": {
                "field": "title"
            }
        }
    }
  },
  "script" : {
    "inline": "ctx._source.title = ctx._source.name; ctx._source.remove(\"name\");"
  }
}

By the above query name field name will be changed to title

Sign up to request clarification or add additional context in comments.

2 Comments

@rknd did you get a chance to go through the answer, looking forward to get feedback from you 🙂
Thanks for answer. This solves the question
2

Refer official ES documentation on how to rename a field, you need to use alias for doing it.

From the same docs

Renaming a field would invalidate data already indexed under the old field name. Instead, add an alias field to create an alternate field name.

Comments

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.