ElasticSearch:如何将字段移动到现有数据的不同级别?

ElasticSearch: How to move a field to a different level with existing data?

假设我有:

PUT /test/_doc/1
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch",
    "data": {
        "modified_date": "2018-11-15T14:12:12",
        "password": "abcpassword"
    }
}

然后我得到以下映射:

GET /test/_mapping/_doc
{
    "test": {
        "mappings": {
            "_doc": {
                "properties": {
                    "data": {
                        "properties": {
                            "modfied_date": {
                                "type": "date"
                            },
                            "password": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            }
                        }
                    },
                    "message": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "post_date": {
                        "type": "date"
                    },
                    "user": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        }
    }
}

如何重新索引映射以使 modified_date 达到与 user 相同的级别并且不丢失任何数据?

{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch",
    "modified_date": "2018-11-15T14:12:12"
    "data": {
        "password": "abcpassword"
    }
}

我建议使用 Ingest Node and Pipelines。您可以在分别添加的链接中阅读有关它们的信息。

基本上我要做的是,构建一个 pipeline 并在 indexingreindexing 过程中提及它,这样您的文档将通过 在文档实际存储在目标索引中之前,按照管道 中定义的方式进行处理。

我为您的用例创建了以下管道。它所做的是,根据需要添加一个具有值的新字段 modified_date 并删除字段 data.modified_date。如果其中未提及任何字段,则不会对其进行修改,并将按原样摄取到目标索引中。

Create/Add管道

PUT _ingest/pipeline/mydatepipeline
{
  "description" : "modified date pipeline",
  "processors" : [
    {
      "set" : {
        "field": "modified_date",
        "value": "{{data.modified_date}}"
      }
    },
    {
      "remove": {
        "field": "data.modified_date"
      }
    }
  ]
}

创建上述管道后,使用它来执行重建索引。

用法 1:在重新索引到新索引期间

POST _reindex
{
  "source": {
    "index": "test"
  },
  "dest": {
    "index": "test_dest",
    "pipeline": "mydatepipeline"
  }
}

文档将按照您的预期进行转换,并将在 test_dest 索引中编制索引。请注意,您需要根据您的要求使用映射详细信息显式创建 test_dest

用法 2:在索引之前的批量操作期间使用管道

您可以在批量操作期间使用它,如下所示:

POST _bulk?pipeline=mydatepipeline

用法 3:在索引期间对单个文档使用管道

PUT test/_doc/1?pipeline=mydatepipeline
{
  "user" : "kimchy",
  "post_date" : "2009-11-15T14:12:12",
  "message" : "trying out Elasticsearch",
  "data": {
      "modified_date": "2018-11-15T14:12:12",
      "password": "abcpassword"
  }
}

对于这两个 Usage 2 and 3,您需要确保相应地创建您的映射。

希望对您有所帮助!