Showing
1 changed file
with
78 additions
and
59 deletions
@@ -10,7 +10,6 @@ import java.util.regex.Pattern; | @@ -10,7 +10,6 @@ import java.util.regex.Pattern; | ||
10 | 10 | ||
11 | import javax.annotation.PostConstruct; | 11 | import javax.annotation.PostConstruct; |
12 | 12 | ||
13 | -import org.apache.commons.lang.StringUtils; | ||
14 | import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse; | 13 | import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse; |
15 | import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken; | 14 | import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken; |
16 | import org.elasticsearch.action.bulk.BulkResponse; | 15 | import org.elasticsearch.action.bulk.BulkResponse; |
@@ -57,7 +56,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -57,7 +56,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
57 | private IYohoIndexDataLoader yohoIndexDataLoader; | 56 | private IYohoIndexDataLoader yohoIndexDataLoader; |
58 | @Autowired | 57 | @Autowired |
59 | private IndexRebuildListenerMgr indexRebuildListenerMgr; | 58 | private IndexRebuildListenerMgr indexRebuildListenerMgr; |
60 | - | 59 | + |
61 | ApplicationEventPublisher publisher; | 60 | ApplicationEventPublisher publisher; |
62 | 61 | ||
63 | @Override | 62 | @Override |
@@ -132,11 +131,15 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -132,11 +131,15 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
132 | } | 131 | } |
133 | IElasticsearchClient client = index.getIndexClient(); | 132 | IElasticsearchClient client = index.getIndexClient(); |
134 | // 获取真实的索引名 | 133 | // 获取真实的索引名 |
135 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
136 | - if (StringUtils.isBlank(realIndexName)) { | 134 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
135 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
136 | + return false; | ||
137 | + } | ||
138 | + if (realIndexNames.size() != 1) { | ||
139 | + client.deleteIndexs(realIndexNames); | ||
137 | return false; | 140 | return false; |
138 | } | 141 | } |
139 | - return client.indexExists(realIndexName); | 142 | + return client.indexExists(realIndexNames.get(0)); |
140 | } | 143 | } |
141 | 144 | ||
142 | @Override | 145 | @Override |
@@ -153,8 +156,8 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -153,8 +156,8 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
153 | } | 156 | } |
154 | IElasticsearchClient client = index.getIndexClient(); | 157 | IElasticsearchClient client = index.getIndexClient(); |
155 | // 1、判断旧索引是否存在,如果存在且不是强制更新,则直接返回 | 158 | // 1、判断旧索引是否存在,如果存在且不是强制更新,则直接返回 |
156 | - String oldRealIndexName = yohoIndexHelper.getRealIndexName(alias, client); | ||
157 | - if (StringUtils.isNotBlank(oldRealIndexName) && !force) { | 159 | + List<String> oldRealIndexNames = yohoIndexHelper.getRealIndexNames(alias, client.getRealNameToAliasesMap()); |
160 | + if (oldRealIndexNames != null && oldRealIndexNames.size() == 1 && !force) { | ||
158 | return null; | 161 | return null; |
159 | } | 162 | } |
160 | // 2、生成真实索引名称 | 163 | // 2、生成真实索引名称 |
@@ -164,17 +167,17 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -164,17 +167,17 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
164 | final String mappingContent = index.getMappingContent(); | 167 | final String mappingContent = index.getMappingContent(); |
165 | client.createIndex(newRealIndexName, yohoIndexName, settings, mappingContent); | 168 | client.createIndex(newRealIndexName, yohoIndexName, settings, mappingContent); |
166 | // 4、删除旧索引别名 | 169 | // 4、删除旧索引别名 |
167 | - if (oldRealIndexName != null) { | ||
168 | - client.removeAlias(oldRealIndexName, alias); | 170 | + if (oldRealIndexNames != null && !oldRealIndexNames.isEmpty()) { |
171 | + client.removeAlias(oldRealIndexNames, alias); | ||
169 | } | 172 | } |
170 | // 5、添加新索引别名【如果存在一个名字为alias的真实索引,则直接删除】 | 173 | // 5、添加新索引别名【如果存在一个名字为alias的真实索引,则直接删除】 |
171 | - if(client.indexExists(alias)){ | 174 | + if (client.indexExists(alias)) { |
172 | client.deleteIndex(alias); | 175 | client.deleteIndex(alias); |
173 | } | 176 | } |
174 | client.addAlias(newRealIndexName, alias); | 177 | client.addAlias(newRealIndexName, alias); |
175 | // 7、删除旧索引 | 178 | // 7、删除旧索引 |
176 | - if (oldRealIndexName != null) { | ||
177 | - client.deleteIndex(oldRealIndexName); | 179 | + if (oldRealIndexNames != null && !oldRealIndexNames.isEmpty()) { |
180 | + client.deleteIndexs(oldRealIndexNames); | ||
178 | } | 181 | } |
179 | return newRealIndexName; | 182 | return newRealIndexName; |
180 | } | 183 | } |
@@ -186,11 +189,11 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -186,11 +189,11 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
186 | return; | 189 | return; |
187 | } | 190 | } |
188 | IElasticsearchClient client = index.getIndexClient(); | 191 | IElasticsearchClient client = index.getIndexClient(); |
189 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
190 | - if (StringUtils.isBlank(realIndexName)) { | 192 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
193 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
191 | return; | 194 | return; |
192 | } | 195 | } |
193 | - client.deleteIndex(realIndexName); | 196 | + client.deleteIndexs(realIndexNames); |
194 | } | 197 | } |
195 | 198 | ||
196 | @Override | 199 | @Override |
@@ -222,7 +225,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -222,7 +225,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
222 | } | 225 | } |
223 | // 3)如果别名中不包含yohoIndexName,则视为失败的临时索引 | 226 | // 3)如果别名中不包含yohoIndexName,则视为失败的临时索引 |
224 | List<String> aliasNames = entry.getValue(); | 227 | List<String> aliasNames = entry.getValue(); |
225 | - if (!aliasNames.contains(yohoIndexName)) { | 228 | + if (aliasNames == null || !aliasNames.contains(yohoIndexName)) { |
226 | tempRealIndexNames.add(realIndexName); | 229 | tempRealIndexNames.add(realIndexName); |
227 | continue; | 230 | continue; |
228 | } | 231 | } |
@@ -241,11 +244,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -241,11 +244,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
241 | return; | 244 | return; |
242 | } | 245 | } |
243 | IElasticsearchClient client = index.getIndexClient(); | 246 | IElasticsearchClient client = index.getIndexClient(); |
244 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
245 | - if (StringUtils.isBlank(realIndexName)) { | 247 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
248 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
246 | return; | 249 | return; |
247 | } | 250 | } |
248 | - client.refreshIndex(realIndexName); | 251 | + for (String realIndexName : realIndexNames) { |
252 | + client.refreshIndex(realIndexName); | ||
253 | + } | ||
249 | } | 254 | } |
250 | 255 | ||
251 | @Override | 256 | @Override |
@@ -255,11 +260,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -255,11 +260,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
255 | return; | 260 | return; |
256 | } | 261 | } |
257 | IElasticsearchClient client = index.getIndexClient(); | 262 | IElasticsearchClient client = index.getIndexClient(); |
258 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
259 | - if (StringUtils.isBlank(realIndexName)) { | 263 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
264 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
260 | return; | 265 | return; |
261 | } | 266 | } |
262 | - client.closeIndex(realIndexName); | 267 | + for (String realIndexName : realIndexNames) { |
268 | + client.closeIndex(realIndexName); | ||
269 | + } | ||
263 | } | 270 | } |
264 | 271 | ||
265 | @Override | 272 | @Override |
@@ -269,11 +276,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -269,11 +276,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
269 | return; | 276 | return; |
270 | } | 277 | } |
271 | IElasticsearchClient client = index.getIndexClient(); | 278 | IElasticsearchClient client = index.getIndexClient(); |
272 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
273 | - if (StringUtils.isBlank(realIndexName)) { | 279 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
280 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
274 | return; | 281 | return; |
275 | } | 282 | } |
276 | - client.flushIndex(realIndexName); | 283 | + for (String realIndexName : realIndexNames) { |
284 | + client.flushIndex(realIndexName); | ||
285 | + } | ||
277 | } | 286 | } |
278 | 287 | ||
279 | @Override | 288 | @Override |
@@ -287,34 +296,38 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -287,34 +296,38 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
287 | // 1、获取真实索引名与别名的对应关系 | 296 | // 1、获取真实索引名与别名的对应关系 |
288 | Map<String, List<String>> realNameToAliasesMap = client.getRealNameToAliasesMap(); | 297 | Map<String, List<String>> realNameToAliasesMap = client.getRealNameToAliasesMap(); |
289 | 298 | ||
290 | - // 2、获取真实的索引名称 | ||
291 | - String oldIndexRealName = yohoIndexHelper.getRealIndexName(yohoIndexName, realNameToAliasesMap); | ||
292 | - String newIndexRealName = yohoIndexHelper.getRealIndexName(yohoTemplateIndexName, realNameToAliasesMap); | ||
293 | - if (newIndexRealName == null) { | 299 | + // 2、获取新的索引名称[考虑一个别名有多个索引的情况] |
300 | + List<String> newIndexRealNames = yohoIndexHelper.getRealIndexNames(yohoTemplateIndexName, realNameToAliasesMap); | ||
301 | + if (newIndexRealNames == null || newIndexRealNames.isEmpty()) { | ||
302 | + return; | ||
303 | + } | ||
304 | + if (newIndexRealNames.size() != 1) { | ||
305 | + client.deleteIndexs(newIndexRealNames); | ||
294 | return; | 306 | return; |
295 | } | 307 | } |
296 | 308 | ||
297 | - // 3、获取有货索引的别名 | ||
298 | - String yohoIndexAliasName = yohoIndexName; | 309 | + // 3、获取老的索引名称列表 |
310 | + List<String> oldIndexRealNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, realNameToAliasesMap); | ||
299 | 311 | ||
300 | - // 4. 把老索引的索引名先删除 | ||
301 | - if (oldIndexRealName != null) { | ||
302 | - client.removeAlias(oldIndexRealName, yohoIndexAliasName); | 312 | + // 4. 把全部老索引的别名全部删除 |
313 | + String yohoIndexAliasName = yohoIndexName; | ||
314 | + if (oldIndexRealNames != null && !oldIndexRealNames.isEmpty()) { | ||
315 | + client.removeAlias(oldIndexRealNames, yohoIndexAliasName); | ||
303 | } | 316 | } |
304 | 317 | ||
305 | - // 5. 把新索引的别名全部删除 | 318 | + // 5. 把唯一的一个新索引的别名全部删除 |
319 | + String newIndexRealName = newIndexRealNames.get(0); | ||
306 | List<String> aliasList = realNameToAliasesMap.get(newIndexRealName); | 320 | List<String> aliasList = realNameToAliasesMap.get(newIndexRealName); |
307 | String[] aliases = aliasList.toArray(new String[aliasList.size()]); | 321 | String[] aliases = aliasList.toArray(new String[aliasList.size()]); |
308 | client.removeAlias(newIndexRealName, aliases); | 322 | client.removeAlias(newIndexRealName, aliases); |
309 | 323 | ||
310 | - // 6. 把老索引的索引名加到新索引上面 | 324 | + // 6. 把别名加到新索引上面 |
311 | client.addAlias(newIndexRealName, yohoIndexAliasName); | 325 | client.addAlias(newIndexRealName, yohoIndexAliasName); |
312 | 326 | ||
313 | // 7. 删除老索引 | 327 | // 7. 删除老索引 |
314 | - if (oldIndexRealName != null) { | ||
315 | - client.deleteIndex(oldIndexRealName); | 328 | + if (oldIndexRealNames != null && !oldIndexRealNames.isEmpty()) { |
329 | + client.deleteIndexs(oldIndexRealNames); | ||
316 | } | 330 | } |
317 | - | ||
318 | } | 331 | } |
319 | 332 | ||
320 | @Override | 333 | @Override |
@@ -324,11 +337,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -324,11 +337,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
324 | return; | 337 | return; |
325 | } | 338 | } |
326 | IElasticsearchClient client = index.getIndexClient(); | 339 | IElasticsearchClient client = index.getIndexClient(); |
327 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
328 | - if (StringUtils.isBlank(realIndexName)) { | 340 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
341 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
329 | return; | 342 | return; |
330 | } | 343 | } |
331 | - client.optimize(realIndexName); | 344 | + for (String realIndexName : realIndexNames) { |
345 | + client.optimize(realIndexName); | ||
346 | + } | ||
332 | } | 347 | } |
333 | 348 | ||
334 | /** | 349 | /** |
@@ -421,11 +436,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -421,11 +436,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
421 | return; | 436 | return; |
422 | } | 437 | } |
423 | IElasticsearchClient client = index.getIndexClient(); | 438 | IElasticsearchClient client = index.getIndexClient(); |
424 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
425 | - if (StringUtils.isBlank(realIndexName)) { | 439 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
440 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
426 | return; | 441 | return; |
427 | } | 442 | } |
428 | - client.addIndexData(realIndexName, yohoIndexName, id, data); | 443 | + for (String realIndexName : realIndexNames) { |
444 | + client.addIndexData(realIndexName, yohoIndexName, id, data); | ||
445 | + } | ||
429 | } | 446 | } |
430 | 447 | ||
431 | @Override | 448 | @Override |
@@ -435,11 +452,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -435,11 +452,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
435 | return; | 452 | return; |
436 | } | 453 | } |
437 | IElasticsearchClient client = index.getIndexClient(); | 454 | IElasticsearchClient client = index.getIndexClient(); |
438 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
439 | - if (StringUtils.isBlank(realIndexName)) { | 455 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
456 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
440 | return; | 457 | return; |
441 | } | 458 | } |
442 | - client.deleteIndexData(realIndexName, yohoIndexName, id); | 459 | + for (String realIndexName : realIndexNames) { |
460 | + client.deleteIndexData(realIndexName, yohoIndexName, id); | ||
461 | + } | ||
443 | } | 462 | } |
444 | 463 | ||
445 | @Override | 464 | @Override |
@@ -449,7 +468,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -449,7 +468,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
449 | return; | 468 | return; |
450 | } | 469 | } |
451 | IElasticsearchClient client = index.getIndexClient(); | 470 | IElasticsearchClient client = index.getIndexClient(); |
452 | - List<String> realIndexNames = yohoIndexHelper.getRelatedIndexs(yohoIndexName, client); | 471 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
453 | if (realIndexNames == null || realIndexNames.isEmpty()) { | 472 | if (realIndexNames == null || realIndexNames.isEmpty()) { |
454 | return; | 473 | return; |
455 | } | 474 | } |
@@ -465,7 +484,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -465,7 +484,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
465 | return; | 484 | return; |
466 | } | 485 | } |
467 | IElasticsearchClient client = index.getIndexClient(); | 486 | IElasticsearchClient client = index.getIndexClient(); |
468 | - List<String> realIndexNames = yohoIndexHelper.getRelatedIndexs(yohoIndexName, client); | 487 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
469 | if (realIndexNames == null || realIndexNames.isEmpty()) { | 488 | if (realIndexNames == null || realIndexNames.isEmpty()) { |
470 | return; | 489 | return; |
471 | } | 490 | } |
@@ -501,13 +520,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -501,13 +520,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
501 | return null; | 520 | return null; |
502 | } | 521 | } |
503 | IElasticsearchClient client = index.getIndexClient(); | 522 | IElasticsearchClient client = index.getIndexClient(); |
504 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
505 | - if (StringUtils.isBlank(realIndexName)) { | 523 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
524 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
506 | return null; | 525 | return null; |
507 | } | 526 | } |
508 | - return client.get(realIndexName, yohoIndexName, id); | 527 | + return client.get(realIndexNames.get(0), yohoIndexName, id); |
509 | } | 528 | } |
510 | - | 529 | + |
511 | @Override | 530 | @Override |
512 | public MultiGetResponse multiGet(String yohoIndexName, Set<String> idList, List<String> fields) { | 531 | public MultiGetResponse multiGet(String yohoIndexName, Set<String> idList, List<String> fields) { |
513 | IYohoIndex index = this.nameToIndexMap.get(yohoIndexName); | 532 | IYohoIndex index = this.nameToIndexMap.get(yohoIndexName); |
@@ -515,11 +534,11 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -515,11 +534,11 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
515 | return null; | 534 | return null; |
516 | } | 535 | } |
517 | IElasticsearchClient client = index.getIndexClient(); | 536 | IElasticsearchClient client = index.getIndexClient(); |
518 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
519 | - if (StringUtils.isBlank(realIndexName)) { | 537 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
538 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
520 | return null; | 539 | return null; |
521 | } | 540 | } |
522 | - return client.multiGet(realIndexName, yohoIndexName, idList, fields); | 541 | + return client.multiGet(realIndexNames.get(0), yohoIndexName, idList, fields); |
523 | } | 542 | } |
524 | 543 | ||
525 | @Override | 544 | @Override |
@@ -529,8 +548,8 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -529,8 +548,8 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
529 | return null; | 548 | return null; |
530 | } | 549 | } |
531 | IElasticsearchClient client = index.getIndexClient(); | 550 | IElasticsearchClient client = index.getIndexClient(); |
532 | - String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client); | ||
533 | - if (StringUtils.isBlank(realIndexName)) { | 551 | + List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client); |
552 | + if (realIndexNames == null || realIndexNames.isEmpty()) { | ||
534 | return new ArrayList<AnalyzeToken>(); | 553 | return new ArrayList<AnalyzeToken>(); |
535 | } | 554 | } |
536 | AnalyzeResponse analyzeResponse = client.getAnalyzeResponse(yohoIndexName, text, analyzer); | 555 | AnalyzeResponse analyzeResponse = client.getAnalyzeResponse(yohoIndexName, text, analyzer); |
-
Please register or login to post a comment