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开始创建索引,创建成功。
在这里插入图片描述
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐