java 连接操作elasticsearch(二)
- 判断索引是否存在
/** * 判断索引是否存在 * * @param index * 索引名,类似数据库名 * @return */ public static boolean isIndexExist(String index) { IndicesExistsRequest request = new IndicesExistsRequest(index); IndicesExistsResponse response = ES_Utils.getAdminClient().exists(request).actionGet(); if (response.isExists()) { return true; } return false; }
- 删除索引
/** * 删除索引 * * @param index * 索引名,类似数据库名 * @return */ public static boolean deleteIndex(String index) throws RuntimeException { if (!isIndexExist(index)) { return true; } try { DeleteIndexResponse deleteIndexResponse = ES_Utils.getAdminClient().prepareDelete(index).execute() .actionGet(); boolean isIndexDeleted = deleteIndexResponse.isAcknowledged(); if (isIndexDeleted) { LOGGER.info("索引 " + index + " 删除成功!"); } else { LOGGER.info("索引 " + index + " 删除失败!"); } return deleteIndexResponse.isAcknowledged(); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
- 创建索引
/** * 创建索引 * * @param index * 索引名,类似数据库名 * @param shards * 切片数 * @param replicas * 副本数 * @return */ public static boolean createIndex(String index, int shards, int replicas) { if (isIndexExist(index)) { // 索引库存在则删除索引 deleteIndex(index); } Settings settings = Settings.builder().put("index.number_of_shards", shards) .put("index.number_of_replicas", replicas).build(); CreateIndexResponse createIndexResponse = ES_Utils.getAdminClient().prepareCreate(index.toLowerCase()) .setSettings(settings).execute().actionGet(); boolean isIndexCreated = createIndexResponse.isAcknowledged(); if (isIndexCreated) { LOGGER.info("索引 " + index + " 创建成功!"); } else { LOGGER.info("索引 " + index + " 创建失败!"); } return isIndexCreated; }
- 索引中创建结构mapping
/** * 索引中创建结构mapping * * @param index * 索引名,类似数据库名 * @param type * 类型,类似表名 * @param mapping * 表结构mapping * @return * @throws IOException */ public static boolean putMapping(String index, String type, XContentBuilder mapping) throws IOException { if (!isIndexExist(index)) { LOGGER.info("创建索引库" + index + "mapping" + mapping + "结构失败,索引库不存在!"); return false; } // 添加mapping绑定到 index PutMappingRequest putMappingRequest = Requests.putMappingRequest(index).type(type).source(mapping); PutMappingResponse putMappingResponse = ES_Utils.getAdminClient().putMapping(putMappingRequest).actionGet(); boolean isPutMapping = putMappingResponse.isAcknowledged(); if (isPutMapping) { LOGGER.info("索引: " + index + "类型: " + type + " 的Mapping创建成功!"); } else { LOGGER.info("索引: " + index + "类型: " + type + " 的Mapping创建失败!"); } return isPutMapping; } /** * 索引中创建mapping * * @param mapping * @return * @return */ public static void setTvMapping(String indexName, String typeName) { XContentBuilder mapping = null; try { mapping = XContentFactory.jsonBuilder() .startObject() .startObject("properties") .startObject("id") .field("type", "keyword") .endObject() .startObject("file_name") .field("type", "keyword") .endObject() .startObject("file_time") .field("type", "date") .field("format", "yyyy-MM-dd") .endObject() .startObject("characters") .field("type", "text") .field("analyzer", "ik_smart") .field("search_analyzer", "ik_smart") .endObject() .startObject("language_type") .field("type", "keyword") .endObject() .startObject("create_by") .field("type", "keyword") .endObject() .startObject("create_date") .field("type", "date") .field("format", "yyyy-MM-dd HH:mm:ss") .endObject() .startObject("update_by") .field("type", "keyword") //.field("type", "keyword") .endObject() .startObject("update_date") .field("type", "date") .field("format", "yyyy-MM-dd HH:mm:ss") .endObject() .startObject("remarks") .field("type", "keyword") .endObject() .startObject("del_flag") .field("type", "keyword") .endObject() .startObject("time_long") .field("type", "keyword") .endObject() .startObject("timemark") .field("type", "text") .endObject() .endObject() .endObject(); ES_Utils.putMapping(indexName, typeName, mapping); } catch (IOException e) { e.printStackTrace(); } }
- es中的增删改
/** * 插入单条数据prepareIndex * * @param jsonObject * json对象 * @param index * 索引名,类似数据库名 * @param type * 类型,类似表名 * @param id * 数据id * @return */ public static void insertData(JSONObject jsonObject, String index, String type, String id) { try { IndexResponse indexResponse = ES_Utils.getSingleClient().prepareIndex(index, type, id).setSource(jsonObject) .get(); LOGGER.info("插入数据状态-status:{},id:{}", indexResponse.status(), indexResponse.getId()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } /** * 通过ID 更新数据prepareUpdate * * @param jsonObject * 要增加的数据 * @param index * 索引,类似数据库名 * @param type * 类型,类似表名 * @param id * 数据ID * @return */ public static String updateDataById(JSONObject jsonObject, String index, String type, String id) { try { UpdateResponse updateResponse = ES_Utils.getSingleClient().prepareUpdate(index, type, id).setDoc(jsonObject) .get(); RestStatus status = updateResponse.status(); LOGGER.info("更新数据状态-status:{},id:{}", updateResponse.status(), updateResponse.getId()); return status.toString(); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } /** * 删除索引中的文档prepareDelete * * @param index * 索引,类似数据库名 * @param type * 类型,类似表名 * @param id */ public static void deleteData(String index, String type, String id) { try { DeleteResponse deleteResponse = ES_Utils.getSingleClient().prepareDelete(index.toLowerCase(), type, id) .execute().actionGet(); // RestStatus status = deleteResponse.status(); LOGGER.info("删除数据状态-status:{},id:{}", deleteResponse.status(), deleteResponse.getId()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } }
小白技术分享
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix