mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-12 13:23:26 +00:00
DATAES-6 : Instead of storing data in "fs" it should store "in memory" for unit tests
This commit is contained in:
parent
db5d9b8fc7
commit
86c2b1d1c5
@ -35,4 +35,5 @@ public @interface Document {
|
|||||||
|
|
||||||
String indexName();
|
String indexName();
|
||||||
String type() default "";
|
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.Client;
|
||||||
import org.elasticsearch.client.node.NodeClient;
|
import org.elasticsearch.client.node.NodeClient;
|
||||||
|
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
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 static final Logger logger = LoggerFactory.getLogger(NodeClientFactoryBean.class);
|
||||||
private boolean local;
|
private boolean local;
|
||||||
private boolean purgeDataOnShutdown;
|
private boolean enableHttp;
|
||||||
|
private String clusterName;
|
||||||
private NodeClient nodeClient;
|
private NodeClient nodeClient;
|
||||||
|
|
||||||
NodeClientFactoryBean() {
|
NodeClientFactoryBean() {
|
||||||
@ -63,15 +65,23 @@ public class NodeClientFactoryBean implements FactoryBean<NodeClient>, Initializ
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
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) {
|
public void setLocal(boolean local) {
|
||||||
this.local = local;
|
this.local = local;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPurgeDataOnShutdown(boolean purgeDataOnShutdown) {
|
public void setEnableHttp(boolean enableHttp) {
|
||||||
this.purgeDataOnShutdown = purgeDataOnShutdown;
|
this.enableHttp = enableHttp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClusterName(String clusterName) {
|
||||||
|
this.clusterName = clusterName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -41,7 +41,8 @@ public class NodeClientBeanDefinitionParser extends AbstractBeanDefinitionParser
|
|||||||
|
|
||||||
private void setLocalSettings(Element element, BeanDefinitionBuilder builder) {
|
private void setLocalSettings(Element element, BeanDefinitionBuilder builder) {
|
||||||
builder.addPropertyValue("local", Boolean.valueOf(element.getAttribute("local")));
|
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
|
@Override
|
||||||
public <T> boolean createIndex(Class<T> clazz) {
|
public <T> boolean createIndex(Class<T> clazz) {
|
||||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||||
return createIndexIfNotCreated(persistentEntity.getIndexName());
|
return createIndexIfNotCreated(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -369,8 +369,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
return searchRequest.setQuery(query).execute().actionGet();
|
return searchRequest.setQuery(query).execute().actionGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean createIndexIfNotCreated(String indexName) {
|
private <T> boolean createIndexIfNotCreated(Class<T> clazz) {
|
||||||
return indexExists(indexName) || createIndex(indexName);
|
return indexExists(getPersistentEntityFor(clazz).getIndexName()) || createIndexWithSettings(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean indexExists(String indexName) {
|
private boolean indexExists(String indexName) {
|
||||||
@ -379,9 +379,12 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
.exists(indicesExistsRequest(indexName)).actionGet().exists();
|
.exists(indicesExistsRequest(indexName)).actionGet().exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean createIndex(String indexName) {
|
private <T> boolean createIndexWithSettings(Class<T> clazz) {
|
||||||
return client.admin().indices().create(Requests.createIndexRequest(indexName).
|
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||||
settings(new MapBuilder<String, String>().put("index.refresh_interval", "-1").map())).actionGet().acknowledged();
|
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){
|
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz){
|
||||||
|
@ -27,5 +27,6 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
|||||||
|
|
||||||
String getIndexName();
|
String getIndexName();
|
||||||
String getIndexType();
|
String getIndexType();
|
||||||
|
String getIndexStoreType();
|
||||||
ElasticsearchPersistentProperty getVersionProperty();
|
ElasticsearchPersistentProperty getVersionProperty();
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
private final StandardEvaluationContext context;
|
private final StandardEvaluationContext context;
|
||||||
private String indexName;
|
private String indexName;
|
||||||
private String indexType;
|
private String indexType;
|
||||||
|
private String indexStoreType;
|
||||||
|
|
||||||
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
|
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
|
||||||
super(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\")");
|
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.indexName = typeInformation.getType().getAnnotation(Document.class).indexName();
|
||||||
this.indexType = hasText(document.type())? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH);
|
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;
|
return indexType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getIndexStoreType() {
|
||||||
|
return indexStoreType;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
||||||
super.addPersistentProperty(property);
|
super.addPersistentProperty(property);
|
||||||
|
@ -45,7 +45,8 @@
|
|||||||
<xsd:complexContent>
|
<xsd:complexContent>
|
||||||
<xsd:extension base="beans:identifiedType">
|
<xsd:extension base="beans:identifiedType">
|
||||||
<xsd:attribute name="local" type="xsd:boolean" default="false"/>
|
<xsd:attribute name="local" type="xsd:boolean" default="false"/>
|
||||||
<xsd:attribute name="purge-data-on-shutdown" type="xsd:boolean" default="true"/>
|
<xsd:attribute name="cluster-name" type="xsd:string" default="elasticsearch"/>
|
||||||
|
<xsd:attribute name="http-enabled" type="xsd:boolean" default="true"/>
|
||||||
</xsd:extension>
|
</xsd:extension>
|
||||||
</xsd:complexContent>
|
</xsd:complexContent>
|
||||||
</xsd:complexType>
|
</xsd:complexType>
|
||||||
|
@ -21,7 +21,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
|||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
*/
|
*/
|
||||||
@Document(indexName = "book",type = "book")
|
@Document(indexName = "book",type = "book", indexStoreType = "memory")
|
||||||
public class Book {
|
public class Book {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ -20,7 +20,7 @@ import org.springframework.data.annotation.Version;
|
|||||||
import org.springframework.data.elasticsearch.annotations.Document;
|
import org.springframework.data.elasticsearch.annotations.Document;
|
||||||
|
|
||||||
|
|
||||||
@Document(indexName = "double-keyed-entity", type = "double-keyed-entity")
|
@Document(indexName = "double-keyed-entity", type = "double-keyed-entity", indexStoreType = "memory")
|
||||||
public class DoubleIDEntity {
|
public class DoubleIDEntity {
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ import org.springframework.data.annotation.Version;
|
|||||||
import org.springframework.data.elasticsearch.annotations.Document;
|
import org.springframework.data.elasticsearch.annotations.Document;
|
||||||
|
|
||||||
|
|
||||||
@Document(indexName = "integer-keyed-entity", type = "integer-keyed-entity")
|
@Document(indexName = "integer-keyed-entity", type = "integer-keyed-entity", indexStoreType = "memory")
|
||||||
public class IntegerIDEntity {
|
public class IntegerIDEntity {
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
|||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
*/
|
*/
|
||||||
@Document(indexName = "test-index", type = "test-type")
|
@Document(indexName = "test-index", type = "test-type", indexStoreType = "memory")
|
||||||
public class SampleEntity {
|
public class SampleEntity {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ -24,7 +24,7 @@ import org.springframework.data.elasticsearch.annotations.Field;
|
|||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
*/
|
*/
|
||||||
@Document(indexName = "test-mapping", type = "mapping")
|
@Document(indexName = "test-mapping", type = "mapping", indexStoreType = "memory")
|
||||||
public class SampleMappingEntity {
|
public class SampleMappingEntity {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ -25,17 +25,13 @@ import org.springframework.test.context.ContextConfiguration;
|
|||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
import static org.apache.commons.lang.RandomStringUtils.randomNumeric;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
/**
|
/**
|
||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @author Mohsin Husen
|
||||||
@ -56,7 +52,6 @@ public class CriteriaQueryTests {
|
|||||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldPerformAndOperation(){
|
public void shouldPerformAndOperation(){
|
||||||
//given
|
//given
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
package org.springframework.data.elasticsearch.repositories;
|
package org.springframework.data.elasticsearch.repositories;
|
||||||
|
|
||||||
import org.springframework.data.elasticsearch.IntegerIDEntity;
|
import org.springframework.data.elasticsearch.IntegerIDEntity;
|
||||||
import org.springframework.data.elasticsearch.SampleEntity;
|
|
||||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||||
|
|
||||||
public interface IntegerIDRepository extends ElasticsearchRepository<IntegerIDEntity,Integer> {
|
public interface IntegerIDRepository extends ElasticsearchRepository<IntegerIDEntity,Integer> {
|
||||||
|
@ -377,13 +377,13 @@ public class DoubleIDRepositoryTests {
|
|||||||
Double documentId = RandomUtils.nextDouble();
|
Double documentId = RandomUtils.nextDouble();
|
||||||
DoubleIDEntity sampleEntity = new DoubleIDEntity();
|
DoubleIDEntity sampleEntity = new DoubleIDEntity();
|
||||||
sampleEntity.setId(documentId);
|
sampleEntity.setId(documentId);
|
||||||
sampleEntity.setMessage("A. hello world.");
|
sampleEntity.setMessage("abc");
|
||||||
repository.save(sampleEntity);
|
repository.save(sampleEntity);
|
||||||
|
|
||||||
Double documentId2 = RandomUtils.nextDouble();
|
Double documentId2 = RandomUtils.nextDouble();
|
||||||
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
|
DoubleIDEntity sampleEntity2 = new DoubleIDEntity();
|
||||||
sampleEntity2.setId(documentId2);
|
sampleEntity2.setId(documentId2);
|
||||||
sampleEntity2.setMessage("B.hello world.");
|
sampleEntity2.setMessage("xyz");
|
||||||
repository.save(sampleEntity2);
|
repository.save(sampleEntity2);
|
||||||
//when
|
//when
|
||||||
Iterable<DoubleIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
|
Iterable<DoubleIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
|
||||||
|
@ -377,13 +377,13 @@ public class IntegerIDRepositoryTests {
|
|||||||
Integer documentId = RandomUtils.nextInt();
|
Integer documentId = RandomUtils.nextInt();
|
||||||
IntegerIDEntity sampleEntity = new IntegerIDEntity();
|
IntegerIDEntity sampleEntity = new IntegerIDEntity();
|
||||||
sampleEntity.setId(documentId);
|
sampleEntity.setId(documentId);
|
||||||
sampleEntity.setMessage("A. hello world.");
|
sampleEntity.setMessage("hello");
|
||||||
repository.save(sampleEntity);
|
repository.save(sampleEntity);
|
||||||
|
|
||||||
Integer documentId2 = RandomUtils.nextInt();
|
Integer documentId2 = RandomUtils.nextInt();
|
||||||
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
|
IntegerIDEntity sampleEntity2 = new IntegerIDEntity();
|
||||||
sampleEntity2.setId(documentId2);
|
sampleEntity2.setId(documentId2);
|
||||||
sampleEntity2.setMessage("B.hello world.");
|
sampleEntity2.setMessage("world");
|
||||||
repository.save(sampleEntity2);
|
repository.save(sampleEntity2);
|
||||||
//when
|
//when
|
||||||
Iterable<IntegerIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
|
Iterable<IntegerIDEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
|
||||||
|
@ -373,13 +373,13 @@ public class SimpleElasticsearchRepositoryTests {
|
|||||||
String documentId = randomNumeric(5);
|
String documentId = randomNumeric(5);
|
||||||
SampleEntity sampleEntity = new SampleEntity();
|
SampleEntity sampleEntity = new SampleEntity();
|
||||||
sampleEntity.setId(documentId);
|
sampleEntity.setId(documentId);
|
||||||
sampleEntity.setMessage("A. hello world.");
|
sampleEntity.setMessage("world");
|
||||||
repository.save(sampleEntity);
|
repository.save(sampleEntity);
|
||||||
|
|
||||||
String documentId2 = randomNumeric(5);
|
String documentId2 = randomNumeric(5);
|
||||||
SampleEntity sampleEntity2 = new SampleEntity();
|
SampleEntity sampleEntity2 = new SampleEntity();
|
||||||
sampleEntity2.setId(documentId2);
|
sampleEntity2.setId(documentId2);
|
||||||
sampleEntity2.setMessage("B.hello world.");
|
sampleEntity2.setMessage("hello");
|
||||||
repository.save(sampleEntity2);
|
repository.save(sampleEntity2);
|
||||||
//when
|
//when
|
||||||
Iterable<SampleEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
|
Iterable<SampleEntity> sampleEntities=repository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC,"message")));
|
||||||
|
@ -5,6 +5,6 @@
|
|||||||
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
|
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
|
||||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
<elasticsearch:node-client id="client" local="true" purge-data-on-shutdown="true" />
|
<elasticsearch:node-client id="client" local="true" cluster-name="testCluster" http-enabled="false" />
|
||||||
|
|
||||||
</beans>
|
</beans>
|
@ -5,7 +5,7 @@
|
|||||||
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
|
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
|
||||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
<elasticsearch:node-client id="client" local="true"/>
|
<elasticsearch:node-client id="client" local="true" cluster-name="testCluster" http-enabled="false"/>
|
||||||
|
|
||||||
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
|
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
|
||||||
<constructor-arg name="client" ref="client"/>
|
<constructor-arg name="client" ref="client"/>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user