Go 调用 elasticsearch 批量操作接口 “/_bulk” ,提示"\n"错误

问题如标题。

我已经按照文档的说明来进行操作了,但是还是提示错误。求解。谢谢。

文档

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>
已邀请:

yulongshuai

赞同来自:

看文档是最后一行也要"\n",你试试?

apple123 - lololo

赞同来自:

确实最后一行需要\n

viewer

赞同来自:

最后一行要"\n"

要回复问题请先登录注册