如何为Elasticsearch索引中的文档设置TTL

cl25kdpy  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(2)|浏览(391)

我正在寻找一种方法来设置TTL的文件在我的ElasticSearch索引理想的一个属性在我的Spring Boot 应用程序
我试着用curl来做,但我得到了这个错误。

curl --location --request PUT 'http://localhost:9200/order_index/_mapping/order_doc' \
--header 'Content-Type: application/json' \
--data-raw '{
    "_ttl": {
        "enabled": true,
        "default": "24h"
    }
}'

错误:

{
    "error": {
        "root_cause": [
            {
                "type": "mapper_parsing_exception",
                "reason": "Root mapping definition has unsupported parameters:  [_ttl : {default=24h, enabled=true}]"
            }
        ],
        "type": "mapper_parsing_exception",
        "reason": "Root mapping definition has unsupported parameters:  [_ttl : {default=24h, enabled=true}]"
    },
    "status": 400
}
w1e3prcc

w1e3prcc1#

在 当前 最 新 版本 的 Elasticsearch ( 7.15 ) _ttl is not yet supported 中 , 你 必须 创建 Index Lifecycle Management 策略 。
因此 , 您 必须 声明 策略 来 管理 datalifecyle , 并 在 Map 上 设置 此 策略 。
像 这样 的 例子 :信息 生命 周期 管理 :

PUT _ilm/policy/my_policy {   "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_primary_shard_size": "25GB" 
          }
        }
      },
      "delete": {
        "min_age": "30d",
        "actions": {
          "delete": {} 
        }
      }
    }   } }

中 的 每 一 个
Map :

PUT test-index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "index.lifecycle.name": "my_policy" 
  }
}

格式

jljoyd4f

jljoyd4f2#

**首先,**创建一个策略来描述何时删除索引

PUT〈-您的策略名称

{
  "policy": {
    "phases": {
      "hot": {
        "min_age": "0ms",
        "actions": {
          "set_priority": {
            "priority": 0
          }
        }
      },
      "delete": {
        "min_age": "2d", <-- Set your TTL here
        "actions": {
          "delete": {
            "delete_searchable_snapshot": true
          }
        }
      }
    }
  }
}

**接下来,**创建一个模板以选择使用此策略的索引类型。

在两天后删除

{
"index_patterns": [
    "test*" <-- Choose your index here
],
"template": {
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1,
        "index.lifecycle.name": "delete_log_after_2day"
    }
}
}

现在,当您创建一个新的索引示例时:test 001,它将在2天后自动删除。

**注意:**对于旧索引,它不会分配给新策略,因此除非您分配它,否则它不会被删除.

您可以使用此API将所有旧索引分配给策略
PUT http://localhost:9200/test */_settings〈--您的旧索引在这里,可以使用模式

{
  "index": {
    "lifecycle": {
      "name": "delete_log_after_2day" <-- Your policy name
    }
  }
}

然后在到期日期之后,所有旧索引都将被完全删除。

相关问题