python - 在 Python 中将 CSV 索引到 ElasticSearch

标签 python csv elasticsearch python-3.5 elasticsearch-dsl

希望在不使用 Logstash 的情况下将 CSV 文件编入 ElasticSearch 的索引。 我正在使用 elasticsearch-dsl 高级库。

例如给定一个带标题的 CSV:

name,address,url
adam,hills 32,http://rockit.com
jane,valleys 23,http://popit.com

按字段索引所有数据的最佳方法是什么?最终我想让每一行看起来像这样

{
"name": "adam",
"address": "hills 32",
"url":  "http://rockit.com"
}

最佳答案

使用较低级别的 elasticsearch-py 库可以更轻松地完成此类任务:

from elasticsearch import helpers, Elasticsearch
import csv

es = Elasticsearch()

with open('/tmp/x.csv') as f:
    reader = csv.DictReader(f)
    helpers.bulk(es, reader, index='my-index', doc_type='my-type')

关于python - 在 Python 中将 CSV 索引到 ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41573616/

相关文章:

python - 从包导入时模块抛出名称错误

asp.net - 将 CSV 文件或 Excel 电子表格转换为 RESX 文件

elasticsearch - ElasticSearch:如果文档中存在字段,则在script_score函数内执行余弦相似度

Elasticsearch 5 : Unknown key for a START_OBJECT in [filters]

python - 如何使用 Python/C API 将 numpy.ndarray 转换为 cv::Mat?

python - 使用 threading.Timer 与基于异步的函数

python - 如何子类化 pyCLI 的 cli.app.CommandLineApp?

string - 将字符串转换为 csv Powershell

java - Apache commons-csv 错误(带引号)

elasticsearch - 在ElasticSearch中序列化RegionInfo