Authored by 胡古飞

update

... ... @@ -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;
... ... @@ -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,12 +244,14 @@ 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;
}
for (String realIndexName : realIndexNames) {
client.refreshIndex(realIndexName);
}
}
@Override
public void closeIndex(final String yohoIndexName) throws Exception {
... ... @@ -255,12 +260,14 @@ 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;
}
for (String realIndexName : realIndexNames) {
client.closeIndex(realIndexName);
}
}
@Override
public void flushIndex(final String yohoIndexName) throws Exception {
... ... @@ -269,12 +276,14 @@ 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;
}
for (String realIndexName : realIndexNames) {
client.flushIndex(realIndexName);
}
}
@Override
public void replaceIndex(String yohoIndexName, String yohoTemplateIndexName) {
... ... @@ -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,12 +337,14 @@ 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;
}
for (String realIndexName : realIndexNames) {
client.optimize(realIndexName);
}
}
/**
* 执行索引重建 重建策略: 1. 建一个新索引 2. 向新导入数据 3. 删除原索引的别名 4. 为新索引增加别名
... ... @@ -421,12 +436,14 @@ 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;
}
for (String realIndexName : realIndexNames) {
client.addIndexData(realIndexName, yohoIndexName, id, data);
}
}
@Override
public void deleteIndexData(final String yohoIndexName, final String id) throws Exception {
... ... @@ -435,12 +452,14 @@ 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;
}
for (String realIndexName : realIndexNames) {
client.deleteIndexData(realIndexName, yohoIndexName, id);
}
}
@Override
public void updateIndexData(final String yohoIndexName, final String id, final Object data) throws Exception {
... ... @@ -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,11 +520,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.get(realIndexName, yohoIndexName, id);
return client.get(realIndexNames.get(0), yohoIndexName, id);
}
@Override
... ... @@ -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);
... ...