elasticsearch - 我如何使用 olivere 的弹性 client.Update() 服务来更改数组字段、通用字符串字段和可能的嵌套结构字段?

标签 elasticsearch go

https://github.com/olivere/elastic 版本 5.x

wiki 文档并不清楚 client.Update() 的工作原理。需要完全更改字段并修改数组。即在 wiki 文档的示例中,人们将如何向推文添加和删除标签或更改推文的内容?此外,如果一条推文在 go 中表示为一个结构,并且我添加了一个名为“echo”的嵌套结构,其中包含一个 int 类型的 foo、字符串类型的内容和另一种类型的字符串数组,那么如何使用更改这些字段中的任何一个client.Update() 如果可能的话?

在我个人的例子中我有这个功能:

func UpdateEntryContent(eclient *elastic.Client, entryID string, newContent []rune) error{
    ctx:=context.Background()

    exists, err := eclient.IndexExists(ENTRY_INDEX).Do(ctx)
    if err != nil {return err}
    if !exists {return errors.New("Index does not exist")}

    _, err = eclient.Update().Index(ENTRY_INDEX).Type(ENTRY_TYPE).Id(entryID).
        Script("ctx._source.Content = newCont").
        ScriptParams(map[string]interface{}{"newCont": newContent}).
        Do(ctx)

    if err != nil {return err}

    return nil
}

但是当我尝试编译时出现以下错误:

不能在 eclient.Update().Index(ENTRY_INDEX).Type(ENTRY_TYPE).Id(entryID).Script 的参数中使用“ctx._source.Content = newCont”(字符串类型)作为类型 *elastic.Script

eclient.Update().Index(ENTRY_INDEX).Type(ENTRY_TYPE).Id(entryID).Script("ctx._source.Content = newCont").ScriptParams 未定义(类型 *elastic.UpdateService 没有字段或方法脚本参数)

最佳答案

Script 方法接受一个 *elastic.Script,而不是一个字符串。 ScriptParams 方法也作为 Params*elastic.Script 上找到,而不是在 *elastic.UpdateService 上。

func UpdateEntryContent(eclient *elastic.Client, entryID string, newContent []rune) error{
    ctx:=context.Background()

    exists, err := eclient.IndexExists(ENTRY_INDEX).Do(ctx)
    if err != nil {return err}
    if !exists {return errors.New("Index does not exist")}

    script := elastic.NewScript("ctx._source.Content = newCont").Params(map[string]interface{}{"newCont": newContent})

    _, err = eclient.Update().Index(ENTRY_INDEX).Type(ENTRY_TYPE).Id(entryID).
        Script(script).
        Do(ctx)

    if err != nil {return err}

    return nil
}

您可以使用 GoDoc 查看有关包的更多信息或查看源代码。

关于elasticsearch - 我如何使用 olivere 的弹性 client.Update() 服务来更改数组字段、通用字符串字段和可能的嵌套结构字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45849186/

相关文章:

mongodb - mgo 中的 $dateToString 不起作用

go - 在golang中全局设置时区

elasticsearch - 发现Elasticsearch中的历史趋势(非视觉)

Go httputil.ReverseProxy 不覆盖 Host header

go - aws-sdk-go s3 在存储桶之间移动目录

elasticsearch - 在跨度优先查询中,我们可以基于存储在ES中的实际字符串来指定 “end”参数,还是必须根据存储在ES中的 token 来指定

go - 如何理解bazel覆盖coverage.dat文件?

elasticsearch - 三节点集群有多少master

php - 如何在 Elasticsearch 中获取搜索历史记录?

search - 检查服务器状态时Elasticsearch 503错误