ElasticSearch scan和scroll功能 python 实现

前言

search 我们经常使用,默认一次返回10条数据,并且可以通过 from 和 size 参数修改返回条数并执行分页操作。但是有时需要返回大量数据,就必须通过scan和scroll实现。两者一起使用来从Elasticsearch里高效地取回巨大数量的结果而不需要付出深分页的代价。
详情参考:https://es.xiaoleilu.com/060_Distributed_Search/20_Scan_and_scroll.html
与上文链接不同的是,本文是关于python实现的介绍和描述。

数据说明

索引hz中一共29999条数据,且内容如下。批量导入数据代码可见:
http://blog.csdn.net/xsdxs/article/details/72849796
这里写图片描述

代码示例

ES客户端代码:

# -*- coding: utf-8 -*-
import elasticsearch

ES_SERVERS = [{
    'host': 'localhost',
    'port': 9200
}]

es_client = elasticsearch.Elasticsearch(
    hosts=ES_SERVERS
)

search接口搜索代码:

# -*- coding: utf-8 -*-
from es_client import es_client


def search(search_offset, search_size):
    es_search_options = set_search_optional()
    es_result = get_search_result(es_search_options, search_offset, search_size)
    final_result = get_result_list(es_result)
    return final_result


def get_result_list(es_result):
    final_result = []
    result_items = es_result['hits']['hits']
    for item in result_items:
        final_result.append(item['_source'])
    return final_result


def get_search_result(es_search_options, search_offset, search_size, index='hz', doc_type='xyd'):
    es_result = es_client.search(
        index=index,
        doc_type=doc_type,
        body=es_search_options,
        from_=search_offset,
        size=search_size
    )
    return es_result


def set_search_optional():
    # 检索选项
    es_search_options = {
        "query": {
            "match_all": {}
        }
    }
    return es_search_options


if __name__ == '__main__':
    final_results = search(0, 1000)
    print len(final_results)

这样一切貌似ok,正常输出1000,但是现在改下需求,想搜索其中20000条数据。

if __name__ == '__main__':
    final_results = search(0, 20000)

输出如下错误:

elasticsearch.exceptions.TransportError: TransportError(500, u’search_phase_execution_exception’, u’Result window is too large, from + size must be less than or equal to: [10000] but was [20000]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level parameter.’)

说明:search接口最多返回1w条数据。所以这里会报错。
不废话,基于scan和scroll实现,直接给代码如下:

# -*- coding: utf-8 -*-
from es_client import es_client
from elasticsearch import helpers


def search():
    es_search_options = set_search_optional()
    es_result = get_search_result(es_search_options)
    final_result = get_result_list(es_result)
    return final_result


def get_result_list(es_result):
    final_result = []
    for item in es_result:
        final_result.append(item['_source'])
    return final_result


def get_search_result(es_search_options, scroll='5m', index='hz', doc_type='xyd', timeout="1m"):
    es_result = helpers.scan(
        client=es_client,
        query=es_search_options,
        scroll=scroll,
        index=index,
        doc_type=doc_type,
        timeout=timeout
    )
    return es_result


def set_search_optional():
    # 检索选项
    es_search_options = {
        "query": {
            "match_all": {}
        }
    }
    return es_search_options


if __name__ == '__main__':
    final_results = search()
    print len(final_results)

输出如下:
这里写图片描述
把29999条数据全部取出来了。

  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
Python通过使用`elasticsearch`库和`mysql-connector-python`库可以实现MySQL数据库同步到Elasticsearch功能。下面是一个300字的回答。 首先,安装所需的库。可以使用以下命令安装`elasticsearch`库和`mysql-connector-python`库: ``` pip install elasticsearch mysql-connector-python ``` 接下来,导入所需的库并连接到MySQL数据库和Elasticsearch: ```python import mysql.connector from elasticsearch import Elasticsearch # 连接到MySQL数据库 conn = mysql.connector.connect( host="localhost", user="root", password="password", database="mydatabase" ) # 连接Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) ``` 然后,执行MySQL查询语句来获取数据,并将其插入到Elasticsearch中: ```python # 创建MySQL游标对象 cursor = conn.cursor() # 执行MySQL查询语句 cursor.execute("SELECT * FROM mytable") # 获取查询结果 results = cursor.fetchall() # 将结果插入到Elasticsearch for row in results: document = { 'id': row[0], # 假设MySQL表中有一个id列 'name': row[1], # 假设MySQL表中有一个name列 # 添加其他需要同步的字段 } es.index(index='myindex', doc_type='mytype', body=document) ``` 最后,关闭MySQL数据库连接Elasticsearch连接: ```python # 关闭MySQL数据库连接 conn.close() # 关闭Elasticsearch连接 es.close() ``` 以上是用Python实现MySQL数据库同步到Elasticsearch的基本步骤。可以根据具体需求对代码进行更改和优化,例如使用配置文件来管理数据库连接信息和Elasticsearch的索引名称等。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值