mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-13 05:43:26 +00:00
DATAES-8 : Add support for Index level configuration
This commit is contained in:
parent
e72521ba48
commit
8c31761308
@ -35,5 +35,8 @@ public @interface Document {
|
|||||||
|
|
||||||
String indexName();
|
String indexName();
|
||||||
String type() default "";
|
String type() default "";
|
||||||
|
short shards() default 1;
|
||||||
|
short replicas() default 5;
|
||||||
|
String refreshInterval() default "1s";
|
||||||
String indexStoreType() default "fs";
|
String indexStoreType() default "fs";
|
||||||
}
|
}
|
||||||
|
@ -382,9 +382,16 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
|||||||
private <T> boolean createIndexWithSettings(Class<T> clazz) {
|
private <T> boolean createIndexWithSettings(Class<T> clazz) {
|
||||||
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
|
||||||
return client.admin().indices().create(Requests.createIndexRequest(persistentEntity.getIndexName()).
|
return client.admin().indices().create(Requests.createIndexRequest(persistentEntity.getIndexName()).
|
||||||
settings(new MapBuilder<String, String>()
|
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())
|
.put("index.store.type", persistentEntity.getIndexStoreType())
|
||||||
.map())).actionGet().acknowledged();
|
.map();
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz){
|
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz){
|
||||||
|
@ -27,6 +27,9 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
|
|||||||
|
|
||||||
String getIndexName();
|
String getIndexName();
|
||||||
String getIndexType();
|
String getIndexType();
|
||||||
|
short getShards();
|
||||||
|
short getReplicas();
|
||||||
|
String getRefreshInterval();
|
||||||
String getIndexStoreType();
|
String getIndexStoreType();
|
||||||
ElasticsearchPersistentProperty getVersionProperty();
|
ElasticsearchPersistentProperty getVersionProperty();
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,9 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
private final StandardEvaluationContext context;
|
private final StandardEvaluationContext context;
|
||||||
private String indexName;
|
private String indexName;
|
||||||
private String indexType;
|
private String indexType;
|
||||||
|
private short shards;
|
||||||
|
private short replicas;
|
||||||
|
private String refreshInterval;
|
||||||
private String indexStoreType;
|
private String indexStoreType;
|
||||||
|
|
||||||
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
|
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\")");
|
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 = typeInformation.getType().getAnnotation(Document.class).indexName();
|
||||||
this.indexType = hasText(document.type())? document.type() : clazz.getSimpleName().toLowerCase(Locale.ENGLISH);
|
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.indexStoreType = typeInformation.getType().getAnnotation(Document.class).indexStoreType();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,10 +83,26 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
|
|||||||
return indexType;
|
return indexType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getIndexStoreType() {
|
public String getIndexStoreType() {
|
||||||
return indexStoreType;
|
return indexStoreType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short getShards() {
|
||||||
|
return shards;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short getReplicas() {
|
||||||
|
return replicas;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRefreshInterval() {
|
||||||
|
return refreshInterval;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
|
||||||
super.addPersistentProperty(property);
|
super.addPersistentProperty(property);
|
||||||
|
@ -21,7 +21,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
|||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @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 {
|
public class Book {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ -20,7 +20,7 @@ import org.springframework.data.annotation.Version;
|
|||||||
import org.springframework.data.elasticsearch.annotations.Document;
|
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 {
|
public class DoubleIDEntity {
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,14 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.elasticsearch;
|
package org.springframework.data.elasticsearch;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
|
||||||
|
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
import org.springframework.data.annotation.Version;
|
import org.springframework.data.annotation.Version;
|
||||||
import org.springframework.data.elasticsearch.annotations.Document;
|
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 {
|
public class IntegerIDEntity {
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
|
|||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @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 {
|
public class SampleEntity {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ -24,7 +24,7 @@ import org.springframework.data.elasticsearch.annotations.Field;
|
|||||||
* @author Rizwan Idrees
|
* @author Rizwan Idrees
|
||||||
* @author Mohsin Husen
|
* @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 {
|
public class SampleMappingEntity {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
Loading…
x
Reference in New Issue
Block a user