elasticsearch,python-requests,Indexing,elasticsearch,Python Requests" /> elasticsearch,python-requests,Indexing,elasticsearch,Python Requests" />

Indexing 使用python请求模块的Elasticsearch批量/批量索引

Indexing 使用python请求模块的Elasticsearch批量/批量索引,indexing,elasticsearch,python-requests,Indexing,elasticsearch,Python Requests,我有一个很小的(约50,00)json字典数组,我想在ES中存储/索引这些字典。我的首选是使用python,因为我要索引的数据来自csv文件,通过python加载并转换为json。或者,我想跳过转换为json的步骤,简单地使用我拥有的python字典数组。无论如何,快速搜索揭示了ES的批量索引功能。我想这样做: post_url = 'http://localhost:9202/_bulk' request.post(post_url, data = acc ) # acc a pytho

我有一个很小的(约50,00)json字典数组,我想在ES中存储/索引这些字典。我的首选是使用python,因为我要索引的数据来自csv文件,通过python加载并转换为json。或者,我想跳过转换为json的步骤,简单地使用我拥有的python字典数组。无论如何,快速搜索揭示了ES的批量索引功能。我想这样做:

post_url = 'http://localhost:9202/_bulk'
request.post(post_url, data = acc )    # acc a python array of dictionaries


这两个请求都会出现[HTTP 500错误]

我对Python了解不多,但您看过吗?
pye支持大容量。

我的理解是,每行必须有一个“命令”(index、create、delete…),然后其中一些命令(如index)在下一行获取一行数据,如下所示

{'index': ''}\n
{'your': 'data'}\n
{'index': ''}\n
{'other': 'data'}\n
注意新行,即使在最后一行


如果您发布到../index/type/\u bulk,或者您需要指定索引和类型,则上述空索引对象可以正常工作。我想,您还没有尝试过这样做。

您可以使用以下函数:

def post_request(self, endpoint, data):
   endpoint = 'localhost:9200/_bulk'
   response = requests.post(endpoint, data=data, headers={'content-type':'application/json', 'charset':'UTF-8'})

   return response
作为数据,您需要传递以下字符串:

{ "index" : { "_index" : "test-index", "_type" : "_doc", "_id" : "1681", "routing" : 0 }}
{ "field1" : ... , ..., "fieldN" : ... }
{ "index" : { "_index" : "test-index", "_type" : "_doc", "_id" : "1684", "routing" : 1 }}
{ "field1" : ... , ..., "fieldN" : ... }

确保在每行末尾添加“\n”。

数据的数据类型是什么,不应该是DICT列表吗?
{ "index" : { "_index" : "test-index", "_type" : "_doc", "_id" : "1681", "routing" : 0 }}
{ "field1" : ... , ..., "fieldN" : ... }
{ "index" : { "_index" : "test-index", "_type" : "_doc", "_id" : "1684", "routing" : 1 }}
{ "field1" : ... , ..., "fieldN" : ... }