Elasticsearch Curator - 删除除最新索引外的索引

Elasticsearch Curator - delete indices except newest

使用 Elasticsearch curator,我如何删除与模式匹配的所有索引,最新的索引除外?

我尝试使用 filtertype: age,但它似乎无法满足我的需要。

您需要两个过滤器:pattern(匹配您要删除的索引)和age(指定要删除的索引的期限)。

例如下面的 Curator 配置被配置为删除

  • 名为 example_dev_*
  • 的索引
  • 并且超过 10 天

配置:

actions:
  1:
    action: delete_indices
    description: >-
      Delete indices older than 10 days (based on index name), for example_dev_
      prefixed indices.
    options:
      ignore_empty_list: True
      disable_action: True
    filters:
    - filtertype: pattern
      kind: prefix
      value: example_dev_
    - filtertype: age
      source: creation_date
      direction: older
      unit: days
      unit_count: 10
    - filtertype: count
      count: 1

您需要根据需要调整这两个过滤条件,但这会达到您的预期。

我建议在模式过滤器之后使用计数过滤器。请务必使用 exclude true/false 进行试运行,直到它达到您的预期为止。

Here is an example code which you can use to delete the indices which are older than 14 days assuming your index name have the date in it. You can get more information on the below link https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/curator.html

import os
import sys
import json, io, boto3
import time, datetime
import curator
from elasticsearch import Elasticsearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
import boto3

esEndPoint = ES_HOST # Add the ElasticSearch host.
region = REGION # Region where the ElasticSearch is present.
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

def lambda_handler(event, context):
    esClient = connectES(esEndPoint)
    index_list = curator.IndexList(esClient)
    index_list.filter_by_age(source='name', direction='older', timestring='%Y.%m.%d', unit='days', unit_count=14)
    print(index_list.indices)
    if index_list.indices:
        curator.DeleteIndices(index_list).do_action() # Delete the indices

def connectES(esEndPoint): 
    # Function used to connect to ES
    try:
        es = Elasticsearch(
            hosts=[{'host': esEndPoint, 'port': 443}],
            http_auth=awsauth,
            use_ssl=True,
            verify_certs=True,
            connection_class=RequestsHttpConnection
        )
        return es
    except Exception as E:
        print("Unable to connect to {0}".format(esEndPoint))
        print(E)