2014-09-04 109 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

滾動是昂貴的,但如果你堅持,從查詢中刪除「掃描」搜索類型。掃描時禁用排序當您滾動。