Go 呼叫 elasticsearch 批量操作介面 “/_bulk” ,提示"\n"錯誤

1272881215發表於2018-08-08

問題如標題。

我已經按照文件的說明來進行操作了,但是還是提示錯誤。求解。謝謝。

文件

https://www.bookstack.cn/read/elasticsearch-definitive-guide-cn/030_Data-55_Bulk.md

我的原始碼

package main

import (
    "fmt"
    "strings"

    "github.com/astaxie/beego/httplib"
)

func main() {
    api := "http://localhost:9200/_bulk"
    body := []string{
        `{ "index": { "_index": "testbulk", "_type": "fulltext","_id":1 }}`,
        `{ "title":    "My second blog post 1" }`,
        `{ "index": { "_index": "testbulk", "_type": "fulltext","_id":2 }}`,
        `{ "title":    "My second blog post 2" }`,
        `{ "index": { "_index": "testbulk", "_type": "fulltext","_id":3 }}`,
        `{ "title":    "My second blog post 3" }`,
    }
    str := strings.Join(body, "\n")
    fmt.Println(str)
    resp, err := httplib.Post(api).Header("Content-Type", "application/json").Body(str).String()
    fmt.Println(resp, err)
}

提示的錯誤

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"}],"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"},"status":400} <nil>
更多原創文章乾貨分享,請關注公眾號
  • Go 呼叫 elasticsearch 批量操作介面 “/_bulk” ,提示"\n"錯誤
  • 加微信實戰群請加微信(註明:實戰群):gocnio

相關文章