如何从Golang客户端创建ElasticSearch策略

0mkxixxg  于 2022-12-07  发布在  Go
关注(0)|答案(1)|浏览(180)

我正在尝试从Elastic Golang客户端olivere创建索引生命周期管理(ILM)策略,以删除超过3个月的索引(使用“index-per-day”模式)。

{
  "policy": {
    "phases": {      
      "delete": {
        "min_age": "90d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}

我可以在库的源代码中看到它的结构:XPackIlmPutLifecycleService,其中包含以下字段:

type XPackIlmPutLifecycleService struct {
    client *Client

    pretty     *bool       // pretty format the returned JSON response
    human      *bool       // return human readable values for statistics
    errorTrace *bool       // include the stack trace of returned errors
    filterPath []string    // list of filters used to reduce the response
    headers    http.Header // custom request-level HTTP headers

    policy        string
    timeout       string
    masterTimeout string
    flatSettings  *bool
    bodyJson      interface{}
    bodyString    string
}

这里是文档link。但是,我对如何使用它来创建策略感到有点困惑,因为它似乎缺少一些字段(例如,min_age用于设置索引的TTL)。通过此客户端创建ILM策略的正确方法是什么?

g6ll5ycj

g6ll5ycj1#

你可以引用测试代码!基本上你可以把json放到body字段。

testPolicyName := "test-policy"

    body := `{
        "policy": {
            "phases": {
                "delete": {
                    "min_age": "90d",
                    "actions": {
                        "delete": {}
                    }
                }
            }
        }
    }`

    // Create the policy
    putilm, err := client.XPackIlmPutLifecycle().Policy(testPolicyName).BodyString(body).Do(context.TODO())

https://github.com/olivere/elastic/blob/release-branch.v7/xpack_ilm_test.go#L15-L31

相关问题