Home >Backend Development >Golang >How to create an ElasticSearch policy from Golang client

How to create an ElasticSearch policy from Golang client

WBOY
WBOYforward
2024-02-05 22:15:12984browse

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

Question content

I am trying to create an index lifecycle management (ilm) policy from elastic golang client olivere to delete indexes older than 3 months (Using "Daily Index" mode). Something like this:

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

I can see a structure like this in the source code of the library: 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
}

This is the documentation link. However, I'm a bit confused as to how to create a strategy to use this to do the job, as it seems to be missing some fields (e.g. min_age to set the ttl of the index). What is the correct way to create ilm policy via this client.


Correct answer


You can refer to the test code! Basically you can put json into 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

The above is the detailed content of How to create an ElasticSearch policy from Golang client. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete