如何在一个查询中插入多个记录?

时间:2014-07-16 10:27:01

标签: elasticsearch

例如:

curl -XPUT 'http://localhost:9200/test/users/2' -d '{ "firstname"   : "test" }'

只插入一条记录。

如何在一个查询中插入多个记录?

4 个答案:

答案 0 :(得分:6)

您需要使用elasticsearch Bulk API。它允许您通过一个请求插入多个项目。请求将发布到特殊端点/_bulk,如下所示:

{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }
{ "field1" : "value1" }
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
{ "field1" : "value2" }
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "3" } }
{ "field1" : "value3" }

答案 1 :(得分:1)

我没有足够的业力来回应@Tadzys,但是,是的,将文档批量插入一个或多个没有id的索引似乎是可行的。请参阅Elasticsearch: Bulk Inserting Examples中的“在同一请求中插入属于不同类型和索引的文档”。

从引用页面:

POST http://path.to.your.cluster/_bulk
{ "index":{ "_index": "myIndex", "_type": "person" } }
{ "name":"john doe","age":25 }
{ "index":{ "_index": "myOtherIndex", "_type": "dog" } }
{ "name":"fido","breed":"chihuahua" }

“Elasticsearch版本1.x,2.x和后来的版本也可以使用。”

答案 2 :(得分:1)

迟到总比没有好。这就是我如何向弹性服务器发出curl请求以使用Bulk API转储多个记录

PropertySort.NoSort

P.S:Elastic 5.x(atleast)似乎不允许在没有id属性的情况下进行批量创建。不得不痛苦地想出100个这样的记录

答案 3 :(得分:0)

以上所有答案都是正确的。我有一个更具体的一个。

端口 9200 ,而不是9201 @Hari K Murthy的回答。或者可能还有一些其他配置。

帖子网址是:http://127.0.0.1:9200/_bulk?pretty。您可能想要更改为自己的IP地址。

发表json是:

{ "create" : { "_index" : "my_index", "_type" : "my_document_type", "_id" : "1"} }
{"id" : "AAA" , "name" : "AAAAAAAAAAAAAA"}
{ "create" : { "_index" : "my_index", "_type" : "my_document_type", "_id" : "2"} }
{"id" : "BBB" , "name" : "BBBBBBBBBBBBBBB"}

例如: use bulk api in postman

注意:您必须将json格式化为一行元数据,另一行格式为bulk api doc