Elastic Search reindex API - 如何保留目标索引映射?

Paw*_*bin 4 elasticsearch reindex

my-index-0为 ES 索引,别名为my-index

它具有以下映射:

{
    "my-index-0": {
        "aliases": {
            "my-index": {}
        },
        "mappings": {
            "doc": {
                "properties": {
                    "foo": {
                        "properties": {
                            "fizz": {
                                "type": "keyword"
                            },
                            "baz": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

假设我想baz从 中删除该字段foo。我正在使用以下步骤:

  1. my-index-1使用更新的映射(foo.baz已删除)创建新索引PUT /my-index-1
{
    "mappings": {
        "doc": {
            "properties": {
                "foo": {
                    "properties": {
                        "fizz": {
                            "type": "keyword"
                        },
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 将数据重新索引my-index-0my-index-1使用POST /_reindex
{
  "source": {
    "index": "my-index-0"
  },
  "dest": {
    "index": "my-index-1"
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 使用以下命令将别名移动my-index到索引my-index-1POST /_aliases
{
    "actions": [
        {"remove": {"index": "my-index-0", "alias": "my-index"}},
        {"add": {"index": "my-index-1", "alias": "my-index"}},
    ]
}
Run Code Online (Sandbox Code Playgroud)

预期结果

新索引中的数据不具有该foo.baz属性。

实际结果

创建时my-index-1,其映射不包含该foo.baz字段,但是,重新索引后,my-index-1的映射将更改为旧索引的映射。

注意:_source可用于简单的字段删除

如果要删除某个字段,例如bar从下面的映射中删除

{
    "mappings": {
        "foo": {
            "type": "text"
        },
        "bar": {
            "type": "text"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在重新索引 API 的请求中提供_source不带字段的参数就足够了:bar

{
  "source": {
    "index": "my-index-0",
    "_source": ["foo"]
  },
  "dest": {
    "index": "my-index-1"
  }
}
Run Code Online (Sandbox Code Playgroud)

如何通过嵌套结构实现相同的效果?

Sae*_*ehi 10

当您使用reindexES 时,尝试将所有数据从源索引复制到目标索引。如果您想让索引不被修改,您需要将此行添加到映射中:

"dynamic" : "strict"
Run Code Online (Sandbox Code Playgroud)

现在,如果您想要reindex数据,您将收到错误,"strict_dynamic_mapping_exception"因为"mapping set to strict, dynamic introduction of [baz] within [foo] is not allowed". 所以你需要像reindex这样删除这个字段:

POST _reindex
{
  "source": {
    "index": "my-index-0"
  },
  "dest": {
    "index": "my-index-1"
  },
  "script": {
    "source": "ctx._source.remove(\"foo.baz\")"
  }
}
Run Code Online (Sandbox Code Playgroud)

注意:添加"dynamic" : "strict"是可选的,可以防止您的索引被修改。如果您只是编辑查询,它将为您工作reindex