简体   繁体   English

在 Elasticsearch 中将索引的字段类型更改为新索引

[英]Changing field type of an index to new index in Elasticsearch

lets say that I have index that its mapping look like this:假设我有索引,它的映射如下所示:

curl -XPUT 'http://localhost:9200/oldindex/_mapping/book' -d '
   {
    "book" : {
        "properties" : {
            "title" : {"type" : "text"},
            "words" : {"type" : "text"},
            "pages": {"type": "int"}
        }
     }
   }'

I want to create a new index from the old index, but now I want the "words" type field to be "keyword" instead of "text":我想从旧索引创建一个新索引,但现在我希望“words”类型字段是“keyword”而不是“text”:

curl -XPUT 'http://localhost:9200/oldindex/_mapping/book' -d '
       {
        "book" : {
            "properties" : {
                "title" : {"type" : "text"},
                "words" : {"type" : "keyword"},
                "pages": {"type": "int"}
            }
         }
       }'

How can I do that?我该怎么做? can I use "Reindex API" or there is a better solution?我可以使用“Reindex API”还是有更好的解决方案?

Assuming you don't want another field like wordsKeyword on the index, the Reindex API would probably be best.假设您不希望索引上有其他字段,如wordsKeyword ,那么wordsKeyword API 可能是最好的。 Move your data to a temp index, nuke the index and recreate it (with an updated mapping), then reindex the temp index data back into the new index.将您的数据移动到临时索引,取消索引并重新创建它(使用更新的映射),然后将临时索引数据重新索引到新索引中。
Step by step this is:一步一步是:

  1. Create new index with new mapping.使用新映射创建新索引。
  2. Copy the data from the old index to the new index via _reindex .通过_reindex将数据从旧索引复制到新索引。
  3. Delete the old index.删除旧索引。
  4. Recreate the old index with the new mapping.使用新映射重新创建旧索引。
  5. Copy the data from the new index to the old index via _reindex.通过 _reindex 将数据从新索引复制到旧索引。
  6. Delete the new index.删除新索引。 -- The "old index" (well technically a new one with the same name) will now have the updated mapping and data. -- “旧索引”(技术上是同名的新索引)现在将具有更新的映射和数据。 Total of five API calls (two _reindexes to move the data over to new mapping, then to move it back)总共五个 API 调用(两个 _reindexes 将数据移至新映射,然后将其移回)

If you don't mind having another field and don't want to reindex, you could look into using the _update_by_query API to literally copy over the value into the newly mapped keyword field via a script, thereby deprecating the text one.如果您不介意拥有另一个字段并且不想重新索引,您可以考虑使用_update_by_query API 通过脚本将值逐字复制到新映射的keyword字段中,从而弃用text字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM