...
|
...
|
@@ -10,7 +10,6 @@ import java.util.regex.Pattern; |
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
|
|
|
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
|
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
...
|
...
|
@@ -57,7 +56,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
private IYohoIndexDataLoader yohoIndexDataLoader;
|
|
|
@Autowired
|
|
|
private IndexRebuildListenerMgr indexRebuildListenerMgr;
|
|
|
|
|
|
|
|
|
ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -132,11 +131,15 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
// 获取真实的索引名
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return false;
|
|
|
}
|
|
|
if (realIndexNames.size() != 1) {
|
|
|
client.deleteIndexs(realIndexNames);
|
|
|
return false;
|
|
|
}
|
|
|
return client.indexExists(realIndexName);
|
|
|
return client.indexExists(realIndexNames.get(0));
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -153,8 +156,8 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
// 1、判断旧索引是否存在,如果存在且不是强制更新,则直接返回
|
|
|
String oldRealIndexName = yohoIndexHelper.getRealIndexName(alias, client);
|
|
|
if (StringUtils.isNotBlank(oldRealIndexName) && !force) {
|
|
|
List<String> oldRealIndexNames = yohoIndexHelper.getRealIndexNames(alias, client.getRealNameToAliasesMap());
|
|
|
if (oldRealIndexNames != null && oldRealIndexNames.size() == 1 && !force) {
|
|
|
return null;
|
|
|
}
|
|
|
// 2、生成真实索引名称
|
...
|
...
|
@@ -164,17 +167,17 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
final String mappingContent = index.getMappingContent();
|
|
|
client.createIndex(newRealIndexName, yohoIndexName, settings, mappingContent);
|
|
|
// 4、删除旧索引别名
|
|
|
if (oldRealIndexName != null) {
|
|
|
client.removeAlias(oldRealIndexName, alias);
|
|
|
if (oldRealIndexNames != null && !oldRealIndexNames.isEmpty()) {
|
|
|
client.removeAlias(oldRealIndexNames, alias);
|
|
|
}
|
|
|
// 5、添加新索引别名【如果存在一个名字为alias的真实索引,则直接删除】
|
|
|
if(client.indexExists(alias)){
|
|
|
if (client.indexExists(alias)) {
|
|
|
client.deleteIndex(alias);
|
|
|
}
|
|
|
client.addAlias(newRealIndexName, alias);
|
|
|
// 7、删除旧索引
|
|
|
if (oldRealIndexName != null) {
|
|
|
client.deleteIndex(oldRealIndexName);
|
|
|
if (oldRealIndexNames != null && !oldRealIndexNames.isEmpty()) {
|
|
|
client.deleteIndexs(oldRealIndexNames);
|
|
|
}
|
|
|
return newRealIndexName;
|
|
|
}
|
...
|
...
|
@@ -186,11 +189,11 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
client.deleteIndex(realIndexName);
|
|
|
client.deleteIndexs(realIndexNames);
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -222,7 +225,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
}
|
|
|
// 3)如果别名中不包含yohoIndexName,则视为失败的临时索引
|
|
|
List<String> aliasNames = entry.getValue();
|
|
|
if (!aliasNames.contains(yohoIndexName)) {
|
|
|
if (aliasNames == null || !aliasNames.contains(yohoIndexName)) {
|
|
|
tempRealIndexNames.add(realIndexName);
|
|
|
continue;
|
|
|
}
|
...
|
...
|
@@ -241,11 +244,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
client.refreshIndex(realIndexName);
|
|
|
for (String realIndexName : realIndexNames) {
|
|
|
client.refreshIndex(realIndexName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -255,11 +260,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
client.closeIndex(realIndexName);
|
|
|
for (String realIndexName : realIndexNames) {
|
|
|
client.closeIndex(realIndexName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -269,11 +276,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
client.flushIndex(realIndexName);
|
|
|
for (String realIndexName : realIndexNames) {
|
|
|
client.flushIndex(realIndexName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -287,34 +296,38 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
// 1、获取真实索引名与别名的对应关系
|
|
|
Map<String, List<String>> realNameToAliasesMap = client.getRealNameToAliasesMap();
|
|
|
|
|
|
// 2、获取真实的索引名称
|
|
|
String oldIndexRealName = yohoIndexHelper.getRealIndexName(yohoIndexName, realNameToAliasesMap);
|
|
|
String newIndexRealName = yohoIndexHelper.getRealIndexName(yohoTemplateIndexName, realNameToAliasesMap);
|
|
|
if (newIndexRealName == null) {
|
|
|
// 2、获取新的索引名称[考虑一个别名有多个索引的情况]
|
|
|
List<String> newIndexRealNames = yohoIndexHelper.getRealIndexNames(yohoTemplateIndexName, realNameToAliasesMap);
|
|
|
if (newIndexRealNames == null || newIndexRealNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
if (newIndexRealNames.size() != 1) {
|
|
|
client.deleteIndexs(newIndexRealNames);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 3、获取有货索引的别名
|
|
|
String yohoIndexAliasName = yohoIndexName;
|
|
|
// 3、获取老的索引名称列表
|
|
|
List<String> oldIndexRealNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, realNameToAliasesMap);
|
|
|
|
|
|
// 4. 把老索引的索引名先删除
|
|
|
if (oldIndexRealName != null) {
|
|
|
client.removeAlias(oldIndexRealName, yohoIndexAliasName);
|
|
|
// 4. 把全部老索引的别名全部删除
|
|
|
String yohoIndexAliasName = yohoIndexName;
|
|
|
if (oldIndexRealNames != null && !oldIndexRealNames.isEmpty()) {
|
|
|
client.removeAlias(oldIndexRealNames, yohoIndexAliasName);
|
|
|
}
|
|
|
|
|
|
// 5. 把新索引的别名全部删除
|
|
|
// 5. 把唯一的一个新索引的别名全部删除
|
|
|
String newIndexRealName = newIndexRealNames.get(0);
|
|
|
List<String> aliasList = realNameToAliasesMap.get(newIndexRealName);
|
|
|
String[] aliases = aliasList.toArray(new String[aliasList.size()]);
|
|
|
client.removeAlias(newIndexRealName, aliases);
|
|
|
|
|
|
// 6. 把老索引的索引名加到新索引上面
|
|
|
// 6. 把别名加到新索引上面
|
|
|
client.addAlias(newIndexRealName, yohoIndexAliasName);
|
|
|
|
|
|
// 7. 删除老索引
|
|
|
if (oldIndexRealName != null) {
|
|
|
client.deleteIndex(oldIndexRealName);
|
|
|
if (oldIndexRealNames != null && !oldIndexRealNames.isEmpty()) {
|
|
|
client.deleteIndexs(oldIndexRealNames);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -324,11 +337,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
client.optimize(realIndexName);
|
|
|
for (String realIndexName : realIndexNames) {
|
|
|
client.optimize(realIndexName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -421,11 +436,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
client.addIndexData(realIndexName, yohoIndexName, id, data);
|
|
|
for (String realIndexName : realIndexNames) {
|
|
|
client.addIndexData(realIndexName, yohoIndexName, id, data);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -435,11 +452,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
client.deleteIndexData(realIndexName, yohoIndexName, id);
|
|
|
for (String realIndexName : realIndexNames) {
|
|
|
client.deleteIndexData(realIndexName, yohoIndexName, id);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -449,7 +468,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRelatedIndexs(yohoIndexName, client);
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
...
|
...
|
@@ -465,7 +484,7 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRelatedIndexs(yohoIndexName, client);
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return;
|
|
|
}
|
...
|
...
|
@@ -501,13 +520,13 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return null;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return null;
|
|
|
}
|
|
|
return client.get(realIndexName, yohoIndexName, id);
|
|
|
return client.get(realIndexNames.get(0), yohoIndexName, id);
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public MultiGetResponse multiGet(String yohoIndexName, Set<String> idList, List<String> fields) {
|
|
|
IYohoIndex index = this.nameToIndexMap.get(yohoIndexName);
|
...
|
...
|
@@ -515,11 +534,11 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return null;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return null;
|
|
|
}
|
|
|
return client.multiGet(realIndexName, yohoIndexName, idList, fields);
|
|
|
return client.multiGet(realIndexNames.get(0), yohoIndexName, idList, fields);
|
|
|
}
|
|
|
|
|
|
@Override
|
...
|
...
|
@@ -529,8 +548,8 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent |
|
|
return null;
|
|
|
}
|
|
|
IElasticsearchClient client = index.getIndexClient();
|
|
|
String realIndexName = yohoIndexHelper.getRealIndexName(yohoIndexName, client);
|
|
|
if (StringUtils.isBlank(realIndexName)) {
|
|
|
List<String> realIndexNames = yohoIndexHelper.getRealIndexNames(yohoIndexName, client);
|
|
|
if (realIndexNames == null || realIndexNames.isEmpty()) {
|
|
|
return new ArrayList<AnalyzeToken>();
|
|
|
}
|
|
|
AnalyzeResponse analyzeResponse = client.getAnalyzeResponse(yohoIndexName, text, analyzer);
|
...
|
...
|
|