Skip to content

Commit 751302d

Browse files
msn1444akonczak
authored andcommitted
DATAES-143 added support for turning off auto index creation based on Document annotation.
1 parent 485f0e7 commit 751302d

File tree

11 files changed

+120
-23
lines changed

11 files changed

+120
-23
lines changed

src/main/java/org/springframework/data/elasticsearch/annotations/Document.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*
2525
* @author Rizwan Idrees
2626
* @author Mohsin Husen
27+
* @author Mason Chan
2728
*/
2829

2930
@Persistent
@@ -45,4 +46,6 @@
4546
String refreshInterval() default "1s";
4647

4748
String indexStoreType() default "fs";
49+
50+
boolean createIndex() default true;
4851
}

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchOperations.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.action.update.UpdateResponse;
1919
import org.springframework.data.domain.Page;
2020
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
21+
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
2122
import org.springframework.data.elasticsearch.core.query.*;
2223
import org.springframework.data.util.CloseableIterator;
2324

@@ -558,4 +559,7 @@ public interface ElasticsearchOperations {
558559

559560

560561
<T> T query(SearchQuery query, ResultsExtractor<T> resultsExtractor);
562+
563+
564+
ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz);
561565
}

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
* @author Mohsin Husen
105105
* @author Artur Konczak
106106
* @author Kevin Leturc
107+
* @author Mason Chan
107108
*/
108109

109110
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware {
@@ -1026,7 +1027,8 @@ public Set<String> queryForAlias(String indexName) {
10261027
return newHashSet(iterator);
10271028
}
10281029

1029-
private ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz) {
1030+
@Override
1031+
public ElasticsearchPersistentEntity getPersistentEntityFor(Class clazz) {
10301032
Assert.isTrue(clazz.isAnnotationPresent(Document.class), "Unable to identify index name. " + clazz.getSimpleName()
10311033
+ " is not a Document. Make sure the document class is annotated with @Document(indexName=\"foo\")");
10321034
return elasticsearchConverter.getMappingContext().getPersistentEntity(clazz);

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
4747
ElasticsearchPersistentProperty getParentIdProperty();
4848

4949
String settingPath();
50+
51+
boolean isCreateIndexAndMapping();
5052
}

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
5858
private String parentType;
5959
private ElasticsearchPersistentProperty parentIdProperty;
6060
private String settingPath;
61+
private boolean createIndexAndMapping;
6162

6263
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
6364
super(typeInformation);
@@ -76,6 +77,7 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
7677
this.replicas = document.replicas();
7778
this.refreshInterval = document.refreshInterval();
7879
this.indexStoreType = document.indexStoreType();
80+
this.createIndexAndMapping = document.createIndex();
7981
}
8082
if (clazz.isAnnotationPresent(Setting.class)) {
8183
this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath();
@@ -141,6 +143,11 @@ public String settingPath() {
141143
return settingPath;
142144
}
143145

146+
@Override
147+
public boolean isCreateIndexAndMapping() {
148+
return createIndexAndMapping;
149+
}
150+
144151
@Override
145152
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
146153
super.addPersistentProperty(property);

src/main/java/org/springframework/data/elasticsearch/repository/support/AbstractElasticsearchRepository.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support;
1717

18-
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
18+
import static org.elasticsearch.index.query.QueryBuilders.*;
1919

2020
import java.io.Serializable;
2121
import java.lang.reflect.ParameterizedType;
@@ -30,19 +30,10 @@
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232
import org.springframework.dao.InvalidDataAccessApiUsageException;
33-
import org.springframework.data.domain.Page;
34-
import org.springframework.data.domain.PageImpl;
35-
import org.springframework.data.domain.PageRequest;
36-
import org.springframework.data.domain.Pageable;
37-
import org.springframework.data.domain.Sort;
33+
import org.springframework.data.domain.*;
3834
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
3935
import org.springframework.data.elasticsearch.core.FacetedPage;
40-
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
41-
import org.springframework.data.elasticsearch.core.query.GetQuery;
42-
import org.springframework.data.elasticsearch.core.query.IndexQuery;
43-
import org.springframework.data.elasticsearch.core.query.MoreLikeThisQuery;
44-
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
45-
import org.springframework.data.elasticsearch.core.query.SearchQuery;
36+
import org.springframework.data.elasticsearch.core.query.*;
4637
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
4738
import org.springframework.util.Assert;
4839

@@ -78,8 +69,10 @@ public AbstractElasticsearchRepository(ElasticsearchEntityInformation<T, ID> met
7869
this.entityInformation = metadata;
7970
setEntityClass(this.entityInformation.getJavaType());
8071
try {
81-
createIndex();
82-
putMapping();
72+
if (createIndexAndMapping()) {
73+
createIndex();
74+
putMapping();
75+
}
8376
} catch (ElasticsearchException exception) {
8477
LOGGER.error("failed to load elasticsearch nodes : " + exception.getDetailedMessage());
8578
}
@@ -93,6 +86,10 @@ private void putMapping() {
9386
elasticsearchOperations.putMapping(getEntityClass());
9487
}
9588

89+
private boolean createIndexAndMapping() {
90+
return elasticsearchOperations.getPersistentEntityFor(getEntityClass()).isCreateIndexAndMapping();
91+
}
92+
9693
@Override
9794
public T findOne(ID id) {
9895
GetQuery query = new GetQuery();
@@ -311,14 +308,14 @@ protected ID extractIdFromBean(T entity) {
311308
return null;
312309
}
313310

314-
private List<String> stringIdsRepresentation(Iterable<ID> ids) {
315-
Assert.notNull(ids, "ids can't be null.");
316-
List<String> stringIds = new ArrayList<String>();
317-
for (ID id : ids) {
318-
stringIds.add(stringIdRepresentation(id));
319-
}
320-
return stringIds;
321-
}
311+
private List<String> stringIdsRepresentation(Iterable<ID> ids) {
312+
Assert.notNull(ids, "ids can't be null.");
313+
List<String> stringIds = new ArrayList<String>();
314+
for (ID id : ids) {
315+
stringIds.add(stringIdRepresentation(id));
316+
}
317+
return stringIds;
318+
}
322319

323320
protected abstract String stringIdRepresentation(ID id);
324321

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.data.elasticsearch.annotations.Document;
4949
import org.springframework.data.elasticsearch.core.query.*;
5050
import org.springframework.data.elasticsearch.entities.*;
51+
import org.springframework.data.elasticsearch.repositories.existing.index.CreateIndexFalseEntity;
5152
import org.springframework.data.util.CloseableIterator;
5253
import org.springframework.test.context.ContextConfiguration;
5354
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -58,6 +59,7 @@
5859
* @author Franck Marchand
5960
* @author Abdul Mohammed
6061
* @author Kevin Leturc
62+
* @author Mason Chan
6163
*/
6264
@RunWith(SpringJUnit4ClassRunner.class)
6365
@ContextConfiguration("classpath:elasticsearch-template-test.xml")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.data.elasticsearch.repositories.existing.index;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.elasticsearch.annotations.Document;
5+
6+
/**
7+
* CreateIndexFalseEntity
8+
*
9+
* @author Mason Chan
10+
*/
11+
12+
@Document(indexName = "test-index", type = "test-type", createIndex = false)
13+
public class CreateIndexFalseEntity {
14+
@Id
15+
private String id;
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.springframework.data.elasticsearch.repositories.existing.index;
2+
3+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
4+
5+
/**
6+
* Created by akonczak on 16/12/2015.
7+
*/
8+
public interface CreateIndexFalseRepository extends ElasticsearchRepository<CreateIndexFalseEntity, String> {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.springframework.data.elasticsearch.repositories.existing.index;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
9+
import org.springframework.test.context.ContextConfiguration;
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11+
12+
@RunWith(SpringJUnit4ClassRunner.class)
13+
@ContextConfiguration("classpath:/existing-index-repository-test.xml")
14+
public class CreateIndexFalseRepositoryTest {
15+
16+
@Autowired
17+
private CreateIndexFalseRepository repository;
18+
19+
@Autowired
20+
private ElasticsearchTemplate elasticsearchTemplate;
21+
22+
23+
/*
24+
DATAES-143
25+
*/
26+
@Test
27+
public void shouldNotCreateIndex() {
28+
//given
29+
30+
//when
31+
//then
32+
assertFalse(elasticsearchTemplate.indexExists(CreateIndexFalseEntity.class));
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
7+
8+
<import resource="infrastructure.xml"/>
9+
10+
<bean name="elasticsearchTemplate"
11+
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
12+
<constructor-arg name="client" ref="client"/>
13+
</bean>
14+
15+
16+
<elasticsearch:repositories
17+
base-package="org.springframework.data.elasticsearch.repositories.existing.index"/>
18+
19+
</beans>

0 commit comments

Comments
 (0)