dtd5644 2018-07-16 18:08
浏览 1648

Go中Elasticsearch的大量更新

I try to update my database of elasticsearch with golang.I have two functions :

func UpdateAllByUserID(client *elastic.Client, id string, requestName string, requestNick string) error {
    ctx := context.Background()

    query := elastic.NewMatchQuery("user_id", id)

    out_name, err := client.UpdateByQuery().Index("test").Type("test").Query(query).Script(elastic.NewScriptInline("ctx._source.user_name = '" + requestName + "'")).Do(ctx)
    if nil != err {
        log.Println(err)
    }

    fmt.Println("update all name: ", out_name.Updated)

    return nil

}


func UpdateAllNicksByUserIdInFeed(client *elastic.Client, id string, requestNick string) error {
    ctx := context.Background()

    query := elastic.NewMatchQuery("user_id", id)


    out_nick, err := client.UpdateByQuery().Index("test").Type("test").Query(query).Script(elastic.NewScriptInline("ctx._source.user_nick = '" + requestNick + "'")).Do(ctx)
    if nil != err {
        log.Println(err)
    }

    fmt.Println("update all nick: ", out_nick.Updated)

    return nil

}

The POST in elastic:

POST {index}/{type}/_update_by_query
{
  "script": {
    "inline": "ctx._source.user_name = 'test'",
    "inline": "ctx._source.user_nick = 'test test'"
  },
  "query": {
    "match": {
      "user_id": "mtJZngDOy6Qj22Qv9MEf1MhSLVb2"
    }
  }
}

I'm using the library github.com/olivere/elastic. The versión of elasticsearch is 5.6 This functions per separate it works well, but I have two problems:

How can update in the same function? Why then use the two functions at the same time I have this error:

elastic: Error 409 (Conflict)

  • 写回答

2条回答 默认 最新

  • dslpofp041310584 2018-07-17 07:25
    关注

    I resolved the problem:

    out_name, err := client.UpdateByQuery().Index("test").Type("test").Query(query).Script(elastic.NewScriptInline("ctx._source.user_name = '" + requestName + "';ctx._source.user_nick = '" + requestNick + "';ctx._source.user_photo = '" + fullImageURL + "';ctx._source.user_thumb = '" + thumbnailURL + "'")).Do(ctx)
        if nil != err {
            log.Println(err)
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度