2015-02-23 77 views
2

我有一個目錄充滿JSON文件,我想要在彈性搜索中編入索引。我已經研究過pyelastic,但我對Python和彈性搜索都很陌生。 我已經粘貼了下面的一些代碼。如何將多個json文件添加到elasticsearch

from pyelasticsearch import ElasticSearch 

# ElasticSearch settings 
ES_CLUSTER = 'http://localhost:9200/' 
ES_INDEX = 'test' 
ES_TYPE = 'doc' 
es = ElasticSearch(ES_CLUSTER) 

es.bulk_index(ES_INDEX, ES_TYPE, ???) 

回答

1

該函數稱爲批量批量索引文檔。

將它們加載到一個數組中,然後使用批量索引將起作用。如果文檔數量非常大(> 1000),則可以選擇一個索引它們:

from pyelasticsearch import ElasticSearch 
import json 
import os 

es = ElasticSearch(ES_CLUSTER) 

json_docs = [] 
for filename in os.listdir(os.getcwd()): 
    if filename.endswith('.json'): 
     with open(filename) as open_file: 
      json_docs.append(json.load(open_file)) 

es.bulk(ES_INDEX, ES_TYPE, json_docs) 
相關問題