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));*/
}
相关推荐
Nuanyt13 分钟前
LeetCode Hot100 刷题笔记 · 普通数组专题
java·笔记·算法·leetcode
cfm_29141 小时前
Leaf-segment号段模式
java
Wang's Blog2 小时前
Java 项目实战: 交友项目 - 发布圈子动态:数据库表和接口文档介绍
java·数据库·交友
paopaokaka_luck2 小时前
智慧电玩店管理系统(空闲机台随机分配、游玩时长概率模拟、积分自动结算、积分兑换闭环、 ECharts经营分析、 腾讯地图选址)
java·人工智能·mysql·echarts·推荐算法
LMT流氓兔2 小时前
第五单元——第9课:防止同一次操作重复执行
java·服务器·数据库·算法·ai·langchain
core5122 小时前
Elasticsearch 车牌号片段搜索:n-gram 倒排索引实战方案
elasticsearch·n-gram·索引·倒排索引·号牌·片段
OxYGC2 小时前
Spring 面经系列 · 第 6 篇:Spring MVC 请求全流程、父子容器与零配置原理
java·spring·mvc
code2cat2 小时前
Java高级篇之JDBC:连接数据库,完成查询、更新与事务
java·开发语言·数据库
bksczm2 小时前
Redis数据结构之string
java·redis·mybatis
এ慕ོ冬℘゜2 小时前
ES Promise.all/ Promise.race
前端·javascript·elasticsearch