json模板
{ "dynamicTemplates": { "settings": { "number_of_shards": 5, "number_of_replicas": 1, "max_result_window": 100000000 }, "mappings": { "properties":{ "model_chinese_name":{ "type":"keyword" }, "model_identification":{ "type":"keyword" }, "model_version":{ "type":"keyword" }, "standard_name":{ "type":"text", "boost":2, "fields":{ "text":{ "type":"text", "search_analyzer":"ik_smart", "analyzer":"ik_max_word", "index_options":"offsets" } } }, "standard_identification":{ "type":"keyword" }, "standard_version":{ "type":"keyword" } } } }}
es索引初始化
@Component@Slf4jpublic class IndexInitConstant { public static JSONArray highLightFieldList ; public static String dynamicTemplates ; public static Map<String,Object> settings ; public static String mappings ; public static void initEsInitIndexSearch() { try { String esInitIndexKeyword = FileUtil.getResourceAsStringByPath("json/modelStandardRelation.json"); JSONObject esInitIndexObject = JSONObject.parseObject(esInitIndexKeyword, Feature.OrderedField); highLightFieldList = esInitIndexObject.getJSONArray("keyWord"); dynamicTemplates = esInitIndexObject.getJSONObject("dynamicTemplates").toJSONString(); settings = esInitIndexObject.getJSONObject("dynamicTemplates").getJSONObject("settings").getInnerMap(); mappings = esInitIndexObject.getJSONObject("dynamicTemplates").getJSONObject("mappings").toJSONString(); } catch (Exception e) { //log.error(DgdeviceLoggerHelper.message(ErrorCode.FILE_READ_ERROR, "initDeviceSearch readFile error "), e); } }}
@Slf4j@Componentpublic class EsInitIndexInit implements CommandLineRunner { @Value("${es.url}") private String esAddress; @Value("${es.index.modelStandardRelationCatalogCenter}") private String modelStandardRelationIndex; @Override public void run(String... args) throws Exception { //初始化 EsClientUtil.esAddressNoStatic = esAddress; IndexInitConstant.initEsInitIndexSearch(); dataAnalysisTaskIndexInit(); } public synchronized Boolean dataAnalysisTaskIndexInit() { if(null == esAddress){ log.info(ModelCatalogCenterErrorCode.PARAM_NULL,"es address is null!"); return false; } try { boolean indexExists = EsClientUtil.getInstance().exsitIndex(modelStandardRelationIndex); if(!indexExists) { //按照动态模板创建索引,适用ES7.3.2,但是6.5.4不会建成功 EsClientUtil.getInstance().createIndexByDynamicTemplate(modelStandardRelationIndex, ModelStandardRelationIndexInitConstant.dynamicTemplates); //此时再判断一次是否存在,不存在,再按照ES6.5.4的方法创建一次 boolean indexExistsAgain = EsClientUtil.getInstance().exsitIndex(modelStandardRelationIndex); if(!indexExistsAgain) { EsClientUtil.getInstance().createIndex(modelStandardRelationIndex, "_doc", ModelStandardRelationIndexInitConstant.mappings, ModelStandardRelationIndexInitConstant.settings); } log.info("modelCatalogCenter index create success!!!!indexName:{}",modelStandardRelationIndex); } } catch (Exception e) { log.error("modelCatalogCenter index create error...", e); return false; } return true; }}