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

Bdf*_*dfy 4 elasticsearch

例如 :

curl -XPUT 'http://localhost:9200/test/users/2' -d '{ "firstname"   : "test" }'
Run Code Online (Sandbox Code Playgroud)

只插入一条记录.

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

Kon*_*hov 8

您需要使用elasticsearch Bulk API.它允许您通过一个请求插入多个项目.请求被POST到特殊端点/_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" }
Run Code Online (Sandbox Code Playgroud)

  • @康斯坦丁-V-salkhov如果我想插入用于相同的索引和相同类型的文档,并且希望使用自动生成的ID是什么,有没有键入一个可能性:`{"指数":{"_index":"测试","_ type":"type1"}}`只有一次,然后只是文件本身?我想不是,因为我在文档中找不到它,但这将是一个很好的功能. (5认同)

Nel*_*els 5

我没有足够的业力来评论@Tadzys,但是,是的,在没有 id 的情况下将文档批量插入到一个或多个索引似乎是可行的。请参阅Elasticsearch:批量插入示例中的“在同一请求中插入属于不同类型和索引的文档” 。

从引用的页面:

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" }
Run Code Online (Sandbox Code Playgroud)

“示例适用于 Elasticsearch 版本 1.x、2.x,也可能适用于更高版本。”