DATAES-8 : Add support for Index level configuration

This commit is contained in:
Mohsin Husen 2013-05-12 09:43:26 +01:00
parent e72521ba48
commit 8c31761308
9 changed files with 43 additions and 10 deletions

View File

@ -35,5 +35,8 @@ public @interface Document {
String indexName();
String type() default "";
short shards() default 1;
short replicas() default 5;
String refreshInterval() default "1s";
String indexStoreType() default "fs";
}

View File

@ -382,9 +382,16 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
private <T> boolean createIndexWithSettings(Class<T> clazz) {
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
return client.admin().indices().create(Requests.createIndexRequest(persistentEntity.getIndexName()).
settings(new MapBuilder<String, String>()
.put("index.store.type", persistentEntity.getIndexStoreType())
.map())).actionGet().acknowledged();
settings(getSettings(persistentEntity))).actionGet().acknowledged();
}
private <T> Map getSettings(ElasticsearchPersistentEntity<T> persistentEntity) {
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())
.put("index.store.type", persistentEntity.getIndexStoreType())
.map();
}
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz){

View File

@ -27,6 +27,9 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
String getIndexName();
String getIndexType();
short getShards();
short getReplicas();
String getRefreshInterval();
String getIndexStoreType();
ElasticsearchPersistentProperty getVersionProperty();
}

View File

@ -45,6 +45,9 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private final StandardEvaluationContext context;
private String indexName;
private String indexType;
private short shards;
private short replicas;
private String refreshInterval;
private String indexStoreType;
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
@ -56,6 +59,9 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
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.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();
}
}
@ -77,10 +83,26 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
return indexType;
}
@Override
public String getIndexStoreType() {
return indexStoreType;
}
@Override
public short getShards() {
return shards;
}
@Override
public short getReplicas() {
return replicas;
}
@Override
public String getRefreshInterval() {
return refreshInterval;
}
@Override
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);

View File

@ -21,7 +21,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@Document(indexName = "book",type = "book", indexStoreType = "memory")
@Document(indexName = "book",type = "book", indexStoreType = "memory", shards = 1 , replicas = 0, refreshInterval = "-1" )
public class Book {
@Id

View File

@ -20,7 +20,7 @@ import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "double-keyed-entity", type = "double-keyed-entity", indexStoreType = "memory")
@Document(indexName = "double-keyed-entity", type = "double-keyed-entity", indexStoreType = "memory", shards = 1 , replicas = 0, refreshInterval = "-1")
public class DoubleIDEntity {

View File

@ -15,14 +15,12 @@
*/
package org.springframework.data.elasticsearch;
import java.math.BigInteger;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "integer-keyed-entity", type = "integer-keyed-entity", indexStoreType = "memory")
@Document(indexName = "integer-keyed-entity", type = "integer-keyed-entity", indexStoreType = "memory", shards = 1 , replicas = 0, refreshInterval = "-1")
public class IntegerIDEntity {

View File

@ -25,7 +25,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@Document(indexName = "test-index", type = "test-type", indexStoreType = "memory")
@Document(indexName = "test-index", type = "test-type", indexStoreType = "memory", shards = 1 , replicas = 0, refreshInterval = "-1")
public class SampleEntity {
@Id

View File

@ -24,7 +24,7 @@ import org.springframework.data.elasticsearch.annotations.Field;
* @author Rizwan Idrees
* @author Mohsin Husen
*/
@Document(indexName = "test-mapping", type = "mapping", indexStoreType = "memory")
@Document(indexName = "test-mapping", type = "mapping", indexStoreType = "memory", shards = 1 , replicas = 0, refreshInterval = "-1")
public class SampleMappingEntity {
@Id