Elasticsearch索引数量限制

Elasticsearch的索引可以无限创建吗?

版本环境

Elasticsearch 7.9.1

验证

Elasticsearch启动

Elasticsearch启动采用默认配置
在这里插入图片描述

代码

使用SpringBoot编写测试代码,对ES进行索引创建测试。

SpringBoot连接配置
es:
  host: 127.0.0.1
  port: 9200
  scheme: http
测试类代码

单机测试,设置每个索引占用4个分片,0个副本,先创建1000个索引进行测试,是否能创建成功。

@SpringBootTest
@Slf4j
class ElasticsearchApplicationTests {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    @Test
    public void test() {
        for (int i = 1; i <= 1000; i++) {
            createIndex("data_" + i);
            log.info("创建索引:data_" + i + ", 完成。");
        }
    }

    /**
     * 创建索引
     * @param idxName
     */
    public void createIndex(String idxName){
        try {
            if (!this.indexExist(idxName)) {
                log.error(" idxName={} 已经存在",idxName);
                return;
            }
            CreateIndexRequest request = new CreateIndexRequest(idxName);
            buildSetting(request);
//            request.mapping(idxSQL, XContentType.JSON);
//            request.settings() 手工指定Setting
            CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
            if (!res.isAcknowledged()) {
                throw new RuntimeException("初始化失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    /**
     * 判断索引是否存在
     * @param idxName
     * @return
     * @throws Exception
     */
    public boolean indexExist(String idxName) throws Exception {
        GetIndexRequest request = new GetIndexRequest(idxName);
        request.local(false);
        request.humanReadable(true);
        request.includeDefaults(false);
        request.indicesOptions(IndicesOptions.lenientExpandOpen());
        return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
    }

    /**
     * 索引设置4个分片,0个副本
     * @param request
     */
    public void buildSetting(CreateIndexRequest request){
        request.settings(Settings.builder().put("index.number_of_shards",4)
                .put("index.number_of_replicas",0));
    }

}

运行结果

直接运行测试类,运行结果:
在这里插入图片描述
当创建至250个索引后报错,创建失败,也就是在创建第251个索引时失败,具体报错内容如下:

ElasticsearchStatusException[Elasticsearch exception [type=validation_exception, reason=Validation Failed: 1: this action would add [4] total shards, but this cluster currently has [1000]/[1000] maximum shards open;]]
	at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
	at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1793)
	...

报错信息提示:创建新索引时,分片数不够用,导致创建失败。

官方文档

在这里插入图片描述
ES7官方默认设置的索引分片数是1000,当分片被占满后,创建新索引失败,示例中在创建完250个索引后1000个分片已经占用完(250*4=1000)。

修改

修改elasticsearch.yml修改默认配置,根据实际需求修改分片限制数,cluster.max_shards_per_node: 10000,修改后重启ES。
在这里插入图片描述
再次运行测试类,从251开始创建索引,创建成功。
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值