我正在尝试从 elastic golang 客户端 olivere 创建索引生命周期管理 (ilm) 策略,以删除超过 3 个月的索引(使用“每日索引”模式)。像这样的事情:
{
"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
}
这是文档链接。然而,我有点困惑如何创建使用它来完成这项工作的策略,因为它似乎缺少一些字段(例如 min_age
设置索引的 ttl)。通过此客户端创建 ilm 策略的正确方法是什么。
正确答案
可以参考测试代码!基本上你可以将 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