json模板

  1. {
  2. "dynamicTemplates": {
  3. "settings": {
  4. "number_of_shards": 5,
  5. "number_of_replicas": 1,
  6. "max_result_window": 100000000
  7. },
  8. "mappings": {
  9. "properties":{
  10. "model_chinese_name":{
  11. "type":"keyword"
  12. },
  13. "model_identification":{
  14. "type":"keyword"
  15. },
  16. "model_version":{
  17. "type":"keyword"
  18. },
  19. "standard_name":{
  20. "type":"text",
  21. "boost":2,
  22. "fields":{
  23. "text":{
  24. "type":"text",
  25. "search_analyzer":"ik_smart",
  26. "analyzer":"ik_max_word",
  27. "index_options":"offsets"
  28. }
  29. }
  30. },
  31. "standard_identification":{
  32. "type":"keyword"
  33. },
  34. "standard_version":{
  35. "type":"keyword"
  36. }
  37. }
  38. }
  39. }
  40. }

es索引初始化

  1. @Component
  2. @Slf4j
  3. public class IndexInitConstant {
  4. public static JSONArray highLightFieldList ;
  5. public static String dynamicTemplates ;
  6. public static Map<String,Object> settings ;
  7. public static String mappings ;
  8. public static void initEsInitIndexSearch() {
  9. try {
  10. String esInitIndexKeyword = FileUtil.getResourceAsStringByPath("json/modelStandardRelation.json");
  11. JSONObject esInitIndexObject = JSONObject.parseObject(esInitIndexKeyword, Feature.OrderedField);
  12. highLightFieldList = esInitIndexObject.getJSONArray("keyWord");
  13. dynamicTemplates = esInitIndexObject.getJSONObject("dynamicTemplates").toJSONString();
  14. settings = esInitIndexObject.getJSONObject("dynamicTemplates").getJSONObject("settings").getInnerMap();
  15. mappings = esInitIndexObject.getJSONObject("dynamicTemplates").getJSONObject("mappings").toJSONString();
  16. } catch (Exception e) {
  17. //log.error(DgdeviceLoggerHelper.message(ErrorCode.FILE_READ_ERROR, "initDeviceSearch readFile error "), e);
  18. }
  19. }
  20. }
  1. @Slf4j
  2. @Component
  3. public class EsInitIndexInit implements CommandLineRunner {
  4. @Value("${es.url}")
  5. private String esAddress;
  6. @Value("${es.index.modelStandardRelationCatalogCenter}")
  7. private String modelStandardRelationIndex;
  8. @Override
  9. public void run(String... args) throws Exception {
  10. //初始化
  11. EsClientUtil.esAddressNoStatic = esAddress;
  12. IndexInitConstant.initEsInitIndexSearch();
  13. dataAnalysisTaskIndexInit();
  14. }
  15. public synchronized Boolean dataAnalysisTaskIndexInit() {
  16. if(null == esAddress){
  17. log.info(ModelCatalogCenterErrorCode.PARAM_NULL,"es address is null!");
  18. return false;
  19. }
  20. try {
  21. boolean indexExists = EsClientUtil.getInstance().exsitIndex(modelStandardRelationIndex);
  22. if(!indexExists) {
  23. //按照动态模板创建索引,适用ES7.3.2,但是6.5.4不会建成功
  24. EsClientUtil.getInstance().createIndexByDynamicTemplate(modelStandardRelationIndex,
  25. ModelStandardRelationIndexInitConstant.dynamicTemplates);
  26. //此时再判断一次是否存在,不存在,再按照ES6.5.4的方法创建一次
  27. boolean indexExistsAgain = EsClientUtil.getInstance().exsitIndex(modelStandardRelationIndex);
  28. if(!indexExistsAgain) {
  29. EsClientUtil.getInstance().createIndex(modelStandardRelationIndex, "_doc",
  30. ModelStandardRelationIndexInitConstant.mappings, ModelStandardRelationIndexInitConstant.settings);
  31. }
  32. log.info("modelCatalogCenter index create success!!!!indexName:{}",modelStandardRelationIndex);
  33. }
  34. } catch (Exception e) {
  35. log.error("modelCatalogCenter index create error...", e);
  36. return false;
  37. }
  38. return true;
  39. }
  40. }