当我想从elasticsearch索引中检索大量文档时,我总是使用elasticsearch(http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/scan-scroll.html)中的扫描和滚动技术,如下所示:
conn = Elasticsearch( hosts = HOSTS )
the_query = { 'query': { 'match_all': { } }, 'sort': { 'created_at': { 'order': 'asc' } } } # would like sort the documents according to the 'created_at' date
scanResp = conn.search( index=TARGET_INDEX, doc_type=TARGET_DOC_TYPE, body=the_query, search_type='scan', scroll='10m' )
scrollId = scanResp['_scroll_id']
doc_num = 1
response = conn.scroll( scroll_id = scrollId, scroll='10m' )
while ( len( response['hits']['hits'] ) > 0 ):
for item in response['hits']['hits']:
print '\tDocument ' + str(doc_num) + ' of ' + str( response['hits']['total'] )
doc_num += 1
# ====================
# Process the item
# ====================
the_doc = item['_source']
# end for item
scrollId = response['_scroll_id']
if doc_num >= response['hits']['total']:
break
response = conn.scroll( scroll_id = scrollId, scroll='10m' )
# end of while
但是,正如弹性搜索文档所述,检索到的文档将不会被排序,因此结果不是我想要的。
我的问题: 如何在Elasticsearch中对大量文档进行排序?
谢谢:)
答案 0 :(得分:1)
在遍历排序列表时滚动很昂贵,但是如果你坚持,请删除“扫描”'您查询中的search_type。扫描在滚动时禁用排序。