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));*/
}
相关推荐
摇滚侠几秒前
搭建前端开发环境 安装 nodejs 设置淘宝镜像 最简化最标准版本 不使用 NVM NVM 高版本无法安装低版本 nodejs
java·开发语言·node.js
花千树-0107 分钟前
兼容 ThreadLocal 的用户上下文透传方案:WebFlux 项目改造实践
java·spring boot·servlet·jetty
无人机9012 小时前
Delphi 网络编程实战:TIdTCPClient 与 TIdTCPServer 类深度解析
java·开发语言·前端
lclcooky2 小时前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
TeDi TIVE2 小时前
Spring Cloud Gateway
java
:mnong3 小时前
Superpowers 项目设计分析
java·c语言·c++·python·c#·php·skills
扶苏-su3 小时前
Java--获取 Class 类对象
java·开发语言
东离与糖宝3 小时前
LangChain4j vs Spring AI:最新对比,Java企业级Agent开发
java·人工智能
96774 小时前
C++多线程2 如何优雅地锁门 (lock_guard) 多线程里的锁的种类
java·开发语言·c++
老衲提灯找美女4 小时前
数据库事务
java·大数据·数据库