Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

change field name of an index in elasticsearch

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...