如何在弹性搜索中启用滚动功能 [英] How to enable scroll functionality in elastic search

查看:80
本文介绍了如何在弹性搜索中启用滚动功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址api,可以通过弹性搜索进行服务。

I have a web url api which serving by the elastic search.


  • 我的网址是 https://data.emp.com/employees

  • 我的索引中有50名员工(数据)

  • 在每个滚动条上将增加7位员工7,14,21..49,50

  • 在每个滚动条上将首先出现7位员工,然后出现14位员工,..49,50名员工

  • URL下方的我的Api一口气是所有50名员工

  • My web urls is https://data.emp.com/employees
  • I have 50 employee(data) in my index
  • On each scroll 7 employee will be adding take 7,14,21..49,50
  • On each scroll 7 employee will appear first then 14 employees, ..49,50 employees
  • My Api below URL is all 50 employees at one shot
    def elastic_search():
        """
         Return full search using match_all
        """
        try:
     
            full_search= es.search(index="employees",scroll = '2m',size = 10,body={ "query": {"match_all": {}}})
            hits_search = full_search['hits']['hits']
            return hits_search 
        except Exception as e:
            logger.exception("Error" + str(e))
            raise

我修改了下面的代码

        sid =  search["_scroll_id"]
        scroll_size = search['hits']['total']
        scroll_size = scroll_size['value']
        # Start scrolling
        while (scroll_size > 0):

            #print("Scrolling...")
            page = es.scroll(scroll_id = sid, scroll = '1m')

            #print("Hits : ",len(page["hits"]["hits"]))
            
            # Update the scroll ID
            sid = page['_scroll_id']
        
            # Get the number of results that we returned in the last scroll
            scroll_size = len(page['hits']['hits'])
            search_text = page['hits']['hits']
            print (search_text)

我的api是抛出 [] ,因为我最后一个 search_text 留为空白。
在日志中,它每组打印7名员工。但是我的Web网址api正在加载,最后显示空白页

My api is throwing [] because my last search_text giving blank. In the log it is printing each set of 7 employees. But My web url api is loading loading and last it showing blank page

请帮助我们进行更新,以返回 hits_search这是在elastic_search函数中

Please help in updating in returning "hits_search" which is in elastic_search function

推荐答案

我猜是elasticsearch 小于或等于10k,则en / elasticsearch / reference / 6.8 / search-request-from-size.html rel = nofollow noreferrer> from and size 将为您解决问题/ strong>。但仍然要使用 scroll API,那么这就是您所需要的

I guess elasticsearch from and size will do the trick for you if you have doc less than ≤ 10k. But still if you want to use the scroll API then this is what you need,

    # declare a filter query dict object
    match_all = {
        "size": 7,
        "query": {
            "match_all": {}
        }
    }

    # make a search() request to get all docs in the index
    resp = client.search(
        index = 'employees',
        body = match_all,
        scroll = '2s' # length of time to keep search context
    )
    
    # process the first 7 documents here from resp
    # iterate over the document hits for each 'scroll'
    for doc in resp['hits']['hits']:
        print ("\n", doc['_id'], doc['_source'])
        doc_count += 1
        print ("DOC COUNT:", doc_count)
    
    # keep track of pass scroll _id
    old_scroll_id = resp['_scroll_id']

    # use a 'while' iterator to loop over document 'hits'
    while len(resp['hits']['hits']):

        # make a request using the Scroll API
        resp = client.scroll(
            scroll_id = old_scroll_id,
            size = 7,
            scroll = '2s' # length of time to keep search context
        )

        # iterate over the document hits for each 'scroll'
        for doc in resp['hits']['hits']:
            print ("\n", doc['_id'], doc['_source'])
            doc_count += 1
            print ("DOC COUNT:", doc_count)

请参阅参考文献: https://kb.objectrocket.com/elasticsearch/how-to-use-python-to-make-scr oll查询要在elasticsearch-index-752中获取所有文档

这篇关于如何在弹性搜索中启用滚动功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆