elasticsearch,Python,elasticsearch" /> elasticsearch,Python,elasticsearch" />

如何使用python在Elasticsearch中将修改后的索引中的内容复制到另一个索引中

如何使用python在Elasticsearch中将修改后的索引中的内容复制到另一个索引中,python,elasticsearch,Python,elasticsearch,经过一些修改后,我需要将my_index中的内容复制到另一个索引中。怎么做 下面是要放入索引的示例数据和代码 test = [{'id':1,'name': 'A', 'subject': ['Maths', 'Accounting'], 'type':'Contract', 'Location':'NY'}, { 'id':2, 'name': 'AB', 'subject': ['Physics', 'Engineering'], 'type':'Perma

经过一些修改后,我需要将
my_index
中的内容复制到另一个索引中。怎么做

下面是要放入索引的示例数据和代码

test = [{'id':1,'name': 'A', 'subject': ['Maths', 'Accounting'],
        'type':'Contract', 'Location':'NY'},
    { 'id':2,  'name': 'AB', 'subject': ['Physics', 'Engineering'],
    'type':'Permanent','Location':'NY'},
    {'id':3,   'name': 'ABC',   'subject': ['Maths', 'Engineering'],
    'type':'Permanent','Location':'NY'}]

from elasticsearch import Elasticsearch
es = Elasticsearch()
es.indices.create(index='myindex', ignore=400)
for e in test:
        es.index(index="myindex", body=e, id=e['id'])
myindex
中,我需要提取
id
name
并复制到另一个索引
myu index\u 1
如果“myindex”中的文档数不太高,您可以使用搜索API获取所有数据,提取所需数据并对其进行索引。为此,您可以使用如下代码:

res = es.search(index="myindex", body={"query":{"match_all":{}}})
for item in res.get("hits").get("hits"):
    es.index(index="my_index_1", body={"id":item.get("_id"), "name": item.get("_source").get("name")})
如果数据数量较高(通常小于10000),则可以使用上限代码,但最好使用以下限制和偏移:

limit = 30
total_count = es.count(index="my_index", body={"query":{"match_all":{}}}).get("count")
offset = 0
while offset < total_count:
    res = es.search(index="myindex", body={"query":{"match_all":{}}}, size=limit, from_=offset)
    offset+=limit
    for item in res.get("hits").get("hits"):
        es.index(index="my_index_1", body={"id":item.get("_id"), "name": item.get("_source").get("name")})
如果“myindex”中的文档数量不是很高,您可以使用搜索API获取所有数据,提取所需的数据并为它们编制索引。为此,您可以使用如下代码:

res = es.search(index="myindex", body={"query":{"match_all":{}}})
for item in res.get("hits").get("hits"):
    es.index(index="my_index_1", body={"id":item.get("_id"), "name": item.get("_source").get("name")})
如果数据数量较高(通常小于10000),则可以使用上限代码,但最好使用以下限制和偏移:

limit = 30
total_count = es.count(index="my_index", body={"query":{"match_all":{}}}).get("count")
offset = 0
while offset < total_count:
    res = es.search(index="myindex", body={"query":{"match_all":{}}}, size=limit, from_=offset)
    offset+=limit
    for item in res.get("hits").get("hits"):
        es.index(index="my_index_1", body={"id":item.get("_id"), "name": item.get("_source").get("name")})