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()); } }
小白技术分享