如何在ElasticSearch中启用滚动功能

fumotvh3  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(375)

我有一个由ElasticSearch服务的web url api。
我的网址是https://data.emp.com/employees
我的索引中有50个员工(数据)
在每个卷轴上,7个员工将添加take 7,14,21..49,50
在每个滚动条上,首先显示7名员工,然后是14名员工,…49,50名员工
我的api下面的网址是所有50名员工在一枪

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名员工。但我的网页url api正在加载,最后显示为空白页
请帮助更新在返回“点击搜索”这是在ElasticSearch功能

pwuypxnk

pwuypxnk1#

我猜elasticsearch从和大小将为你做的把戏,如果你有博士少于≤ 10公里。但如果你想使用scroll api,这就是你需要的,


# 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)

看到了吗ref:httpshttp://kb.objectrocket.com/elasticsearch/how-to-use-python-to-make-scroll-querys-to-get-all-documents-in-an-elasticsearch-index-752

相关问题