gpt4 book ai didi

python - 使用document_id作为csv字段将csv从Python传输到Elasticsearch

转载 作者:行者123 更新时间:2023-12-02 23:44:19 24 4
gpt4 key购买 nike

想将以下csv转移到elsticsearch

|hcode|hname|
|1|aaaa|
|2|bbbbb|
|3|ccccc|
|4|dddd|
|5|eeee|
|6|ffff|
并需要插入hcode字段作为document_id。低于错误
  File "C:\Users\Namali\Anaconda3\lib\site-packages\elasticsearch\connection\base.py", line 181, in _raise_error
status_code, error_message, additional_info

RequestError: RequestError(400, 'mapper_parsing_exception', 'failed to parse')"
使用elasticseach版本是7.1.1和python vervion是3.7.6
Python代码------------------------------------------------ -----------------
import csv
import json

from elasticsearch import Elasticsearch

es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

def csv_reader(file_obj, delimiter=','):
reader_ = csv.reader(file_obj,delimiter=delimiter,quotechar='"')

i = 1
results = []
for row in reader_:
#try :
#es.index(index='hb_hotel_raw', doc_type='hb_hotel_raw', id=row[0],
# body=json.dump([row for row in reader_], file_obj))
es.index(index='test', doc_type='test', id=row[0],body=json.dumps(row))
#except:
# print("error")
i = i + 1
results.append(row)
print(row)

if __name__ == "__main__":
with open("D:\\namali\\rez\\data_mapping\\test.csv") as f_obj:
csv_reader(f_obj)

最佳答案

首先,elasticsearch 7中省略了doc_type。第二,您需要将有效的json传递给elasticsearch。我编辑了您的代码,如下所示:

for row in reader_:
_id = row[0].split("|")[1]
text = row[0].split("|")[2]
my_dict = {"hname" : text}
es.index(index='test', id=_id, body=my_dict)

关于python - 使用document_id作为csv字段将csv从Python传输到Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62531262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com