为了账号安全,请及时绑定邮箱和手机立即绑定

使用弹性搜索去更新文档

使用弹性搜索去更新文档

Go
心有法竹 2022-11-08 14:46:04
我使用go-elasticsearch作为弹性搜索客户端。我尝试使用此处的代码来更新文档elastic/go-elasticsearch/api.update.go但是使用来自 GitHub 的相同代码,我得到:[UpdateRequest] unknown field [Title]到目前为止,我只有一个看起来像这样的文件{    "took": 3,    "timed_out": false,    "_shards": {        "total": 4,        "successful": 4,        "skipped": 0,        "failed": 0    },    "hits": {        "total": {            "value": 1,            "relation": "eq"        },        "max_score": 1.0,        "hits": [            {                "_index": "posts",                "_type": "_doc",                "_id": "fc87941f-dc82-4fdb-9e04-460830e5f3ab",                "_score": 1.0,                "_source": {                    "Title": "one two three2",                    "Done": false,                    "Created": "2022-02-09T11:51:59.027356+01:00",                    "Updated": null                }            }        ]    }}我的去功能:func (postRepository) Update(id uuid.UUID, input inputs.Post) error {    channel := make(chan error)    go func() {        post := entities.Post{            Title:   "title updated",        }        body, err := json.Marshal(post)        if err != nil {            channel <- err        }        request := esapi.UpdateRequest{            Index:      index,            DocumentID: id.String(),            Body:       strings.NewReader(string(body)),        }        response, err := request.Do(context.Background(), data.ElasticSearchClient)        if err != nil {            channel <- err        }        defer response.Body.Close()此外,这是唯一不起作用的功能,其他功能如:AddDocument, RemoveDocument,GetDocuments都按预期工作。
查看完整描述

1 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

在这里找到解决方案:a-simple-elasticsearch-crud-example-in-golang


而不是使用:


request := esapi.UpdateRequest{

    Index:      index,

    DocumentID: id.String(),

    Body:       strings.NewReader(string(body)),

}

利用


request := esapi.UpdateRequest{

    Index:      index,

    DocumentID: id.String(),

    Body: bytes.NewReader([]byte(fmt.Sprintf(`{"doc":%s}`, body))),

}


查看完整回答
反对 回复 2022-11-08
  • 1 回答
  • 0 关注
  • 161 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号