|
@@ -19,6 +19,7 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.zxp.esclientrhl.annotation.ESID;
|
|
|
import org.zxp.esclientrhl.index.ElasticsearchIndex;
|
|
@@ -40,6 +41,11 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
public class ElasticSearchPartitionImpl<T, M> extends ElasticsearchTemplateImpl<T, M>
|
|
|
implements ElasticSearchPartition<T, M> {
|
|
|
|
|
|
+ @Value("${elasticsearch.enable-env-prefix:false}")
|
|
|
+ private boolean enableEnvPrefix;
|
|
|
+ @Value("${spring.profiles.active:}")
|
|
|
+ private String env;
|
|
|
+
|
|
|
private Logger logger = LoggerFactory.getLogger(ElasticSearchPartitionImpl.class);
|
|
|
|
|
|
private static Map<Class, String> classIDMap = new ConcurrentHashMap();
|
|
@@ -53,6 +59,7 @@ public class ElasticSearchPartitionImpl<T, M> extends ElasticsearchTemplateImpl<
|
|
|
@Override
|
|
|
public PageList<T> searchPartitionPage(String indexName, T t, int pageNo, int pageSize, Sort.Order[] sort)
|
|
|
throws Exception {
|
|
|
+ indexName = getIndexName(indexName);
|
|
|
QueryBuilder queryBuilder = ESUtils.beanToQueryBuilder(t);
|
|
|
return searchPartitionPage(indexName, queryBuilder, pageNo, pageSize, t.getClass(), sort);
|
|
|
}
|
|
@@ -60,6 +67,7 @@ public class ElasticSearchPartitionImpl<T, M> extends ElasticsearchTemplateImpl<
|
|
|
@Override
|
|
|
public PageList<T> searchPartitionPage(String indexName, QueryBuilder queryBuilder, int pageNo, int pageSize,
|
|
|
Class clazz, Sort.Order[] sort) throws Exception {
|
|
|
+ indexName = getIndexName(indexName);
|
|
|
Attach attach = builderAttach(pageNo, pageSize, sort);
|
|
|
PageList<T> pageList = super.search(queryBuilder, attach, clazz, indexName);
|
|
|
return pageList;
|
|
@@ -67,6 +75,7 @@ public class ElasticSearchPartitionImpl<T, M> extends ElasticsearchTemplateImpl<
|
|
|
|
|
|
@Override
|
|
|
public List<T> searchPartition(String indexName, T t) throws Exception {
|
|
|
+ indexName = getIndexName(indexName);
|
|
|
QueryBuilder queryBuilder = ESUtils.beanToQueryBuilder(t);
|
|
|
return super.search(queryBuilder, (Class<T>)t.getClass(), indexName);
|
|
|
}
|
|
@@ -85,7 +94,8 @@ public class ElasticSearchPartitionImpl<T, M> extends ElasticsearchTemplateImpl<
|
|
|
public List<T> search(QueryBuilder queryBuilder, Class<T> clazz, String... indexs) throws Exception {
|
|
|
MetaData metaData = IndexTools.getIndexType(clazz);
|
|
|
List<T> list = new ArrayList();
|
|
|
- SearchRequest searchRequest = new SearchRequest(indexs);
|
|
|
+
|
|
|
+ SearchRequest searchRequest = new SearchRequest(getIndexName(indexs));
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
searchSourceBuilder.query(queryBuilder);
|
|
|
searchSourceBuilder.from(0);
|
|
@@ -148,7 +158,7 @@ public class ElasticSearchPartitionImpl<T, M> extends ElasticsearchTemplateImpl<
|
|
|
MetaData metaData = IndexTools.getIndexType(indexEntity.getClass());
|
|
|
String indexname = DataUtils.getDefaultValue(indexName, metaData.getIndexname());
|
|
|
String indextype = DataUtils.getDefaultValue(metaData.getIndextype(), "");
|
|
|
-
|
|
|
+ indexname = getIndexName(indexname);
|
|
|
String id = Tools.getESId(indexEntity);
|
|
|
IndexRequest indexRequest = null;
|
|
|
if (StringUtils.isEmpty(id)) {
|
|
@@ -184,6 +194,7 @@ public class ElasticSearchPartitionImpl<T, M> extends ElasticsearchTemplateImpl<
|
|
|
|
|
|
@Override
|
|
|
public BulkResponse[] saveBatchPartition(List<T> list, String indexName) throws Exception {
|
|
|
+ indexName = getIndexName(indexName);
|
|
|
if (list != null && list.size() != 0) {
|
|
|
T t = list.get(0);
|
|
|
MetaData metaData = IndexTools.getIndexType(t.getClass());
|
|
@@ -215,4 +226,19 @@ public class ElasticSearchPartitionImpl<T, M> extends ElasticsearchTemplateImpl<
|
|
|
this.elasticsearchIndex.rollover(clazz, true);
|
|
|
return bulkResponse;
|
|
|
}
|
|
|
+
|
|
|
+ private String getIndexName(String indexName) {
|
|
|
+ if (enableEnvPrefix) {
|
|
|
+ return env + "_" + indexName;
|
|
|
+ }
|
|
|
+ return indexName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String[] getIndexName(String... indexName) {
|
|
|
+ String[] indexs = new String[indexName.length];
|
|
|
+ for (int i = 0; i < indexName.length; i++) {
|
|
|
+ indexs[i] = getIndexName(indexName[i]);
|
|
|
+ }
|
|
|
+ return indexs;
|
|
|
+ }
|
|
|
}
|