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));*/
}
相关推荐
zzb15803 小时前
RAG from Scratch-优化-query
java·数据库·人工智能·后端·spring·mybatis
wuqingshun3141593 小时前
如何停止一个正在退出的线程
java·开发语言·jvm
Barkamin4 小时前
队列的实现(Java)
java·开发语言
骇客野人5 小时前
自己手搓磁盘清理工具(JAVA版)
java·开发语言
J2虾虾5 小时前
在SpringBoot中使用Druid
java·spring boot·后端·druid
清风徐来QCQ5 小时前
Java笔试总结一
java·开发语言
Elastic 中国社区官方博客5 小时前
Elastic 为什么捐赠其 OpenTelemetry PHP 发行版
大数据·开发语言·elasticsearch·搜索引擎·信息可视化·全文检索·php
10Eugene5 小时前
C++/Qt自制八股文
java·开发语言·c++
程序员小假5 小时前
为什么要有 time _wait 状态,服务端这个状态过多是什么原因?
java·后端
kuntli6 小时前
p命名空间注入原理详解
spring