Java Api实现:
@Slf4j @Service public class ESMappingService { @Resource protected RestHighLevelClient client; public String[] getIndices(String index){ GetIndexRequest request = new GetIndexRequest(index); //允许不存在的索引 IndicesOptions indicesOptions = IndicesOptions.fromOptions(true, true, true, false); request.indicesOptions(indicesOptions); GetIndexResponse response = null; String[] indices = new String[]{}; try { response = client.indices().get(request, RequestOptions.DEFAULT); } catch (Exception e) { log.error(" ESMappingService getIndices Cause:{},Message:{}", e.getCause(),e.getMessage()); } if (response != null) { indices = response.getIndices(); } return indices; } public String[] getAllIndices(){ GetIndexRequest request = new GetIndexRequest(); GetIndexResponse response = null; String[] indices = new String[]{}; try { response = client.indices().get(request, RequestOptions.DEFAULT); } catch (Exception e) { log.error(" ESMappingService getAllIndices Cause:{},Message:{}", e.getCause(),e.getMessage()); } if (response != null) { indices = response.getIndices(); } return indices; } public ListgetAllTemplates(){ GetIndexTemplatesRequest templatesRequest = new GetIndexTemplatesRequest(); GetIndexTemplatesResponse templatesResponse = null; List list = new ArrayList<>(); try { templatesResponse = client.indices().getIndexTemplate(templatesRequest, RequestOptions.DEFAULT); } catch (Exception e) { log.error(" ESCheckMappingService getAllTemplates query error ", e); } if(templatesResponse == null){ return list; } return templatesResponse.getIndexTemplates(); } public boolean isTemplateExist(String template){ List list = getAllTemplates(); if(list.isEmpty()){ return false; } List names = list.stream().map(IndexTemplatemetadata::name).collect(Collectors.toList()); return names.contains(template); } public MappingmetaData getIndexMapping(String index){ GetMappingsRequest getMappingsRequest = new GetMappingsRequest(); //允许不存在的索引 IndicesOptions indicesOptions = IndicesOptions.fromOptions(true, true, true, false); getMappingsRequest.indicesOptions(indicesOptions); getMappingsRequest.indices(index); GetMappingsResponse getMappingsResponse = null; try { getMappingsResponse = client.indices().getMapping(getMappingsRequest, RequestOptions.DEFAULT); } catch (Exception e) { log.error(" ESCheckMappingService getIndexMapping query error ", e); } if(getMappingsResponse == null){ return null; } return getMappingsResponse.mappings().get(index); } public IndexTemplatemetaData getTemplateMapping(String template){ GetIndexTemplatesRequest templatesRequest = new GetIndexTemplatesRequest(template); GetIndexTemplatesResponse templatesResponse = null; try { templatesResponse = client.indices().getIndexTemplate(templatesRequest, RequestOptions.DEFAULT); } catch (Exception e) { log.error(" ESCheckMappingService getTemplateMapping query error ", e); } if(templatesResponse == null){ return null; } return templatesResponse.getIndexTemplates().get(0); } public boolean isIndexFieldExist(String field, String index){ MappingmetaData data = getIndexMapping(index); if(null == data){ return false; } Map properties = (Map ) data.getSourceAsMap().get(ESIndexConstant.PROPERTIES); return properties.containsKey(field); } public boolean isTemplateFieldExist(String field, String template){ IndexTemplatemetaData templatemetaData = getTemplateMapping(template); if(null == templatemetaData){ return false; } Map properties = (Map ) templatemetaData.mappings().getSourceAsMap().get(ESIndexConstant.PROPERTIES); return properties.containsKey(field); } public boolean putMapping(String index, Map map){ PutMappingRequest putMappingRequest = new PutMappingRequest(index); putMappingRequest.source(map); AcknowledgedResponse acknowledgedResponse = null; try { acknowledgedResponse = client.indices().putMapping(putMappingRequest, RequestOptions.DEFAULT); } catch (Exception e) { log.error(" ESCheckMappingService putMapping update error ", e); } if(acknowledgedResponse == null){ return false; } return acknowledgedResponse.isAcknowledged(); } public boolean putTemplate(String template, Map map){ PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest(template); templateRequest.source(map); AcknowledgedResponse acknowledgedResponse = null; try { acknowledgedResponse = client.indices().putTemplate(templateRequest, RequestOptions.DEFAULT); } catch (Exception e) { log.error(" ESCheckMappingService putTemplate update error ", e); } if(acknowledgedResponse == null){ return false; } return acknowledgedResponse.isAcknowledged(); } public Map addFieldToIndex(String field, String type, String format){ Map map = new HashMap<>(); Map properties = new HashMap<>(); properties.put(field, getFieldMap(type, format)); map.put(ESIndexConstant.PROPERTIES, properties); return map; } private Map getFieldMap(String type, String format){ Map fieldMap = new HashMap<>(); fieldMap.put("type", type); if(null != format){ fieldMap.put("format", format); } return fieldMap; } public Map addFieldToIndex(String field, String type){ return addFieldToIndex(field, type, null); } public Map addFieldToTemplate(String templateName, String field, String type, String format){ //获取模板现有属性信息 IndexTemplatemetaData templatemetaData = this.getTemplateMapping(templateName); //获取模板所有字段属性 Map properties = (Map ) templatemetaData.mappings().getSourceAsMap().get(ESIndexConstant.PROPERTIES); //增加字段属性 properties.put(field, getFieldMap(type, format)); //组装新的模板信息 return getTemplateMap(properties, templatemetaData); } public Map addFieldToTemplate(String templateName, String field, String type){ return addFieldToTemplate(templateName, field, type, null); } public Map addFieldToIndexBatch(List list){ Map map = new HashMap<>(); Map properties = new HashMap<>(); list.forEach(e -> properties.put(e.getField(), getFieldMap(e.getType(), e.getField()))); map.put(ESIndexConstant.PROPERTIES, properties); return map; } public Map addFieldToTemplateBatch(String templateName, List list){ //获取模板现有属性信息 IndexTemplatemetaData templatemetaData = this.getTemplateMapping(templateName); //获取模板所有字段属性 Map properties = (Map ) templatemetaData.mappings().getSourceAsMap().get(ESIndexConstant.PROPERTIES); //增加字段属性 list.forEach(e -> properties.put(e.getField(), getFieldMap(e.getType(), e.getField()))); //组装新的模板信息 return getTemplateMap(properties, templatemetaData); } private Map getTemplateMap(Map properties, IndexTemplatemetaData templatemetaData){ Map templateMap = new HashMap<>(); Map pro = new HashMap<>(); pro.put(ESIndexConstant.PROPERTIES, properties); templateMap.put(ESIndexConstant.MAPPINGS, pro); //指定模板范围为原有,必要 *** 作 templateMap.put(ESIndexConstant.INDEX_PATTERNS, templatemetaData.patterns()); //原有设置属性信息 Map settingsMap = templatemetaData.settings().getAsGroups(); Map setMap = new HashMap<>(); settingsMap.forEach((k,v) -> setMap.put(k, JSON.parseObject(v.toString()))); templateMap.put(ESIndexConstant.SETTINGS, setMap); return templateMap; } public boolean deleteIndices(String... indices){ DeleteIndexRequest request = new DeleteIndexRequest(indices); AcknowledgedResponse response = null; try { response = client.indices().delete(request, RequestOptions.DEFAULT); } catch (Exception e) { log.error(" ESMappingService deleteIndices error", e); } if(response == null){ return false; } return response.isAcknowledged(); } public boolean deleteTemplate(String template){ DeleteIndexTemplateRequest request = new DeleteIndexTemplateRequest(template); AcknowledgedResponse response = null; try { response = client.indices().deleteTemplate(request, RequestOptions.DEFAULT); } catch (Exception e) { log.error(" ESMappingService deleteTemplate error", e); } if(response == null){ return false; } return response.isAcknowledged(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)