使用 Elasticsearch curator,我如何删除与模式匹配的所有索引,最新的除外?
我尝试使用filtertype: age
,但它似乎没有做我需要的。
使用 Elasticsearch curator,我如何删除与模式匹配的所有索引,最新的除外?
我尝试使用filtertype: age
,但它似乎没有做我需要的。
这是一个示例代码,假设您的索引名称中包含日期,您可以使用它来删除早于 14 天的索引。您可以通过以下链接获取更多信息 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)
您需要两个过滤器:(pattern
匹配要删除的索引)和age
(指定要删除的索引的年龄)。
例如下面的 Curator 配置被配置为删除
example_dev_*
配置:
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
您需要根据您的需要调整两种过滤条件,但这会达到您的期望。
我建议在模式过滤器之后使用计数过滤器。请务必使用排除真/假和试运行,直到它达到您的预期。