簡體   English   中英

如何使用彈性搜索和 python 有效地索引 json 文件?

[英]How to efficiently index a json file using elastic search and python?

我是 Elasticsearch 的新手。 我正在嘗試索引包含 100,000 多個對象的 json 文件。 我的 json 文件的格式是:

    [{"ingredients": [{"text": "Butter"}, {"text": "Strawberries"}, {"text": "Granola"}], 
    "url": "http://tastykitchen.com/recipes/breakfastbrunch/yogurt-parfaits/", 
    "title": "Yogurt Parfaits", 
    "id": "000095fc1d", 
    "instructions": [{"text": "Layer all ingredients in a serving dish."}]},
     {"ingredients":
     .....]

這是列表的形式。 我現在使用 write 來索引文件的 python 代碼是:

from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost','port': 9200}])
f = open('data.json')
import json
data = json.load(f)
for i in data:
     res = es.index(index='food',doc_type='Recipe',id=i["id"],body=i)

此方法耗時且效率低下。 我閱讀的其他方法需要以下格式的文件:

{"index": {"_index": "index_name", "_type": "index_type", "_id": "doc_id"}}
{"ingredients:....

你能建議一種有效的方法來索引文件嗎?

嘗試使用 Elasticsearch 散裝 Api

在單個 API 調用中執行多個索引或刪除操作。 這減少了開銷並可以大大提高索引速度。

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#docs-bulk https://elasticsearch-py.readthedocs.io/en/master/helpers.ZFC2535FDC708D52FC6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM