1

我有一个小(~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

或者

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

两个请求都给出 [HTTP 500 错误]

4

3 回答 3

3

我的理解是每行必须有一个“命令”(索引,创建,删除......),然后其中一些(如索引)在下一行获取一行数据,如下所示

{'index': ''}\n
{'your': 'data'}\n
{'index': ''}\n
{'other': 'data'}\n

注意换行符,即使在最后一行。

如果您发布到 ../index/type/_bulk 或者您需要指定索引和类型,我认为没有尝试过,那么像上面这样的空索引对象有效。

于 2014-10-31T07:24:13.887 回答
2

您可以使用以下功能:

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”。

于 2018-06-05T08:38:03.540 回答
0

我对 Python 了解不多,但你看过Pyes吗?Pyes 支持批量。

于 2012-12-22T07:49:37.587 回答