使用Python在Elasticsearch中索引JSON文件? [英] Index JSON files in elasticsearch using Python?

查看:194
本文介绍了使用Python在Elasticsearch中索引JSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆JSON文件(100),分别命名为merged_file 1.json,merged_file 2. json等。

I have a bunch of JSON files(100), which are named as merged_file 1.json, merged_file 2. json and so on.

如何编制索引所有这些文件都使用python(elasticsearch_dsl)转换为elasticsearch吗?

How do I index all these files into elasticsearch using python(elasticsearch_dsl) ?

我正在使用此代码,但似乎无法正常工作:

I am using this code, but it doesn't seem to work:

from elasticsearch_dsl import Elasticsearch
import json
import os
import sys

es = Elasticsearch()

json_docs =[]

directory = sys.argv[1]

for filename in os.listdir(directory):
    if filename.endswith('.json'):
        with open(filename,'r') as open_file:
            json_docs.append(json.load(open_file))

es.bulk("index_name", "type_name", json_docs)

JSON如下所示:

{"one":["some data"],"two":["some other data"],"three":["other data"]}

我该怎么做才能使此正确?

What can I do to make this correct ?

推荐答案

对于此任务,您应该使用 elasticsearch-py pip install elasticsearch ):

For this task you should be using elasticsearch-py (pip install elasticsearch):

from elasticsearch import Elasticsearch, helpers
import sys, json

es = Elasticsearch()

def load_json(directory):
    " Use a generator, no need to load all in memory"
    for filename in os.listdir(directory):
        if filename.endswith('.json'):
            with open(filename,'r') as open_file:
                yield json.load(open_file)

helpers.bulk(es, load_json(sys.argv[1]), index='my-index', doc_type='my-type')

这篇关于使用Python在Elasticsearch中索引JSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆