DATAES-143 added support for turning off auto index creation based on Document annotation.

This commit is contained in:
Mason Chan 2014-12-30 20:13:30 -06:00 committed by Artur Konczak
parent 485f0e7252
commit 751302d6f0
11 changed files with 120 additions and 23 deletions

View File

@ -24,6 +24,7 @@ import org.springframework.data.annotation.Persistent;
*
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Mason Chan
*/
@Persistent
@ -45,4 +46,6 @@ public @interface Document {
String refreshInterval() default "1s";
String indexStoreType() default "fs";
boolean createIndex() default true;
}

View File

@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.core;
import org.elasticsearch.action.update.UpdateResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.data.util.CloseableIterator;
@ -558,4 +559,7 @@ public interface ElasticsearchOperations {
<T> T query(SearchQuery query, ResultsExtractor<T> resultsExtractor);
ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz);
}

View File

@ -104,6 +104,7 @@ import org.springframework.util.Assert;
* @author Mohsin Husen
* @author Artur Konczak
* @author Kevin Leturc
* @author Mason Chan
*/
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
@ -1026,7 +1027,8 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
return newHashSet(iterator);
}
private ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz) {
@Override
public ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz) {
Assert.isTrue(clazz.isAnnotationPresent(Document.class), "Unable to identify index name. " + clazz.getSimpleName()
+ " is not a Document. Make sure the document class is annotated with @Document(indexName=\"foo\")");
return elasticsearchConverter.getMappingContext().getPersistentEntity(clazz);

View File

@ -47,4 +47,6 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
ElasticsearchPersistentProperty getParentIdProperty();
String settingPath();
boolean isCreateIndexAndMapping();
}

View File

@ -58,6 +58,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private String parentType;
private ElasticsearchPersistentProperty parentIdProperty;
private String settingPath;
private boolean createIndexAndMapping;
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
super(typeInformation);
@ -76,6 +77,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
this.replicas = document.replicas();
this.refreshInterval = document.refreshInterval();
this.indexStoreType = document.indexStoreType();
this.createIndexAndMapping = document.createIndex();
}
if (clazz.isAnnotationPresent(Setting.class)) {
this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath();
@ -141,6 +143,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
return settingPath;
}
@Override
public boolean isCreateIndexAndMapping() {
return createIndexAndMapping;
}
@Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);

View File

@ -15,7 +15,7 @@
*/
package org.springframework.data.elasticsearch.repository.support;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.*;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
@ -30,19 +30,10 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.*;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.FacetedPage;
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.util.Assert;
@ -78,8 +69,10 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
this.entityInformation = metadata;
setEntityClass(this.entityInformation.getJavaType());
try {
createIndex();
putMapping();
if (createIndexAndMapping()) {
createIndex();
putMapping();
}
} catch (ElasticsearchException exception) {
LOGGER.error("failed to load elasticsearch nodes : " + exception.getDetailedMessage());
}
@ -93,6 +86,10 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
elasticsearchOperations.putMapping(getEntityClass());
}
private boolean createIndexAndMapping() {
return elasticsearchOperations.getPersistentEntityFor(getEntityClass()).isCreateIndexAndMapping();
}
@Override
public T findOne(ID id) {
GetQuery query = new GetQuery();
@ -311,14 +308,14 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
return null;
}
private List<String> stringIdsRepresentation(Iterable<ID> ids) {
Assert.notNull(ids, "ids can't be null.");
List<String> stringIds = new ArrayList<String>();
for (ID id : ids) {
stringIds.add(stringIdRepresentation(id));
}
return stringIds;
}
private List<String> stringIdsRepresentation(Iterable<ID> ids) {
Assert.notNull(ids, "ids can't be null.");
List<String> stringIds = new ArrayList<String>();
for (ID id : ids) {
stringIds.add(stringIdRepresentation(id));
}
return stringIds;
}
protected abstract String stringIdRepresentation(ID id);

View File

@ -48,6 +48,7 @@ import org.springframework.data.elasticsearch.ElasticsearchException;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.data.elasticsearch.entities.*;
import org.springframework.data.elasticsearch.repositories.existing.index.CreateIndexFalseEntity;
import org.springframework.data.util.CloseableIterator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -58,6 +59,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Franck Marchand
* @author Abdul Mohammed
* @author Kevin Leturc
* @author Mason Chan
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:elasticsearch-template-test.xml")

View File

@ -0,0 +1,17 @@
package org.springframework.data.elasticsearch.repositories.existing.index;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* CreateIndexFalseEntity
*
* @author Mason Chan
*/
@Document(indexName = "test-index", type = "test-type", createIndex = false)
public class CreateIndexFalseEntity {
@Id
private String id;
}

View File

@ -0,0 +1,10 @@
package org.springframework.data.elasticsearch.repositories.existing.index;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* Created by akonczak on 16/12/2015.
*/
public interface CreateIndexFalseRepository extends ElasticsearchRepository<CreateIndexFalseEntity, String> {
}

View File

@ -0,0 +1,34 @@
package org.springframework.data.elasticsearch.repositories.existing.index;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/existing-index-repository-test.xml")
public class CreateIndexFalseRepositoryTest {
@Autowired
private CreateIndexFalseRepository repository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
/*
DATAES-143
*/
@Test
public void shouldNotCreateIndex() {
//given
//when
//then
assertFalse(elasticsearchTemplate.indexExists(CreateIndexFalseEntity.class));
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
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-3.1.xsd">
<import resource="infrastructure.xml"/>
<bean name="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
</bean>
<elasticsearch:repositories
base-package="org.springframework.data.elasticsearch.repositories.existing.index"/>
</beans>