Elasticsearch Query DSL建立滾動索引(生命週期策略)

Jackie_JK發表於2024-11-01

建立生命週期策略,並指定索引文件數量為10時建立新的索引:

PUT _ilm/policy/roll-policy-10
{
  "policy": {
    "phases": {
      "hot": {
        "min_age": "0s",
        "actions": {
          "rollover": {
            "max_docs": 10
          },
          "set_priority": {
            "priority": 100
          }
        }
      }
    }
  }
}

建立一個索引,並將索引與生命週期策略關聯:

PUT /roll-000001
{
  "settings": {
    "index":{
      "lifecycle": {
        "name": "roll-policy",
        "rollover_alias" : "roll-alias"
      }
    }
  }, 
  "aliases": {
    "roll-alias": {
      "is_write_index": true
    }
  }
}

建立後,往索引裡插入一定數量的文件,將會自動建立roll-000002索引,但是據筆者觀察,不一定十分及時。

如果想用Java elasticseach client建立生命週期策略(不常用,一般不由開發建立):

IlmPolicy ilmPolicy = IlmPolicy.of(a -> a.phases(b -> b.hot(c -> c.actions(null))));
PutLifecycleRequest putLifecycleRequest = PutLifecycleRequest.of(a -> a.name("").policy(ilmPolicy));
client.ilm().putLifecycle(putLifecycleRequest);

相關文章