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

如何使用 Go 在弹性搜索查询中放置格式说明符?

如何使用 Go 在弹性搜索查询中放置格式说明符?

Go
眼眸繁星 2022-09-19 21:15:18
在下面的代码中,我想放一个变量id,就像一些格式说明符。如何使用 Golang 对以下弹性搜索查询执行此操作?%dstr := `{        "query": {          "match": {            "id": 123          }        }      }`    s := []byte(str)url := "http://localhost:9200/student/_delete_by_query"_, err = helper.GoDelete(url, s)if err != nil {    return err}return nil
查看完整描述

2 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

使用可能是最简单的方法,而不是最快的方法。但最简单。fmt.Sprintf

d := 123
id := fmt.Sprintf(`{"query": {"match": {"id": %d}}}`, d)


查看完整回答
反对 回复 2022-09-19
?
慕斯王

TA贡献1864条经验 获得超2个赞

fmt.Sprintf可以工作,但也容易出错。我会创建适当的结构,然后用它来做:json.Marshal


type (

    Match struct {

        ID int `json:"id"`

    }

    Query struct {

        Match Match `json:"match"`

    }

    MyStruct struct {

        Query Query `json:"query"`

    }

)


func main() {

    s := MyStruct{

        Query: Query{

            Match: Match{

                ID: 123,

            },

        },

    }

    bytes, err := json.Marshal(s)

}


查看完整回答
反对 回复 2022-09-19
  • 2 回答
  • 0 关注
  • 62 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

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

帮助反馈 APP下载

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

公众号

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