我想将记录从索引复制到另一个。我正在使用 reindex
这样:
POST _reindex
{
"dest": {
"index": "dst"
},
"source": {
"index": "src",
"query": {
"bool": {
"must": [
{
"match": {
"name": "HEIDI"
}
}
]
}
}
}
}
也不需要复制_id
,因为文档实际上是不同的,并且希望生成新的_id
s。我想真正避免的一件事是,如果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不会被重新索引。
添加另一个使用POST _reindex
的答案,因为它可能会在以后帮助某人。因此,诀窍是添加将将_id
重置为NULL的脚本。这对我有帮助:
POST _reindex
{
"source": {
"index": "src"
},
"script": {
"source": "ctx._id = null"
},
"dest": {
"index": "dst"
}
}