DATAES-143 added support for turning off auto index creation based on Document annotation.
This commit is contained in:
committed by
Artur Konczak
parent
485f0e7252
commit
751302d6f0
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -47,4 +47,6 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
||||
ElasticsearchPersistentProperty getParentIdProperty();
|
||||
|
||||
String settingPath();
|
||||
|
||||
boolean isCreateIndexAndMapping();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user