【发布时间】:2013-12-15 19:52:15
【问题描述】:
我必须在 ElasticSearch 中存储一些消息,并与我的 python 程序集成。 现在我尝试存储消息的是:
d={"message":"this is message"}
for index_nr in range(1,5):
ElasticSearchAPI.addToIndex(index_nr, d)
print d
这意味着如果我有 10 条消息,那么我必须重复我的代码 10 次。 所以我想做的是尝试制作一个脚本文件或批处理文件。 我检查了ElasticSearch Guide,可以使用 BULK API。 格式应如下所示:
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }
{ "doc" : {"field2" : "value2"} }
我所做的是:
{"index":{"_index":"test1","_type":"message","_id":"1"}}
{"message":"it is red"}
{"index":{"_index":"test2","_type":"message","_id":"2"}}
{"message":"it is green"}
我还使用 curl 工具来存储文档。
$ curl -s -XPOST localhost:9200/_bulk --data-binary @message.json
现在我想使用我的 Python 代码将文件存储到 Elastic Search。
【问题讨论】:
-
看看一些python客户端,比如pyes:github.com/aparo/pyes或者elasticsearch官方客户端github.com/elasticsearch/elasticsearch-py
-
非常感谢,我检查了一些客户端并尝试使用 pyelasticsearch。我已经用 pyelastic 存储了带有批量索引的摊位。在 pyelasticsearch 中,文档文件将位于代码中。是否可以将我想要批量索引的 doc 文件放在程序之外?
标签: python elasticsearch elasticsearch-bulk-api