如何通过一次API调用将多个文档发送到弹性文件

问题描述 投票:1回答:3

我是Elastic的新手,我需要一种方法将多个文件推送到Elastic只需要调用POST http://localhost:9200/myindex/mytype/

正文架构如下所示:

{ 
"docs": [ 
{ "_source": {"message":"message1"} }, 
{ "_source": {"message":"message2"} } 
] 
}

我尝试使用摄取API和管道而没有运气。

可能吗?

elasticsearch
3个回答
2
投票

我认为您需要的是Bulk API,它允许您在单个API调用中执行许多索引/删除操作,从而提高索引速度。这是link

所以你能做的就是

POST http://localhost:9200/_bulk POST http://localhost:9200/myindex/_bulk POST http://localhost:9200/myindex/mytype/_bulk

尝试其中一个与你的身体内容,并让我知道它是否有效。


1
投票

你可以使用bulk api这样做。

例如:

POST _bulk
{"index":{"_index":"my_index","_type":"_doc","_id":"1"}}
{"field1":"field 1 data 1","field2":11}
{"index":{"_index":"my_index","_type":"_doc","_id":"2"}}
{"field1":"field 1 data 2","field2":21}

在你的情况下,这将转换为:

POST _bulk
{"index":{"_index":"myindex","_type":"mytype"}}
{"message":"message1"}
{"index":{"_index":"myindex","_type":"mytype"}}
{"message":"message2"}

1
投票

谢谢你们@JinLee和@NishantSaini帮助我。我想记录下我做了什么。

首先,添加/_bulk端点。所以API调用现在是:POST http://localhost:9200/myindex/mytype/_bulk

现在将Content-Type标题设置为application/x-ndjson

然后身体必须是这样的:

{"index":{}}
{"message":"message1"}
{"index":{}}
{"message":"message2"}

现在一切正常!

© www.soinside.com 2019 - 2024. All rights reserved.