【问题标题】:How to efficiently index a json file using elastic search and python?如何使用弹性搜索和 python 有效地索引 json 文件?
【发布时间】:2020-08-05 21:20:54
【问题描述】:

我是 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":
     .....]

这是一个列表的形式。 我现在用来索引文件的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:....

您能建议一种有效的方法来索引文件吗?

【问题讨论】:

标签: python json python-3.x elasticsearch lucene


【解决方案1】:

尝试使用 Elasticsearch bulk Api

在单个 API 调用中执行多个索引或删除操作。这可以减少开销并大大提高索引速度。

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

【讨论】:

    猜你喜欢
    • 2021-10-09
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2021-04-05
    相关资源
    最近更新 更多