简体   繁体   English

重命名Elasticsearch索引中的doctype

[英]Rename doctype in elasticsearch index

I used elasticsearch-py to move millions of records represented by a Django model from PostgreSQL to Elasticsearch. 我使用elasticsearch-py将Django模型表示的数百万条记录从PostgreSQL移动到Elasticsearch。 I used the name of the model for doctype (which was in CamelCase). 我将模型的名称用于doctype(在CamelCase中)。

I then switched to Elasticsearch DSL and noticed that by default it creates doctypes with lowercase names with underscores (snake_case). 然后,我切换到Elasticsearch DSL,并注意到默认情况下它会创建带下划线(snake_case)的小写名称的doctype。

I don't want to redefine doc_type in my document meta, so I am to rename it in Elasticsearch. 我不想在我的文档元中重新定义doc_type ,所以我要在Elasticsearch中重命名它。 What would be the fastest way to do this? 最快的方法是什么?

My own solution using elasticsearch_dsl : 我自己的使用elasticsearch_dsl的解决方案:

from elasticsearch.helpers import bulk
from elasticsearch_dsl import Search
from elasticsearch_dsl.connections import connections


connection = connections.get_connection()    
s = Search(index=index, doc_type=old_name)

actions = (dict(
    _index=hit.meta.index, _type=new_name, 
    _id=hit.meta.id, _source=hit.to_dict()
) for hit in s.scan())
bulk(connection, actions, request_timeout=300)
s.params(request_timeout=600).delete()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM