douye2572
2017-08-23 21:00
浏览 468

如何使用Olivere的elastic client.Update()服务更改数组字段,通用字符串字段以及可能嵌套的结构字段?

https://github.com/olivere/elastic Version 5.x

The wiki documentation isn't really clear on how client.Update() works. It's needed to completely change a field and to modify arrays. i.e. in the example in the wiki documentation, how would one go about appending and removing tags to a tweet or changing a tweet's content? Also if a tweet was represented in go as a struct and I added a nested struct called "echo" which contains a foo of type int, content of type string and another type string array, how would one go about changing any of these fields using client.Update() if it's even possible?

In my personal example I have this function:

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
}

But I get this following error when I try to compile:

cannot use "ctx._source.Content = newCont" (type string) as type *elastic.Script in argument to eclient.Update().Index(ENTRY_INDEX).Type(ENTRY_TYPE).Id(entryID).Script

eclient.Update().Index(ENTRY_INDEX).Type(ENTRY_TYPE).Id(entryID).Script("ctx._source.Content = newCont").ScriptParams undefined (type *elastic.UpdateService has no field or method ScriptParams)

图片转代码服务由CSDN问答提供 功能建议

https://github.com/olivere/elastic Version 5.x

Wiki文档尚不清楚client.Update()的工作方式。 需要完全更改一个字段并修改数组。 即,在Wiki文档的示例中,如何将标签添加和删除到推文上或更改推文的内容? 另外,如果一条推文以go表示为结构,并且我添加了一个称为“ echo”的嵌套结构,其中包含一个int类型的foo,类型为string的内容和另一个类型为string的数组,那么如何使用这些字段来更改任何这些字段

在我的个人示例中,我具有以下功能:

  func UpdateEntryContent(eclient * elastic  .Client,entryID字符串,newContent [] rune)错误{
 ctx:= context.Background()
 
存在,err:= eclient.IndexExists(ENTRY_INDEX).Do(ctx)
如果err!= nil  {return err} 
如果存在{返回错误。New(“索引不存在”)} 
 
 _,err = eclient.Update()。Index(ENTRY_INDEX).Type(ENTRY_TYPE).Id(entryID  )。
 Script(“ ctx._source.Content = newCont”)。
 ScriptParams(map [string] interface {} {“ newCont”:newContent})。
如果错误,请执行(ctx)
 
!  = nil {返回err} 
 
返回nil 
} 
   
 
 

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

不能使用“ ct x._source.Content = newCont“(类型字符串)作为* elastic.Script类型,位于eclient.Update()。Index(ENTRY_INDEX).Type(ENTRY_TYPE).Id(entryID).Script \ n

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongyu8694 2017-09-09 21:33
    已采纳

    Credit where it's due Gavin's answer put me on the right track. This is for another .Index but the full function that acts as a generic single field update is as follows:

    func UpdateUser(eclient *elastic.Client, userID string, field string, newContent interface{})error {
        //CHANGES A SINGLE FIELD OF ES USER DOCUMENT(requires an elastic client pointer,
        //  the user DocID, the feild you wish to modify as a string,
        //  and what you want to change that field to as any type necessary)
        //RETURN AN error IF SUCESSFUL error = nil
    
        ctx := context.Background()
    
        exists, err := eclient.IndexExists(USER_INDEX).Do(ctx)
        if err != nil {return err}
        if !exists {return errors.New("Index does not exist")}
    
        _, err = eclient.Update().
            Index(USER_INDEX).
            Type(USER_TYPE).
            Id(userID).
            Doc(map[string]interface{}{field: newContent}).
            Do(ctx)
    
    
        return nil
    }
    

    You can change the .Index, .Type, and .Id and it works with all fields and types as far as I can tell

    打赏 评论
  • doutang6600 2017-08-24 19:14

    The Script method accepts a *elastic.Script, not a string. The ScriptParams method is also found on *elastic.Script as Params instead of being on *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
    }
    

    You can see more information about the package with GoDoc or by looking through the source code.

    打赏 评论
  • dongnan1899 2017-08-28 20:31

    The following code should resolve the issue

    _, err = eclient.Update().Index(INDEX). Type(TYPE). Id(ID). Doc(map[string]interface{}{field: message}). Do(ctx)

    打赏 评论

相关推荐 更多相似问题