多子类的设计

顶级接口

 1 /**
 2  * @author caiyike
 3  * @version 1.0
 4  * @date 2019-03-03 16:09
 5  */
 6 public interface ISearchService {
 7 
 8     IndexAliasVO getIndexAlias(String staticIndexName, List<String> staticAllIndexName);
 9 
10     void resetIndex(String indexName, String settingFile, String mappingFile, String type);
11 
12     void refreshIndex(String indexName);
13 
14     boolean indexExists(String indexName);
15 
16     boolean switchIndexAlias(List<String> staticAllIndexName, String staticIndexName, String newIndexName);
17 
18     String indexItem(List<String> staticAllIndexName, String staticIndexName, String indexName, Object doc);
19 
20     boolean indexItems(List<String> staticAllIndexName, String staticIndexName, String indexName, Collection<Object> docs);
21 }


父类(顶级接口实现类)

  1 /**
  2  * @author caiyike
  3  * @version 1.0
  4  * @date 2019-03-03 16:11
  5  */
  6 @Slf4j
  7 public abstract class SearchService implements ISearchService {
  8 
  9     private static Logger logger = LoggerFactory.getLogger(SearchService.class);
 10 
 11     @Autowired
 12     private Client esClient;
 13 
 14     @Autowired
 15     private ElasticsearchTemplate esTemplate;
 16 
 17     @Override
 18     public IndexAliasVO getIndexAlias(String staticIndexName, List<String> staticAllIndexName) {
 19         GetAliasesAction act = GetAliasesAction.INSTANCE;
 20         GetAliasesRequestBuilder req = act.newRequestBuilder(esClient);
 21         req.setAliases(staticIndexName);
 22         ImmutableOpenMap<String, List<AliasMetaData>> resp = req.execute().actionGet().getAliases();
 23         IndexAliasVO vo = new IndexAliasVO();
 24         vo.setAlias(staticIndexName);
 25 
 26         List<String> candidateIndices = new ArrayList<>(staticAllIndexName);
 27         vo.setCandidateIndices(candidateIndices);
 28         for (String index : staticAllIndexName) {
 29             if (SoaHelper.isNotNullOrEmpty(resp.get(index))) {
 30                 vo.setCurrentIndex(index);
 31                 candidateIndices.remove(index);
 32                 break;
 33             }
 34         }
 35         return vo;
 36     }
 37 
 38     @Override
 39     public void resetIndex(String indexName, String settingFile, String mappingFile, String type) {
 40         if (esTemplate.indexExists(indexName))
 41             esTemplate.deleteIndex(indexName);
 42 
 43         createIndexIfNotExists(indexName, settingFile, mappingFile, type);
 44     }
 45 
 46 
 47     @Override
 48     public void refreshIndex(String indexName) {
 49         esTemplate.refresh(indexName);
 50     }
 51 
 52     @Override
 53     public boolean indexExists(String indexName) {
 54         return esTemplate.indexExists(indexName);
 55     }
 56 
 57     @Override
 58     public boolean switchIndexAlias(List<String> staticAllIndexName, String staticIndexName, String newIndexName) {
 59         checkIndexName(staticAllIndexName, staticIndexName, newIndexName, false);
 60 
 61         if (!esTemplate.indexExists(newIndexName)) {
 62             throw new RuntimeException(String.format("index not exists: '%s'", newIndexName));
 63         }
 64 
 65         IndexAliasVO vo = getIndexAlias(staticIndexName, staticAllIndexName);
 66 
 67         if (newIndexName.equals(vo.getCurrentIndex()))
 68             return true;
 69 
 70         refreshIndex(newIndexName);
 71 
 72         IndicesAliasesRequestBuilder req = esClient.admin().indices().prepareAliases();
 73         if (GeneralHelper.isStrNotEmpty(vo.getCurrentIndex())) {
 74             req.removeAlias(vo.getCurrentIndex(), staticIndexName);
 75         }
 76         return req.addAlias(newIndexName, staticIndexName).execute().actionGet().isAcknowledged();
 77     }
 78 
 79 
 80     @Override
 81     public String indexItem(List<String> staticAllIndexName, String staticIndexName, String indexName, Object doc) {
 82         checkIndexName(staticAllIndexName, staticIndexName, indexName, true);
 83 
 84         IndexQuery req = new IndexQuery();
 85         req.setIndexName(indexName);
 86         req.setObject(doc);
 87         return esTemplate.index(req);
 88     }
 89 
 90 
 91     @Override
 92     public boolean indexItems(List<String> staticAllIndexName, String staticIndexName, String indexName, Collection<Object> docs) {
 93 
 94         try {
 95             checkIndexName(staticAllIndexName, staticIndexName, indexName, true);
 96 
 97             if (SoaHelper.isNullOrEmpty(docs))
 98                 return false;
 99 
100             List<IndexQuery> list = new ArrayList<>();
101 
102             for (Object doc : docs) {
103                 IndexQuery req = new IndexQuery();
104                 req.setIndexName(indexName);
105                 req.setObject(doc);
106                 list.add(req);
107             }
108 
109             if (CollectionUtils.isNotEmpty(list)) {
110                 logger.info("导入es数据量: {}", list.size());
111                 logger.info("导入批次item起始: {}----item结束: {}", list.get(0).getObject().toString(), list.get(list.size() - 1).getObject().toString());
112             }
113 
114             esTemplate.bulkIndex(list);
115         } catch (RuntimeException e) {
116             logger.info("导入商品入es失败index: {}", indexName);
117 
118             e.printStackTrace();
119         }
120         return true;
121     }
122 
123 
124     private void createIndexIfNotExists(String indexName, String settingFile, String mappingFile, String type) {
125         if (!esTemplate.indexExists(indexName)) {
126             String setting = ElasticsearchTemplate.readFileFromClasspath(settingFile);
127             String mapping = ElasticsearchTemplate.readFileFromClasspath(mappingFile);
128 
129             try {
130                 esTemplate.createIndex(indexName, setting);
131                 esTemplate.putMapping(indexName, type, mapping);
132             } catch (Exception e) {
133                 if (esTemplate.indexExists(indexName))
134                     esTemplate.deleteIndex(indexName);
135 
136                 String msg = MessageFormat.format("create index {} fail", indexName);
137                 log.error(msg, e);
138                 throw new RuntimeException(msg, e);
139             }
140         }
141     }
142 
143     private void checkIndexName(List<String> staticAllIndexName, String staticIndexName, String indexName, boolean withAliasName) {
144         boolean isOK = staticAllIndexName.contains(indexName);
145 
146         if (!isOK && withAliasName)
147             isOK = staticIndexName.equals(indexName);
148 
149         if (!isOK)
150             throw new RuntimeException(String.format("invald indexName: '%s'", indexName));
151     }
152 }


子类接口

 

 1 /**
 2  * @author caiyike
 3  * @version 1.0
 4  * @date 2019-03-03 17:05
 5  */
 6 public interface IOriginalItemSearchService extends ISearchService {
 7 
 8     IndexAliasVO getIndexAlias();
 9     void resetIndex(String indexName);
10     void refreshIndex();
11     void refreshIndex(String indexName);
12     boolean indexExists(String indexName);
13     boolean switchIndexAlias(String newIndexName);
14 
15 }


子类实现

 1 /**
 2  * @author caiyike
 3  * @version 1.0
 4  * @date 2019-03-03 17:08
 5  */
 6 public class OriginalItemSearchService extends SearchService implements IOriginalItemSearchService {
 7 
 8     private static final String INDEX_NAME = "";
 9 
10     private static final String INDEX_NAME_A = "";
11 
12     private static final String INDEX_NAME_B = "";
13 
14     private static final List<String> ALL_INDEX_NAME = Arrays.asList(INDEX_NAME_A, INDEX_NAME_B);
15 
16     public static final String INDEX_SETTING_FILE = "xxx-setting.json";
17 
18     public static final String INDEX_ITEM_TYPE_MAPPING_FILE = "xxx-mapping.json";
19 
20     public static final String ITEM_TYPE = "item";
21 
22     @Override
23     public IndexAliasVO getIndexAlias() {
24         return super.getIndexAlias(INDEX_NAME, ALL_INDEX_NAME);
25     }
26 
27     @Override
28     public void resetIndex(String indexName) {
29         super.resetIndex(indexName, INDEX_SETTING_FILE, INDEX_ITEM_TYPE_MAPPING_FILE, ITEM_TYPE);
30     }
31 
32 
33     @Override
34     public void refreshIndex() {
35         super.refreshIndex(INDEX_NAME);
36     }
37 
38     @Override
39     public boolean switchIndexAlias(String newIndexName) {
40         return super.switchIndexAlias(ALL_INDEX_NAME, INDEX_NAME, newIndexName);
41     }
42 
43 
44 }

 

posted @ 2019-03-03 17:46  Joke科  阅读(251)  评论(0编辑  收藏  举报