如何使用go-elasticsearch获取/更新别名?

huangapple go评论116阅读模式
英文:

How to get/update aliases with go-elasticsearch?

问题

我正在使用go-elasticsearch,我需要使用Aliases API中的操作:

  • 使用GET _alias/my-alias-name获取别名的信息
  • 使用POST _aliases将多个索引名称添加到别名中,以原子操作的方式

我找不到go-elasticsearch客户端中这些API的表示方式,是否有其他方法可以使用该库向这些端点发出请求,或者我只需要为这些方法使用一个普通的HTTP客户端?

英文:

I'm using the go-elasticsearch and I need to use the operations in the Aliases API:

  • GET _alias/my-alias-name to get the information on an alias
  • POST _aliases to add multiple index names to an alias in an atomic operation

I can't find the representations of these APIs in the go-elasticsearch client, is there any alternative way to make a request to these endpoints with the library, or I just need to use a plain HTTP client only for these methods?

答案1

得分: 1

终于找到了一种方法来实现,Aliases API位于Indices API内部:

  • GET _alias/my-alias-name:我们可以使用GetAlias()方法获取别名信息,构建如下请求:
  1. res, err := s.client.Indices.GetAlias(
  2. s.client.Indices.GetAlias.WithName("my-alias-name"),
  3. s.client.Indices.GetAlias.WithContext(context.Background()),
  4. )
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. // ...解析响应
  • POST _aliases:要在单个操作中添加或删除别名的索引,我们可以使用UpdateAliases()
  1. type UpdateAliasRequest struct {
  2. Actions []map[string]*UpdateAliasAction `json:"actions"`
  3. }
  4. // UpdateAliasAction表示Elasticsearch别名API中的操作。
  5. type UpdateAliasAction struct {
  6. Index string `json:"index"`
  7. Alias string `json:"alias"`
  8. }
  9. updateActions := make([]map[string]*UpdateAliasAction, 0)
  10. removeAction := make(map[string]*UpdateAliasAction)
  11. removeAction["remove"] = &UpdateAliasAction{
  12. Index: "old-index-00",
  13. Alias: "my-alias-name",
  14. }
  15. updateActions = append(updateActions, removeAction)
  16. addAction := make(map[string]*UpdateAliasAction)
  17. addAction["add"] = &UpdateAliasAction{
  18. Index: "new-index-00",
  19. Alias: "my-alias-name",
  20. }
  21. updateActions = append(updateActions, addAction)
  22. jsonBody, err := json.Marshal(&UpdateAliasRequest{
  23. Actions: updateActions,
  24. })
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. // 发送API请求
  29. res, err := s.client.Indices.UpdateAliases(
  30. bytes.NewBuffer(jsonBody),
  31. s.client.Indices.UpdateAliases.WithContext(context.Background()),
  32. )
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. // ...解析响应
英文:

Finally found a way to do it, the Aliases API is inside the Indices API:

  • GET _alias/my-alias-name: we can get the alias information using the GetAlias() method, building a request like this:
  1. res, err := s.client.Indices.GetAlias(
  2. s.client.Indices.GetAlias.WithName("my-alias-name"),
  3. s.client.Indices.GetAlias.WithContext(context.Background()),
  4. )
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. // ...parse response
  • POST _aliases to add or delete indices to aliases in an single operation we can use UpdateAliases():
  1. type UpdateAliasRequest struct {
  2. Actions []map[string]*UpdateAliasAction `json:"actions"`
  3. }
  4. // UpdateAliasAction represents an action in the Elasticsearch Aliases API.
  5. type UpdateAliasAction struct {
  6. Index string `json:"index"`
  7. Alias string `json:"alias"`
  8. }
  9. updateActions := make([]map[string]*UpdateAliasAction, 0)
  10. removeAction := make(map[string]*UpdateAliasAction)
  11. removeAction["remove"] = &UpdateAliasAction{
  12. Index: "old-index-00",
  13. Alias: "my-alias-name",
  14. }
  15. updateActions = append(updateActions, removeAction)
  16. addAction := make(map[string]*UpdateAliasAction)
  17. addAction["add"] = &UpdateAliasAction{
  18. Index: "new-index-00",
  19. Alias: "my-alias-name",
  20. }
  21. updateActions = append(updateActions, addAction)
  22. jsonBody, err := json.Marshal(&UpdateAliasRequest{
  23. Actions: updateActions,
  24. })
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. // make API request
  29. res, err := s.client.Indices.UpdateAliases(
  30. bytes.NewBuffer(jsonBody),
  31. s.client.Indices.UpdateAliases.WithContext(context.Background()),
  32. )
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. // ...parse response

huangapple
  • 本文由 发表于 2023年1月27日 14:15:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75254651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定