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

How to create an ElasticSearch policy from Golang client

WBOY
Release: 2024-02-05 22:15:12
forward
985 people have browsed it

如何从 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": {}
        }
      }
    }
  }
}
Copy after login

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
}
Copy after login

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())
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template