2014-09-04 76 views
0

文件数量巨大。当我想从elasticsearch索引检索的文件数量巨大,我总是用从elasticsearch(http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/scan-scroll.html)扫描和滚动技术如下:排序在Elasticsearch

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文档,检索到的文档将不会被排序,因此结果不是我想要的。

我的问题: 如何在Elasticsearch中对大量文档进行排序?

谢谢:)通过排序列表进行遍历时,

回答

1

滚动是昂贵的,但如果你坚持,从查询中删除“扫描”搜索类型。扫描时禁用排序当您滚动。