gpt4 book ai didi

elasticsearch - 将记录复制到另一个索引而不会发生ID冲突

转载 作者:行者123 更新时间:2023-12-02 22:48:55 26 4
gpt4 key购买 nike

我想将记录从索引复制到另一个。我使用reindex像这样:

POST _reindex
{
"dest": {
"index": "dst"
},
"source": {
"index": "src",
"query": {
"bool": {
"must": [
{
"match": {
"name": "HEIDI"
}
}
]
}
}
}
}

不想也必须复制 _id,因为文档实际上是不同的,并且希望生成新的 _id。我要真正避免的一件事是,如果ID偶尔匹配,则从目标中的源覆盖文档。

如何使用ElasticSearch 5进行设置?
谢谢

最佳答案

有两种方法可以做到这一点:

  • 只需编写python脚本即可从源文件中获取文档,并重新提取到目标文件中。我已经有一些python代码可以做到这一点。也许会对您有帮助。它是:

  • `
    from elasticsearch import helpers
    import elasticsearch

    es = elasticsearch.Elasticsearch(
    hosts=[{'host': '<your-es-host-name>'}],
    )

    results = helpers.scan(
    es,
    query={"query": {
    "bool": {
    "must": [
    {
    "match": {
    "name": "HEIDI"
    }
    }
    ]
    }
    }},
    scroll="20m",
    index="<your-source-index-name>",
    doc_type="<your-source-index-type>"
    )

    actions = []
    for item in results:
    action = {
    "_index": "<your-dest-index-name>",
    "_type": "<your-dest-index-type>",
    "_source": item["_source"]
    }
    actions.append(action)

    helpers.bulk(es, actions)

    `
  • 您可以使用reindex API并使用以下方法避免ID冲突:
  • POST _reindex
    {
    "conflicts": "proceed",
    "dest": {
    "index": "dst",
    "op_type": "create"
    },
    "source": {
    "index": "src",
    "query": {
    "bool": {
    "must": [
    {
    "match": {
    "name": "HEIDI"
    }
    }
    ]
    }
    }
    }
    }

    但是请记住,使用#2时,与目标索引冲突的ID不会被重新索引。

    关于elasticsearch - 将记录复制到另一个索引而不会发生ID冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46574747/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com