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

时间:2019-09-19 13:47:19

标签: elasticsearch elasticsearch-curator

使用Elasticsearch策展人,如何删除与模式匹配的所有索引,但最新的除外?

我尝试使用filtertype: age,但是它似乎并没有满足我的需要。

3 个答案:

答案 0 :(得分:2)

这是示例代码,您可以使用该代码删除索引 假设索引名称中包含日期,则早于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)

答案 1 :(得分:0)

您需要两个过滤器: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

您需要使两种过滤条件都适应您的需求,但这将达到您的期望。

答案 2 :(得分:0)

我建议在模式过滤器之后使用计数过滤器。一定要排除真/假和空转,直到它达到您的期望为止。