弹性搜索的索引迁移

slingshot的Python项目详细描述


https://travis-ci.org/jthi3rry/slingshot.svg?branch=masterhttps://coveralls.io/repos/jthi3rry/slingshot/badge.png?branch=master

官方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管理了写别名和读别名(如果存在)的迁移。

运行测试

获取存储库的副本:

git clone git@github.com:OohlaLabs/slingshot.git .

安装tox

pip install tox

运行测试:

tox

贡献

欢迎大家发表意见。只需创建一个请求或报告一个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

  • 首字母

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
来自控制器的java集合引用应用程序?   java无法插入到swagger 2.0文档中常见错误代码的html链接   循环中的java输入不匹配异常   java Spring批处理集成运行并行作业的远程分区   在Android中使用Gson在自定义类的ArrayList中读取java   C++规范化图像描述符OpenCV-java   java Andmore的Android软件包生成器失败,错误与sun/misc/BASE64Encoder相关   如何用java从多部分数据格式创建接收代码   java用文件填充数组   分页如何在Java代码中实现下一步按钮单击?   我们能用泛型参数动态调用Java接口方法吗?   java从另一个项目中定义的类调用静态方法需要为这两个项目添加库   反射:运行时类型信息是否存储在java中?   编写一个Java程序,允许用户输入自己的公式并进行计算   java Tomcat多个webapps文件夹   java比较两个xml文件并向第一个xml文件添加新标记   反射我能用正则表达式在java中找到类的方法吗?