elasticsearch - Elasticsearch:在指定时间范围内滚动

标签 elasticsearch elasticsearch-py

我在Elasticsearch中有一些数据。如图所示

enter image description here

我使用下面的链接示例进行滚动

https://gist.github.com/drorata/146ce50807d16fd4a6aa

page = es.search(
    index = INDEX_NAME,
    scroll = '1m',
    size = 1000,
    body={"query": {"match_all": {}}})
    sid = page['_scroll_id']
    scroll_size = page['hits']['total']

    # Start scrolling

    print( "Scrolling...")
    while (scroll_size > 0):


        print("Page: ",count)
        page = es.scroll(scroll_id = sid, scroll = '10m')
        # Update the scroll ID
        sid = page['_scroll_id']

        for hit in page['hits']['hits']:
            #some code processing here

目前,我的要求是我要滚动但要指定开始时间戳和结束时间戳
需要有关如何使用滚动条进行操作的帮助。

最佳答案

示例代码。时间范围应在es查询中。另外,您应该处理第一个查询结果。

es_query_dict = {"query": {"range": {"timestamp":{
    "gte":"2018-08-00T00:00:00Z", "lte":"2018-08-17T00:00:00Z"}}}}


def get_es_logs():
    es_client = Elasticsearch([source_es_ip], port=9200, timeout=300)

    total_docs = 0
    page = es_client.search(scroll=scroll_time,
                            size=scroll_size,
                            body=json.dumps(es_query_dict))
    while True:
        sid = page['_scroll_id']
        details = page["hits"]["hits"]
        doc_count = len(details)
        if len(details) > 0:
            total_docs += doc_count
            print("scroll size: " + str(doc_count))
            print("start bulk index docs")
            # index_bulk(details)
            print("end success")
        else:
            break
        page = es_client.scroll(scroll_id=sid, scroll=scroll_time)

    print("total docs: " + str(total_docs))

关于elasticsearch - Elasticsearch:在指定时间范围内滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51888981/

相关文章:

python - 字段中的大写字母导致查询在Elasticsearch中失败

python - Python elasticsearch仅返回具有特定字段的条目

c# - 在 NEST 中搜索没有类型特定类的特定查询(TermsQuery)

java - 如果存在则在 field1 上应用搜索并忽略 field2 否则在 field2 上搜索 Elasticsearch

ElasticSearch 使用经纬度查找文档完全匹配

python - 在 Python 中将 multi_match 与 Elasticsearch 批量扫描结合使用

multithreading - 具有多线程的ElasticSearch Scroll API

search - Elasticsearch :URI搜索不准确

json - 弹性查询嵌套查询

elasticsearch - 在Elasticsearch查询中转义特殊字符