13

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

例如从 fieldCamelcase 到 fieldCamelCase

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

4 回答 4

20

您可以通过创建一个包含Rename ProcessorReindex API的Ingest管道来做到这一点。

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 在他的评论中提到的内容:)

于 2017-03-31T14:01:48.843 回答
8

使用 _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\");"
  }
}
于 2020-07-31T13:31:19.697 回答
5

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

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

最简单的方法是使用update by queryAPI: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\");"
    }
}
于 2019-07-24T07:53:20.140 回答
0

从 ES 6.4 开始,您可以使用“字段别名”,它允许您使用接近 0 的工作或资源来寻找您正在寻找的功能。

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

于 2020-11-23T18:15:23.037 回答