1
|
package com.yoho.search.consumer.index.common.impl;
|
1
|
package com.yoho.search.consumer.index.common.impl;
|
2
|
|
2
|
|
|
|
3
|
+import java.io.InputStream;
|
|
|
4
|
+import java.util.ArrayList;
|
|
|
5
|
+import java.util.List;
|
|
|
6
|
+import java.util.Map;
|
|
|
7
|
+import java.util.Set;
|
|
|
8
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
9
|
+import java.util.regex.Pattern;
|
|
|
10
|
+
|
|
|
11
|
+import javax.annotation.PostConstruct;
|
|
|
12
|
+
|
|
|
13
|
+import org.apache.commons.lang.StringUtils;
|
|
|
14
|
+import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
|
|
|
15
|
+import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
|
|
|
16
|
+import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
17
|
+import org.elasticsearch.action.get.GetResponse;
|
|
|
18
|
+import org.elasticsearch.action.get.MultiGetResponse;
|
|
|
19
|
+import org.elasticsearch.cluster.health.ClusterHealthStatus;
|
|
|
20
|
+import org.slf4j.Logger;
|
|
|
21
|
+import org.slf4j.LoggerFactory;
|
|
|
22
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
23
|
+import org.springframework.context.ApplicationEventPublisher;
|
|
|
24
|
+import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
25
|
+import org.springframework.stereotype.Component;
|
|
|
26
|
+
|
3
|
import com.yoho.search.base.utils.FileUtils;
|
27
|
import com.yoho.search.base.utils.FileUtils;
|
4
|
import com.yoho.search.base.utils.JaxbBinder;
|
28
|
import com.yoho.search.base.utils.JaxbBinder;
|
5
|
import com.yoho.search.consumer.index.common.IIndexBuilder;
|
29
|
import com.yoho.search.consumer.index.common.IIndexBuilder;
|
|
@@ -17,28 +41,6 @@ import com.yoho.search.core.es.impl.YohoIndexHelper; |
|
@@ -17,28 +41,6 @@ import com.yoho.search.core.es.impl.YohoIndexHelper; |
17
|
import com.yoho.search.core.es.model.ESBluk;
|
41
|
import com.yoho.search.core.es.model.ESBluk;
|
18
|
import com.yoho.search.core.es.model.SearchParam;
|
42
|
import com.yoho.search.core.es.model.SearchParam;
|
19
|
import com.yoho.search.core.es.model.SearchResult;
|
43
|
import com.yoho.search.core.es.model.SearchResult;
|
20
|
-import org.apache.commons.lang.StringUtils;
|
|
|
21
|
-import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
|
|
|
22
|
-import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
|
|
|
23
|
-import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
24
|
-import org.elasticsearch.action.get.GetResponse;
|
|
|
25
|
-import org.elasticsearch.action.get.MultiGetResponse;
|
|
|
26
|
-import org.elasticsearch.cluster.health.ClusterHealthStatus;
|
|
|
27
|
-import org.slf4j.Logger;
|
|
|
28
|
-import org.slf4j.LoggerFactory;
|
|
|
29
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
30
|
-import org.springframework.context.ApplicationEventPublisher;
|
|
|
31
|
-import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
32
|
-import org.springframework.stereotype.Component;
|
|
|
33
|
-
|
|
|
34
|
-import javax.annotation.PostConstruct;
|
|
|
35
|
-import java.io.InputStream;
|
|
|
36
|
-import java.util.ArrayList;
|
|
|
37
|
-import java.util.List;
|
|
|
38
|
-import java.util.Map;
|
|
|
39
|
-import java.util.Set;
|
|
|
40
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
41
|
-import java.util.regex.Pattern;
|
|
|
42
|
|
44
|
|
43
|
@Component
|
45
|
@Component
|
44
|
public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEventPublisherAware {
|
46
|
public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEventPublisherAware {
|
|
@@ -55,6 +57,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
@@ -55,6 +57,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
55
|
private IYohoIndexDataLoader yohoIndexDataLoader;
|
57
|
private IYohoIndexDataLoader yohoIndexDataLoader;
|
56
|
@Autowired
|
58
|
@Autowired
|
57
|
private IndexRebuildListenerMgr indexRebuildListenerMgr;
|
59
|
private IndexRebuildListenerMgr indexRebuildListenerMgr;
|
|
|
60
|
+
|
58
|
ApplicationEventPublisher publisher;
|
61
|
ApplicationEventPublisher publisher;
|
59
|
|
62
|
|
60
|
@Override
|
63
|
@Override
|
|
@@ -138,7 +141,8 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
@@ -138,7 +141,8 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
138
|
|
141
|
|
139
|
@Override
|
142
|
@Override
|
140
|
public String createIndex(final String yohoIndexName, final boolean force) throws Exception {
|
143
|
public String createIndex(final String yohoIndexName, final boolean force) throws Exception {
|
141
|
- return this.createIndex(yohoIndexName, yohoIndexName, force);
|
144
|
+ String yohoIndexAliasName = yohoIndexName;
|
|
|
145
|
+ return this.createIndex(yohoIndexName, yohoIndexAliasName, force);
|
142
|
}
|
146
|
}
|
143
|
|
147
|
|
144
|
@Override
|
148
|
@Override
|
|
@@ -164,7 +168,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
@@ -164,7 +168,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
164
|
client.removeAlias(oldRealIndexName, alias);
|
168
|
client.removeAlias(oldRealIndexName, alias);
|
165
|
}
|
169
|
}
|
166
|
// 5、添加新索引别名
|
170
|
// 5、添加新索引别名
|
167
|
- client.addAlias(alias, newRealIndexName);
|
171
|
+ client.addAlias(newRealIndexName, alias);
|
168
|
// 6、删除旧索引
|
172
|
// 6、删除旧索引
|
169
|
if (oldRealIndexName != null) {
|
173
|
if (oldRealIndexName != null) {
|
170
|
client.deleteIndex(oldRealIndexName);
|
174
|
client.deleteIndex(oldRealIndexName);
|
|
@@ -281,29 +285,31 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
@@ -281,29 +285,31 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
281
|
Map<String, List<String>> realNameToAliasesMap = client.getRealNameToAliasesMap();
|
285
|
Map<String, List<String>> realNameToAliasesMap = client.getRealNameToAliasesMap();
|
282
|
|
286
|
|
283
|
// 2、获取真实的索引名称
|
287
|
// 2、获取真实的索引名称
|
284
|
- String oldIndexId = yohoIndexHelper.getRealIndexName(yohoIndexName, realNameToAliasesMap);
|
|
|
285
|
- String newIndexId = yohoIndexHelper.getRealIndexName(yohoTemplateIndexName, realNameToAliasesMap);
|
|
|
286
|
- if (newIndexId == null) {
|
288
|
+ String oldIndexRealName = yohoIndexHelper.getRealIndexName(yohoIndexName, realNameToAliasesMap);
|
|
|
289
|
+ String newIndexRealName = yohoIndexHelper.getRealIndexName(yohoTemplateIndexName, realNameToAliasesMap);
|
|
|
290
|
+ if (newIndexRealName == null) {
|
287
|
return;
|
291
|
return;
|
288
|
}
|
292
|
}
|
289
|
|
293
|
|
|
|
294
|
+ // 3、获取有货索引的别名
|
|
|
295
|
+ String yohoIndexAliasName = yohoIndexName;
|
|
|
296
|
+
|
290
|
// 4. 把老索引的索引名先删除
|
297
|
// 4. 把老索引的索引名先删除
|
291
|
- if (oldIndexId != null) {
|
|
|
292
|
- client.removeAlias(oldIndexId, yohoIndexName);
|
298
|
+ if (oldIndexRealName != null) {
|
|
|
299
|
+ client.removeAlias(oldIndexRealName, yohoIndexAliasName);
|
293
|
}
|
300
|
}
|
294
|
|
301
|
|
295
|
// 5. 把新索引的别名全部删除
|
302
|
// 5. 把新索引的别名全部删除
|
296
|
- List<String> yohoTemplateIndexNameAlias = realNameToAliasesMap.get(newIndexId);
|
|
|
297
|
- for (String alias : yohoTemplateIndexNameAlias) {
|
|
|
298
|
- client.removeAlias(newIndexId, alias);
|
|
|
299
|
- }
|
303
|
+ List<String> aliasList = realNameToAliasesMap.get(newIndexRealName);
|
|
|
304
|
+ String[] aliases = aliasList.toArray(new String[aliasList.size()]);
|
|
|
305
|
+ client.removeAlias(newIndexRealName, aliases);
|
300
|
|
306
|
|
301
|
// 6. 把老索引的索引名加到新索引上面
|
307
|
// 6. 把老索引的索引名加到新索引上面
|
302
|
- client.addAlias(yohoIndexName, newIndexId);
|
308
|
+ client.addAlias(newIndexRealName, yohoIndexAliasName);
|
303
|
|
309
|
|
304
|
// 7. 删除老索引
|
310
|
// 7. 删除老索引
|
305
|
- if (oldIndexId != null) {
|
|
|
306
|
- client.deleteIndex(oldIndexId);
|
311
|
+ if (oldIndexRealName != null) {
|
|
|
312
|
+ client.deleteIndex(oldIndexRealName);
|
307
|
}
|
313
|
}
|
308
|
|
314
|
|
309
|
}
|
315
|
}
|
|
@@ -419,22 +425,6 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
@@ -419,22 +425,6 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
419
|
client.addIndexData(realIndexName, yohoIndexName, id, data);
|
425
|
client.addIndexData(realIndexName, yohoIndexName, id, data);
|
420
|
}
|
426
|
}
|
421
|
|
427
|
|
422
|
- // @Override
|
|
|
423
|
- // public void addIndexData(final String yohoIndexName, final
|
|
|
424
|
- // List<Map<String, Object>> dataList) throws Exception {
|
|
|
425
|
- // IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
|
|
|
426
|
- // if (index == null) {
|
|
|
427
|
- // return;
|
|
|
428
|
- // }
|
|
|
429
|
- // IElasticsearchClient client = index.getIndexClient();
|
|
|
430
|
- // String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName,
|
|
|
431
|
- // client);
|
|
|
432
|
- // if (StringUtils.isBlank(realIndexName)) {
|
|
|
433
|
- // return;
|
|
|
434
|
- // }
|
|
|
435
|
- // client.addIndexData(realIndexName, yohoIndexName, dataList);
|
|
|
436
|
- // }
|
|
|
437
|
-
|
|
|
438
|
@Override
|
428
|
@Override
|
439
|
public void deleteIndexData(final String yohoIndexName, final String id) throws Exception {
|
429
|
public void deleteIndexData(final String yohoIndexName, final String id) throws Exception {
|
440
|
IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
|
430
|
IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
|
|
@@ -449,22 +439,6 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
@@ -449,22 +439,6 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
449
|
client.deleteIndexData(realIndexName, yohoIndexName, id);
|
439
|
client.deleteIndexData(realIndexName, yohoIndexName, id);
|
450
|
}
|
440
|
}
|
451
|
|
441
|
|
452
|
- // @Override
|
|
|
453
|
- // public void deleteIndexDataByQuery(final String yohoIndexName, final
|
|
|
454
|
- // String query) throws Exception {
|
|
|
455
|
- // IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
|
|
|
456
|
- // if (index == null) {
|
|
|
457
|
- // return;
|
|
|
458
|
- // }
|
|
|
459
|
- // IElasticsearchClient client = index.getIndexClient();
|
|
|
460
|
- // String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName,
|
|
|
461
|
- // client);
|
|
|
462
|
- // if (StringUtils.isBlank(realIndexName)) {
|
|
|
463
|
- // return;
|
|
|
464
|
- // }
|
|
|
465
|
- // client.deleteIndexDataByQuery(realIndexName, yohoIndexName, query);
|
|
|
466
|
- // }
|
|
|
467
|
-
|
|
|
468
|
@Override
|
442
|
@Override
|
469
|
public void updateIndexData(final String yohoIndexName, final String id, final Object data) throws Exception {
|
443
|
public void updateIndexData(final String yohoIndexName, final String id, final Object data) throws Exception {
|
470
|
IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
|
444
|
IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
|
|
@@ -566,6 +540,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
@@ -566,6 +540,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
566
|
if (index == null) {
|
540
|
if (index == null) {
|
567
|
return null;
|
541
|
return null;
|
568
|
}
|
542
|
}
|
|
|
543
|
+ rebuildFlagService.waitingRebuildingIndex();
|
569
|
IElasticsearchClient client = index.getIndexClient();
|
544
|
IElasticsearchClient client = index.getIndexClient();
|
570
|
BulkResponse bulkResponse = client.bulk(esBluks);
|
545
|
BulkResponse bulkResponse = client.bulk(esBluks);
|
571
|
if (bulkResponse.hasFailures()) {
|
546
|
if (bulkResponse.hasFailures()) {
|