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));*/
}
相关推荐
晚安code33 分钟前
Java编程规范避坑指南:阿里开发手册15条强制规约实战解析
java·后端
Elasticsearch1 小时前
一个 ES|QL 查询替代两个:`WHERE IN` subquery 取代 Elasticsearch 中的复制粘贴循环
elasticsearch
小蒜学长1 小时前
“守望自然”招募志愿者环保行动网站的设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端
萧瑟余晖2 小时前
Java深入解析篇十八之响应式编程
java·开发语言
IKUN家族2 小时前
Spring MVC(五)
java·spring·mvc
weixin_BYSJ19872 小时前
【java项目分享】springboot阅读推荐平台10600
java·javascript·spring boot·python·django·flask·php
小 黄 鸡2 小时前
JVM 核心总结
java
一路向北North2 小时前
Spring AI(11) :ChatPDF-向量数据库、PDF处理、向量写入和向量搜索
数据库·人工智能·spring
Data_Journal3 小时前
掌握网页抓取中的分页:完整指南
java·服务器·前端
莫逸风3 小时前
【AgentScope 2.0】05-文件系统(Filesystem)详解
java·ai·agent·springai·agentscope