使用 Elasticsearch 策展人,如何删除与模式匹配的所有索引,除了最新的索引?
我尝试使用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
(指定要删除的索引的期限(。
例如,下面的鉴赏家配置配置为删除
- 名为
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
您需要根据自己的需要调整两种过滤条件,但这将达到您的预期。
我建议在模式过滤器之后使用计数过滤器。确保使用排除真/假和试运行,直到它达到您的预期。