簡體   English   中英

如何通過一個API調用將多個文檔發送到Elastic

[英]How can I send multiple documents to elastic with one API call

我是Elastic的新手,我需要一種方法,只需調用POST http://localhost:9200/myindex/mytype/即可將多個文檔推送到Elastic。

主體架構如下所示:

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

我嘗試了攝取API和管道,但沒有運氣。

可能嗎?

我認為您需要的是Bulk API ,該API可讓您在單個API調用中執行許多索引/刪除操作,從而提高了索引編制速度。 這是鏈接

所以你可以做的是

POST http://localhost:9200/_bulk
POST http://localhost:9200/myindex/_bulk
POST http://localhost:9200/myindex/mytype/_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"}

謝謝@JinLee和@NishantSaini的幫助。 我想記錄一下我所做的。

首先,添加/_bulk端點。 因此,API調用現在為: POST http://localhost:9200/myindex/mytype/_bulk

現在將Content-Type標頭設置為application/x-ndjson

然后身體必須是這樣的:

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

現在一切正常!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM