如何限制索引中ElasticSearch文档的最大数量? [英] How to limit max number of ElasticSearch documents in a index?

查看:203
本文介绍了如何限制索引中ElasticSearch文档的最大数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了Elastic Search(7.x版)集群并创建了一个新索引.我想限制此索引中文档的最大数量.比方说10000个文档.

I've installed an Elastic Search (version 7.x) cluster and created a new index. I want to limit the maximum number of documents in this index. Let's say 10000 documents top.

天真的解决方案是在向其中插入新文档之前查询文档数量.但是此方法可能不准确,而且性能较差(有2个请求...).

The naive solution is to query the number of documents before inserting a new document into it. But this method can be not accurate and also have poor performances (2 requests...).

如何正确执行?

推荐答案

最佳做法是使用

The best practice is to use Index Life Management which is in the Basic License and enabled by default in Elastic v7.3+

您可以设置对文档数量进行翻转操作(我最多放置了5个文档):

You can set a rollover action on the number of document (i put 5 max docs) :

PUT _ilm/policy/my_policy
{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_docs": 5
          }
        }
      }
    }
  }
}

现在,我使用策略 my_policy 创建模板:

Now i create a template with the policy my_policy :

PUT _template/my_template
{
  "index_patterns": [
    "my-index*"
  ],
  "settings": {
    "index.blocks.read_only" : true,
    "index.lifecycle.name": "my_policy",
    "index.lifecycle.rollover_alias": "my-index"
  }
}

请注意,我将设置设置为"index.blocks.read_only":true ,因为在应用过渡时,它将使用read_only参数创建一个新索引.

Note that i put the setting "index.blocks.read_only" : true because when the rollover will be applied it will create a new index with read_only parameter.

现在我可以创建索引了:

Now i can create my index :

PUT my-index-000001
{
  "settings": {
    "index.blocks.read_only": false
  },
  "aliases": {
    "my-index": {
      "is_write_index": true
    }
  }
}

就是这样!在5个文档之后,它将创建一个新的只读索引,并且别名将在此索引上写入.

That's it ! After 5 documents, it will create a new read only index and the alias will be on writing on this one.

您可以通过使用别名对一些新文档进行索引来测试:

You can test by index some new docs with the alias :

PUT my-index/_doc/1
{
  "field" : "value"
}

此外,默认情况下,ilm策略将每10分钟应用一次,您可以更改该策略以便通过以下方式进行测试:

Also, by default the ilm policy will be applied every 10 minutes, you can change that in order to test with :

PUT /_cluster/settings
{
  "persistent": {
    "indices.lifecycle.poll_interval": "5s"
  }
}

这篇关于如何限制索引中ElasticSearch文档的最大数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆