spring-data-elasticsearch 使用java api进行动态创建索引

复制代码
private void createIndex(String indexName) {
    IndexCoordinates indexCoordinates = IndexCoordinates.of(indexName);
    IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(indexCoordinates);
    //设置主分片数、副本数、refresh刷新时间间隔
    Map<String, String> setting = new MapBuilder<String, String>()
            .put("index.number_of_shards", "3")
            .put("index.number_of_replicas", "3")
            .put("index.refresh_interval", "1s").map();
    //创建索引,并设置settings参数
    indexOperations.create(Document.from(setting));
    //创建索引mapping方案1:指定以Message类为mapping模板
    indexOperations.putMapping(indexOperations.createMapping(Message.class));
    /* //创建索引mapping方案2:json字符串形式
    String mappingJson = "{\n" +
            "  \"properties\": {\n" +
            "    \"field1\": { \"type\": \"text\" },\n" +
            "    \"field2\": { \"type\": \"keyword\" }\n" +
            "  }\n" +
            "}";
    indexOperations.putMapping(Document.parse(mappingJson));*/
}
相关推荐
吃饱了得干活4 分钟前
缓存与数据库一致性:从理论到实战
java·后端·面试
520拼好饭被践踏12 分钟前
JAVA+Agent学习day22
java·开发语言·后端·学习
笨蛋不要掉眼泪23 分钟前
Java虚拟机:堆的参数配置
java·开发语言·jvm
霸道流氓气质25 分钟前
SpringBoot中使用JasperReports 报表引擎 — 介绍、原理与使用实践
java·spring boot·后端
花生了什么事o31 分钟前
DDD 分层架构:六层分层架构
java·架构·ddd
Y38153266233 分钟前
3 种 SERP 数据处理方案对比:流式 / 批处理 / 缓存
java·spring·缓存
Elastic 中国社区官方博客1 小时前
使用重新设计的 AutoOps 更快地进行 Elasticsearch 问题排查
大数据·运维·前端·人工智能·elasticsearch·搜索引擎·全文检索
qq_150841991 小时前
SQL的insert和update二合一指令
java·数据库·sql
2601_964702891 小时前
Claude Opus 5 API 开发实战:对话、文本生成与结构化输出
java·服务器·前端
Sayuanni%31 小时前
Spring IOC
java·spring·rpc