簡體   English   中英

如何在Elasticsearch中的POST請求正文中指定要索引的2個或更多文檔?

[英]How do I specify 2 or more documents to be indexed in the body of POST request in elasticsearch?

我是Elasticsearch的新手。 我創建了一個名為comforts的索引,並且試圖在Postman工具http:// localhost:9200 / amenities / test / _bulk中執行POST請求。

在這里,將內容類型以及執行請求時給出的錯誤設置為application / json。

在此處輸入圖片說明

在這里,它顯示了JSON中的語法錯誤。

在此處輸入圖片說明

我無法理解如何索引多個JSON對象(文檔)。 當我僅指定一個文檔時,它工作良好,但是當我指定兩個或多個文檔時,JSON無效。

我嘗試了以下解決方案:

{“ index”:{}} {“ type”:“廚房”,“位置”:{“ x”:9881.034723869176,“ y”:-12942.49413158995},“ icon”:“餐具”,“類別”:“設施“} {” index“:{}} {” type“:”垃圾箱“,”位置“:{” x“:9170.444649524274,” y“:-12855.890257805067},” icon“:”垃圾桶“,”類別“ :“便利設施”}

{“ index”:{“ _index”:“便利設施”,“ _type”:“測試”}} {“ type”:“廚房”,“位置”:{“ x”:9881.034723869176,“ y”:-12942.49413158995} ,“ icon”:“餐具”,“ category”:“設施”} {“ index”:{“ _index”:“設施”,“ _type”:“測試”}} {“ type”:“垃圾桶”, “ location”:{“ x”:9170.444649524274,“ y”:-12855.890257805067},“ icon”:“垃圾箱”,“類別”:“設施”}

仍然會給出語法錯誤。

我想念什么? 謝謝!

如上面的評論所述,使用Postman發送一個批量查詢絕對是可能的,只需要正確設置格式並以原始文本發送即可,如下所示:

在此處輸入圖片說明

另請注意,標頭部分包含一個HTTP標頭:

ContentType: application/x-ndjson

您可以將文檔以以下格式(testData.json)放在json文件中:

{"index": {"_index": "animals", "_type": "_doc", "_id": 1}}
{"name": "dog"}
{"index": {"_index": "animals", "_type": "_doc", "_id": 2}}
{"name": "cat"}

並像這樣使用curl:

curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@testData.json";

或者,如果您想在沒有json文件的情況下使用curl:

 curl -X POST "localhost:9200/_bulk" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }'

或者,您可以使用更簡單的kibana。 檢查文檔_bulk

暫無
暫無

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

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