DATAES-64 - Add dynamic settings using @Setting annotation

This commit is contained in:
Mohsin Husen
2014-03-26 20:19:42 +00:00
parent 403930d448
commit 1e3ec3eaf0
9 changed files with 330 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.annotations;
import java.lang.annotation.*;
import org.springframework.data.annotation.Persistent;
/**
* Elasticsearch Setting
*
* @author Mohsin Husen
*/
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Setting {
String settingPath() default "";
}

View File

@@ -23,7 +23,9 @@ import static org.elasticsearch.common.collect.Sets.*;
import static org.elasticsearch.index.VersionType.*;
import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.*;
@@ -64,10 +66,14 @@ import org.elasticsearch.search.facet.FacetBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.ElasticsearchException;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Setting;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
@@ -87,6 +93,7 @@ import org.springframework.util.Assert;
public class ElasticsearchTemplate implements ElasticsearchOperations {
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchTemplate.class);
private Client client;
private ElasticsearchConverter elasticsearchConverter;
private ResultsMapper resultsMapper;
@@ -554,6 +561,17 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
}
private <T> boolean createIndexWithSettings(Class<T> clazz) {
if(clazz.isAnnotationPresent(Setting.class)) {
String settingPath = clazz.getAnnotation(Setting.class).settingPath();
if(isNotBlank(settingPath)) {
String settings = readFileFromClasspath(settingPath);
if(isNotBlank(settings)) {
return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings);
}
} else {
logger.info("settingPath in @Setting has to be defined. Using default instead.");
}
}
return createIndex(getPersistentEntityFor(clazz).getIndexName(), getDefaultSettings(getPersistentEntityFor(clazz)));
}
@@ -790,4 +808,33 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
private boolean isDocument(Class clazz) {
return clazz.isAnnotationPresent(Document.class);
}
public static String readFileFromClasspath(String url) {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
ClassPathResource classPathResource = new ClassPathResource(url);
InputStreamReader inputStreamReader = new InputStreamReader(classPathResource.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (Exception e) {
logger.debug(String.format("Failed to load file from url: %s: %s", url, e.getMessage()));
return null;
} finally {
if (bufferedReader != null)
try {
bufferedReader.close();
} catch (IOException e) {
logger.debug(String.format("Unable to close buffered reader.. %s", e.getMessage()));
}
}
return stringBuilder.toString();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,4 +43,6 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
String getParentType();
ElasticsearchPersistentProperty getParentIdProperty();
String settingPath();
}

View File

@@ -26,6 +26,7 @@ import org.springframework.context.expression.BeanFactoryAccessor;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Parent;
import org.springframework.data.elasticsearch.annotations.Setting;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.util.TypeInformation;
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -50,6 +51,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private String indexStoreType;
private String parentType;
private ElasticsearchPersistentProperty parentIdProperty;
private String settingPath;
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
super(typeInformation);
@@ -66,6 +68,9 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
this.refreshInterval = typeInformation.getType().getAnnotation(Document.class).refreshInterval();
this.indexStoreType = typeInformation.getType().getAnnotation(Document.class).indexStoreType();
}
if(clazz.isAnnotationPresent(Setting.class)) {
this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath();
}
}
@Override
@@ -115,6 +120,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
return parentIdProperty;
}
@Override
public String settingPath() {
return settingPath;
}
@Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);