diff --git a/src/main/java/org/springframework/data/elasticsearch/repository/support/MappingElasticsearchEntityInformation.java b/src/main/java/org/springframework/data/elasticsearch/repository/support/MappingElasticsearchEntityInformation.java index be0ea6bb9..689cd097a 100644 --- a/src/main/java/org/springframework/data/elasticsearch/repository/support/MappingElasticsearchEntityInformation.java +++ b/src/main/java/org/springframework/data/elasticsearch/repository/support/MappingElasticsearchEntityInformation.java @@ -19,7 +19,6 @@ import org.elasticsearch.index.VersionType; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.repository.core.support.PersistentEntityInformation; -import org.springframework.util.Assert; /** * Elasticsearch specific implementation of @@ -34,6 +33,7 @@ import org.springframework.util.Assert; * @author Mark Paluch * @author Christoph Strobl * @author Ivan Greene + * @author Sylvain Laurent */ public class MappingElasticsearchEntityInformation extends PersistentEntityInformation implements ElasticsearchEntityInformation { @@ -50,9 +50,6 @@ public class MappingElasticsearchEntityInformation extends PersistentEnti public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity entity, String indexName, String type, VersionType versionType) { super(entity); - Assert.notNull(indexName, "IndexName must not be null!"); - Assert.notNull(type, "IndexType must not be null!"); - this.entityMetadata = entity; this.indexName = indexName; this.type = type; @@ -66,12 +63,12 @@ public class MappingElasticsearchEntityInformation extends PersistentEnti @Override public String getIndexName() { - return indexName; + return indexName != null ? indexName : entityMetadata.getIndexName(); } @Override public String getType() { - return type; + return type != null ? type : entityMetadata.getIndexType(); } @Override diff --git a/src/test/java/org/springframework/data/elasticsearch/DynamicIndexEntityTests.java b/src/test/java/org/springframework/data/elasticsearch/DynamicIndexEntityTests.java new file mode 100644 index 000000000..5dc68b835 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/DynamicIndexEntityTests.java @@ -0,0 +1,103 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ImportResource; +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.entities.DynamicIndexEntity; +import org.springframework.data.elasticsearch.repositories.dynamicindex.DynamicIndexRepository; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * DynamicIndexEntityTests + * + * @author Sylvain Laurent + */ + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = DynamicIndexEntityTests.TestConfig.class) +public class DynamicIndexEntityTests { + + @Autowired private DynamicIndexRepository repository; + + @Autowired private ElasticsearchTemplate template; + + @Autowired private IndexNameProvider indexNameProvider; + + @Before + public void init() { + deleteIndexes(); + template.createIndex("index1"); + template.createIndex("index2"); + } + + @After + public void teardown() { + deleteIndexes(); + } + + @Test // DATAES-456 + public void indexNameIsDynamicallyProvided() { + int initialCallsCount = indexNameProvider.callsCount; + + indexNameProvider.setIndexName("index1"); + repository.save(new DynamicIndexEntity()); + assertTrue(indexNameProvider.callsCount > initialCallsCount); + assertEquals(1L, repository.count()); + + indexNameProvider.setIndexName("index2"); + assertEquals(0L, repository.count()); + + } + + @ImportResource(value = "classpath:/dynamic-index-repository-test.xml") + static class TestConfig { + + @Bean + public IndexNameProvider indexNameProvider() { + return new IndexNameProvider(); + } + } + + static class IndexNameProvider { + private String indexName; + int callsCount; + + public String getIndexName() { + callsCount++; + return indexName; + } + + public void setIndexName(String indexName) { + this.indexName = indexName; + } + } + + private void deleteIndexes() { + template.deleteIndex("index1"); + template.deleteIndex("index2"); + } + +} diff --git a/src/test/java/org/springframework/data/elasticsearch/entities/DynamicIndexEntity.java b/src/test/java/org/springframework/data/elasticsearch/entities/DynamicIndexEntity.java new file mode 100644 index 000000000..ea9477d24 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/entities/DynamicIndexEntity.java @@ -0,0 +1,39 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.entities; + +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; + +/** + * DynamicIndexEntity + * + * @author Sylvain Laurent + */ + +@Document(indexName = "#{@indexNameProvider.getIndexName()}", createIndex = false) +public class DynamicIndexEntity { + + @Id private String id; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } +} diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/dynamicindex/DynamicIndexRepository.java b/src/test/java/org/springframework/data/elasticsearch/repositories/dynamicindex/DynamicIndexRepository.java new file mode 100644 index 000000000..b87a9bdb0 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/dynamicindex/DynamicIndexRepository.java @@ -0,0 +1,28 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.repositories.dynamicindex; + +import org.springframework.data.elasticsearch.entities.DynamicIndexEntity; +import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; + +/** + * DynamicIndexRepository + * + * @author Sylvain Laurent + */ +public interface DynamicIndexRepository extends ElasticsearchRepository { + +} diff --git a/src/test/resources/dynamic-index-repository-test.xml b/src/test/resources/dynamic-index-repository-test.xml new file mode 100644 index 000000000..e48b1d848 --- /dev/null +++ b/src/test/resources/dynamic-index-repository-test.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + +