Elasticsearch Mapping - 重命名现有字段

时间:2017-03-30 14:13:22

标签: elasticsearch elasticsearch-mapping bigdata

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

e.g。从fieldCamelcase到fieldCamelCase

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

4 个答案:

答案 0 :(得分:6)

您可以通过创建Ingest管道来完成此操作,该管道包含与Rename Processor结合的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在评论中提到的内容:)

答案 1 :(得分:3)

使用_update_by_query API更新弹性版本> 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\");"
  }
}

答案 2 :(得分:0)

首先,您必须了解不可变段(您可以在Internet上轻松阅读)的 elasticsearch lucene 数据存储方式。

因此,任何解决方案都将删除/创建文档并更改映射,或者创建新索引以及创建新映射。

最简单的方法是使用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\");"
    }
}

答案 3 :(得分:0)

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

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