DATAES-6 : Instead of storing data in "fs" it should store "in memory" for unit tests
This commit is contained in:
@@ -35,4 +35,5 @@ public @interface Document {
|
||||
|
||||
String indexName();
|
||||
String type() default "";
|
||||
String indexStoreType() default "fs";
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.elasticsearch.client;
|
||||
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.client.node.NodeClient;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
@@ -36,7 +37,8 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(NodeClientFactoryBean.class);
|
||||
private boolean local;
|
||||
private boolean purgeDataOnShutdown;
|
||||
private boolean enableHttp;
|
||||
private String clusterName;
|
||||
private NodeClient nodeClient;
|
||||
|
||||
NodeClientFactoryBean() {
|
||||
@@ -63,15 +65,23 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
nodeClient = (NodeClient) nodeBuilder().local(this.local).data(!this.purgeDataOnShutdown).node().client();
|
||||
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder()
|
||||
.put("http.enabled", String.valueOf(this.enableHttp));
|
||||
|
||||
nodeClient = (NodeClient) nodeBuilder().settings(settings)
|
||||
.clusterName(this.clusterName).local(this.local).node().client();
|
||||
}
|
||||
|
||||
public void setLocal(boolean local) {
|
||||
this.local = local;
|
||||
}
|
||||
|
||||
public void setPurgeDataOnShutdown(boolean purgeDataOnShutdown) {
|
||||
this.purgeDataOnShutdown = purgeDataOnShutdown;
|
||||
public void setEnableHttp(boolean enableHttp) {
|
||||
this.enableHttp = enableHttp;
|
||||
}
|
||||
|
||||
public void setClusterName(String clusterName) {
|
||||
this.clusterName = clusterName;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -41,7 +41,8 @@ public class NodeClientBeanDefinitionParser extends AbstractBeanDefinitionParser
|
||||
|
||||
private void setLocalSettings(Element element, BeanDefinitionBuilder builder) {
|
||||
builder.addPropertyValue("local", Boolean.valueOf(element.getAttribute("local")));
|
||||
builder.addPropertyValue("purgeDataOnShutdown", Boolean.valueOf(element.getAttribute("purge-data-on-shutdown")));
|
||||
builder.addPropertyValue("clusterName", element.getAttribute("cluster-name"));
|
||||
builder.addPropertyValue("enableHttp",Boolean.valueOf(element.getAttribute("http-enabled")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
@Override
|
||||
public <T> boolean createIndex(Class<T> clazz) {
|
||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||
return createIndexIfNotCreated(persistentEntity.getIndexName());
|
||||
return createIndexIfNotCreated(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -369,8 +369,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
return searchRequest.setQuery(query).execute().actionGet();
|
||||
}
|
||||
|
||||
private boolean createIndexIfNotCreated(String indexName) {
|
||||
return indexExists(indexName) || createIndex(indexName);
|
||||
private <T> boolean createIndexIfNotCreated(Class<T> clazz) {
|
||||
return indexExists(getPersistentEntityFor(clazz).getIndexName()) || createIndexWithSettings(clazz);
|
||||
}
|
||||
|
||||
private boolean indexExists(String indexName) {
|
||||
@@ -379,9 +379,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
.exists(indicesExistsRequest(indexName)).actionGet().exists();
|
||||
}
|
||||
|
||||
private boolean createIndex(String indexName) {
|
||||
return client.admin().indices().create(Requests.createIndexRequest(indexName).
|
||||
settings(new MapBuilder<String, String>().put("index.refresh_interval", "-1").map())).actionGet().acknowledged();
|
||||
private <T> boolean createIndexWithSettings(Class<T> clazz) {
|
||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||
return client.admin().indices().create(Requests.createIndexRequest(persistentEntity.getIndexName()).
|
||||
settings(new MapBuilder<String, String>()
|
||||
.put("index.store.type", persistentEntity.getIndexStoreType())
|
||||
.map())).actionGet().acknowledged();
|
||||
}
|
||||
|
||||
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz){
|
||||
|
||||
@@ -27,5 +27,6 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
||||
|
||||
String getIndexName();
|
||||
String getIndexType();
|
||||
String getIndexStoreType();
|
||||
ElasticsearchPersistentProperty getVersionProperty();
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
||||
private final StandardEvaluationContext context;
|
||||
private String indexName;
|
||||
private String indexType;
|
||||
private String indexStoreType;
|
||||
|
||||
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
|
||||
super(typeInformation);
|
||||
@@ -55,6 +56,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
||||
Assert.hasText(document.indexName(), " Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")");
|
||||
this.indexName = typeInformation.getType().getAnnotation(Document.class).indexName();
|
||||
this.indexType = hasText(document.type())? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH);
|
||||
this.indexStoreType = typeInformation.getType().getAnnotation(Document.class).indexStoreType();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +77,10 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
||||
return indexType;
|
||||
}
|
||||
|
||||
public String getIndexStoreType() {
|
||||
return indexStoreType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
||||
super.addPersistentProperty(property);
|
||||
|
||||
Reference in New Issue
Block a user