Elasticsearch 映射 - 重命名现有字段

Elasticsearch Mapping - Rename existing field

我是否可以重命名现有 elasticsearch 映射中的元素而无需添加新元素? 如果是这样,为了避免破坏现有映射,最好的方法是什么?

例如从 fieldCamelCase 到 fieldCamelCase

{
    "myType": {
        "properties": {
            "timestamp": {
                "type": "date",
                "format": "date_optional_time"
            },
            "fieldCamelcase": {
                "type": "string",
                "index": "not_analyzed"
            },
            "field_test": {
                "type": "double"
            }
        }
    }
}

您可以通过创建一个 Ingest pipeline, that contains a Rename Processor in combination with the Reindex API 来做到这一点。

PUT _ingest/pipeline/my_rename_pipeline
{
  "description" : "describe pipeline",
  "processors" : [
    {
      "rename": {
        "field": "fieldCamelcase",
        "target_field": "fieldCamelCase"
      }
    }
  ]
}

POST _reindex
{
  "source": {
    "index": "source"
  },
  "dest": {
    "index": "dest",
    "pipeline": "my_rename_pipeline"
  }
} 

请注意,您需要成为 运行 Elasticsearch 5.x 才能使用摄取。如果您 运行 < 5.x 那么您将不得不接受@Val 在他的评论中提到的内容 :)

首先,你必须了解elasticsearchlucene如何通过不可变段存储数据(你可以在Internet上轻松阅读).

因此,任何解决方案都会 remove/create 记录和更改映射或创建新索引,因此也会创建新映射。

最简单的方法是使用 update by query API: https://www.elastic.co/guide/en/elasticsearch/reference/2.4/docs-update-by-query.html

POST /XXXX/_update_by_query

{
    "query": { 
    "missing": {
      "field": "fieldCamelCase"
    }
  },
    "script" : {
        "inline": "ctx._source.fieldCamelCase = ctx._source.fieldCamelcase; ctx._source.remove(\"fieldCamelcase\");"
    }
}

使用 _update_by_query API:

更新 ES 中的字段名称(版本>5,缺失已删除)

示例:

POST http://localhost:9200/INDEX_NAME/_update_by_query
{
  "query": { 
    "bool": {
        "must_not": {
            "exists": {
                "field": "NEW_FIELD_NAME"
            }
        }
    }
  },
  "script" : {
    "inline": "ctx._source.NEW_FIELD_NAME = ctx._source.OLD_FIELD_NAME; ctx._source.remove(\"OLD_FIELD_NAME\");"
  }
}

从 ES 6.4 开始,您可以使用“Field Aliases”,它允许您使用接近 0 的工作或资源寻找的功能。

请注意,别名只能用于搜索 - 不能用于索引新文档。