mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-08 21:22:12 +00:00
DATAES-456 - re-enable dynamic index name evaluation.
Original PR: #221
This commit is contained in:
parent
57ba74e543
commit
bf4948239e
@ -19,7 +19,6 @@ import org.elasticsearch.index.VersionType;
|
|||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||||
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
import org.springframework.data.repository.core.support.PersistentEntityInformation;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Elasticsearch specific implementation of
|
* Elasticsearch specific implementation of
|
||||||
@ -34,6 +33,7 @@ import org.springframework.util.Assert;
|
|||||||
* @author Mark Paluch
|
* @author Mark Paluch
|
||||||
* @author Christoph Strobl
|
* @author Christoph Strobl
|
||||||
* @author Ivan Greene
|
* @author Ivan Greene
|
||||||
|
* @author Sylvain Laurent
|
||||||
*/
|
*/
|
||||||
public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEntityInformation<T, ID>
|
public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEntityInformation<T, ID>
|
||||||
implements ElasticsearchEntityInformation<T, ID> {
|
implements ElasticsearchEntityInformation<T, ID> {
|
||||||
@ -50,9 +50,6 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
|
|||||||
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type, VersionType versionType) {
|
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type, VersionType versionType) {
|
||||||
super(entity);
|
super(entity);
|
||||||
|
|
||||||
Assert.notNull(indexName, "IndexName must not be null!");
|
|
||||||
Assert.notNull(type, "IndexType must not be null!");
|
|
||||||
|
|
||||||
this.entityMetadata = entity;
|
this.entityMetadata = entity;
|
||||||
this.indexName = indexName;
|
this.indexName = indexName;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
@ -66,12 +63,12 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getIndexName() {
|
public String getIndexName() {
|
||||||
return indexName;
|
return indexName != null ? indexName : entityMetadata.getIndexName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return type;
|
return type != null ? type : entityMetadata.getIndexType();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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<DynamicIndexEntity, String> {
|
||||||
|
|
||||||
|
}
|
18
src/test/resources/dynamic-index-repository-test.xml
Normal file
18
src/test/resources/dynamic-index-repository-test.xml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?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 https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
|
||||||
|
http://www.springframework.org/schema/beans https://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.dynamicindex"/>
|
||||||
|
|
||||||
|
</beans>
|
Loading…
x
Reference in New Issue
Block a user