我对ElasticSearch的查询的大小问题

2024-04-25 12:02:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python向我的ES发出请求,但是我只能得到10000个数据。我还需要更多的钱(几十万)。你知道吗

我修改了“size”变量,但不能超过10000

res_cpe = es.search(index=cpe_index, doc_type="entries", body = {
'size' : 10000,
'query': {
    'match_all' : {}
}
})

我想在我的“res\u cpe”变量中有所有的条目


Tags: 数据searchsizeindexdocestypematch
1条回答
网友
1楼 · 发布于 2024-04-25 12:02:51

您应该尝试使用ScrollAPI,它可以帮助您检索大量的结果(甚至是所有的结果,就像您的案例一样)。你知道吗

此功能类似于传统数据库中的游标。你知道吗

您只需在Python客户机中将scroll param添加到您的请求中。最简单可行的例子如下所示:

page = es.search(

index = 'yourIndex',

doc_type = 'yourType',

scroll = '2m',

search_type = 'query_then_fetch',

size = 1000,

body = {

//Your query's body

})

sid = page['_scroll_id']

scroll_size = page['hits']['total']

//Start scrolling

while (scroll_size > 0):

print "Scrolling..."

page = es.scroll(scroll_id = sid, scroll = '2m')

//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'])

print "scroll size: " + str(scroll_size)

//Do something with the obtained page

示例取自此处-https://gist.github.com/drorata/146ce50807d16fd4a6aa

Python客户端文档引用-https://elasticsearch-py.readthedocs.io/en/master/api.html

相关问题 更多 >