弹性搜索的索引迁移
slingshot的Python项目详细描述
官方elasticsearch python客户端的扩展,提供indices_manager来创建和管理具有读写别名的索引,并且不执行停机迁移。
安装
pip install slingshot
用法
实例化
from weakref import proxy from elasticsearch.client import Elasticsearch from slingshot.indices_manager import IndicesManagerClient es = Elasticsearch() es.indices_manager = IndicesManagerClient(proxy(es))
创建管理索引
es.indices_manager.create('slingshot', body={"settings": {"number_of_shards": 1, "number_of_replicas": 1}})
这将创建具有读和写别名的索引:
- Creates the index “slingshot.{creation_timestamp}”
- Creates a read alias “slingshot”
- Creates a write alias “slingshot.write”
< H3>升级现有索引
Slingshot管理它创建的索引的读和写别名。但是,可以升级不是用弹弓创建的索引。它只需创建一个写别名来处理迁移。
es.indices_manager.manage('existing_index')
管理索引的迁移
es.indices_manager.migrate('slingshot', body={"settings": {"number_of_shards": 5, "number_of_replicas": 1}})
这允许对索引执行更改并迁移文档以利用新映射:
- creates a new index “slingshot.{modification_timestamp}” with a new configuration (e.g. 5 shards instead of 1)
- swaps write alias to the new index
- scans and bulk imports all documents (optionally ignoring types or performing transformations)
- swaps read alias
- deletes original index (can be skipped)
请注意,必须使用Slingshot创建或升级索引(通过创建写别名或使用manage方法)
转换文档
迁移时,转换文档以匹配新映射可能会很有用。
def transform_my_docs(doc): # recompute some fields doc['_source']['discount'] = doc['_source']['price'] / doc['_source']['value'] * 100.0 # drop some fields doc['_source'].pop('useless') # drop documents based on some business rules (assumes the field is first cast to a datetime) if doc['_source]]['expires_at'] < datetime.now(): return None # Don't forget to return the modified document return doc es.indices_manager.migrate('slingshot', body=config_dict_or_string, transform=transform_my_docs)
忽略文档类型
完全忽略某些文档类型也很有用。
es.indices_manager.migrate('slingshot', body=config_dict_or_string, ignore_types=["my_type_1", "my_type_2"])
保持源索引
如果出于任何原因,您希望在迁移后保留原始索引(例如,在发生任何错误时回滚):
es.indices_manager.migrate('slingshot', body=config_dict_or_string, keep_source=True)
警告
Slingshot无法预测需要对新索引的设置、映射、别名等执行什么操作。
因此,在迁移时,body必须包含所有相关配置才能从头创建索引。 这可以包括设置、映射、别名、取暖器或ElasticSearch索引API支持的任何内容。
但是,SLIGHOT管理了写别名和读别名(如果存在)的迁移。
贡献
欢迎大家发表意见。只需创建一个请求或报告一个bug。
更改日志
v0.0.5
- 迁移数据后重新索引Percolators
v0.0.4
- 允许传递create和copy kwargs进行迁移
v0.0.3
- 使用最新版本的elasticsearch py(<;2.0.0)修复兼容性问题
- 在迁移/复制时添加对并行卷的支持
- 迁移/复制时重新索引Percolators
v0.0.2
- 将六项要求改为最低版本而不是精确版本
v0.0.1
- 首字母