mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-28 06:42:29 +00:00
DATAES-208 - Allow node configuration to honor the server settings in @Document
This commit is contained in:
parent
21ff7e50cf
commit
aa9c47516c
@ -36,6 +36,8 @@ public @interface Document {
|
||||
|
||||
String type() default "";
|
||||
|
||||
boolean useServerConfiguration() default false;
|
||||
|
||||
short shards() default 5;
|
||||
|
||||
short replicas() default 1;
|
||||
|
@ -62,6 +62,7 @@ import org.elasticsearch.cluster.metadata.AliasAction;
|
||||
import org.elasticsearch.cluster.metadata.MappingMetaData;
|
||||
import org.elasticsearch.common.collect.ImmutableOpenMap;
|
||||
import org.elasticsearch.common.collect.MapBuilder;
|
||||
import org.elasticsearch.common.collect.Maps;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.index.query.FilterBuilder;
|
||||
@ -879,6 +880,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
|
||||
}
|
||||
|
||||
private <T> Map getDefaultSettings(ElasticsearchPersistentEntity<T> persistentEntity) {
|
||||
|
||||
if (persistentEntity.isUseServerConfiguration())
|
||||
return Maps.newHashMap();
|
||||
|
||||
return new MapBuilder<String, String>().put("index.number_of_shards", String.valueOf(persistentEntity.getShards()))
|
||||
.put("index.number_of_replicas", String.valueOf(persistentEntity.getReplicas()))
|
||||
.put("index.refresh_interval", persistentEntity.getRefreshInterval())
|
||||
|
@ -34,6 +34,8 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
||||
|
||||
short getReplicas();
|
||||
|
||||
boolean isUseServerConfiguration();
|
||||
|
||||
String getRefreshInterval();
|
||||
|
||||
String getIndexStoreType();
|
||||
|
@ -50,6 +50,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
||||
|
||||
private String indexName;
|
||||
private String indexType;
|
||||
private boolean useServerConfiguration;
|
||||
private short shards;
|
||||
private short replicas;
|
||||
private String refreshInterval;
|
||||
@ -68,12 +69,13 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
||||
Document document = clazz.getAnnotation(Document.class);
|
||||
Assert.hasText(document.indexName(),
|
||||
" Unknown indexName. Make sure the indexName is defined. e.g @Document(indexName=\"foo\")");
|
||||
this.indexName = typeInformation.getType().getAnnotation(Document.class).indexName();
|
||||
this.indexName = document.indexName();
|
||||
this.indexType = hasText(document.type()) ? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH);
|
||||
this.shards = typeInformation.getType().getAnnotation(Document.class).shards();
|
||||
this.replicas = typeInformation.getType().getAnnotation(Document.class).replicas();
|
||||
this.refreshInterval = typeInformation.getType().getAnnotation(Document.class).refreshInterval();
|
||||
this.indexStoreType = typeInformation.getType().getAnnotation(Document.class).indexStoreType();
|
||||
this.useServerConfiguration = document.useServerConfiguration();
|
||||
this.shards = document.shards();
|
||||
this.replicas = document.replicas();
|
||||
this.refreshInterval = document.refreshInterval();
|
||||
this.indexStoreType = document.indexStoreType();
|
||||
}
|
||||
if (clazz.isAnnotationPresent(Setting.class)) {
|
||||
this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath();
|
||||
@ -114,6 +116,11 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
||||
return replicas;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUseServerConfiguration() {
|
||||
return useServerConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRefreshInterval() {
|
||||
return refreshInterval;
|
||||
|
@ -34,6 +34,7 @@ import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@ -46,10 +47,7 @@ import org.springframework.data.domain.Sort;
|
||||
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.HetroEntity1;
|
||||
import org.springframework.data.elasticsearch.entities.HetroEntity2;
|
||||
import org.springframework.data.elasticsearch.entities.SampleEntity;
|
||||
import org.springframework.data.elasticsearch.entities.SampleMappingEntity;
|
||||
import org.springframework.data.elasticsearch.entities.*;
|
||||
import org.springframework.data.util.CloseableIterator;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@ -79,6 +77,7 @@ public class ElasticsearchTemplateTests {
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.deleteIndex(INDEX_1_NAME);
|
||||
elasticsearchTemplate.deleteIndex(INDEX_2_NAME);
|
||||
elasticsearchTemplate.deleteIndex(UseServerConfigurationEntity.class);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
}
|
||||
|
||||
@ -535,8 +534,11 @@ public class ElasticsearchTemplateTests {
|
||||
public void shouldCreateIndexGivenEntityClass() {
|
||||
// when
|
||||
boolean created = elasticsearchTemplate.createIndex(SampleEntity.class);
|
||||
final Map setting = elasticsearchTemplate.getSetting(SampleEntity.class);
|
||||
// then
|
||||
assertThat(created, is(true));
|
||||
assertThat(setting.get("index.number_of_shards"), Matchers.<Object>is("1"));
|
||||
assertThat(setting.get("index.number_of_replicas"), Matchers.<Object>is("0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -1901,6 +1903,18 @@ public class ElasticsearchTemplateTests {
|
||||
assertThat(page.getTotalElements(), is(2l));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateIndexUsingServerDefaultConfiguration() {
|
||||
//given
|
||||
|
||||
//when
|
||||
boolean created = elasticsearchTemplate.createIndex(UseServerConfigurationEntity.class);
|
||||
//then
|
||||
assertThat(created, is(true));
|
||||
final Map setting = elasticsearchTemplate.getSetting(UseServerConfigurationEntity.class);
|
||||
assertThat(setting.get("index.number_of_shards"), Matchers.<Object>is("5"));
|
||||
assertThat(setting.get("index.number_of_replicas"), Matchers.<Object>is("1"));
|
||||
}
|
||||
|
||||
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
|
||||
return new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity).build();
|
||||
|
@ -0,0 +1,23 @@
|
||||
package org.springframework.data.elasticsearch.entities;
|
||||
|
||||
import lombok.*;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
|
||||
/**
|
||||
* Created by akonczak on 12/12/2015.
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@Document(indexName = "test-index-server-configuration", type = "test-type", useServerConfiguration = true, indexStoreType = "memory", shards = 10, replicas = 10, refreshInterval = "-1")
|
||||
public class UseServerConfigurationEntity {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String val;
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user