Home > Net >  How to create ElasticSearch policy from Golang client
How to create ElasticSearch policy from Golang client

Time:12-05

I'm trying to create index lifecycle management (ILM) policy from Elastic Golang client olivere to delete indexes older than 3 months (using "index-per-day" pattern). Something like this:

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

I can see in the lib's source code there is structure for that: XPackIlmPutLifecycleService which has the following fields:

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
}

And here is the documentation link. However I'm a bit confused how to create policy using it to do the job as it seems missing some fields (for example min_age to set the TTL for index). What's the proper way to create ILM policy via this client.

CodePudding user response:

You can reference the test code! Basically you can put the json to the body field.

    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

  • Related