douye2572 2017-08-23 21:00
浏览 858
已采纳

如何使用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)

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab