mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-23 12:32:10 +00:00
DATAES-88 - add method createIndex(Class<T> clazz, Object settings)
This commit is contained in:
parent
3261af3f5a
commit
230c041d83
@ -61,6 +61,14 @@ public interface ElasticsearchOperations {
|
||||
*/
|
||||
boolean createIndex(String indexName, Object settings);
|
||||
|
||||
/**
|
||||
* Create an index for given class and Settings
|
||||
*
|
||||
* @param clazz
|
||||
* @param settings
|
||||
*/
|
||||
<T> boolean createIndex(Class<T> clazz, Object settings);
|
||||
|
||||
/**
|
||||
* Create mapping for a class
|
||||
*
|
||||
|
@ -643,6 +643,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
return createIndexRequestBuilder.execute().actionGet().isAcknowledged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> boolean createIndex(Class<T> clazz, Object settings) {
|
||||
return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings);
|
||||
}
|
||||
|
||||
private <T> Map getDefaultSettings(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()))
|
||||
|
@ -1377,29 +1377,28 @@ public class ElasticsearchTemplateTests {
|
||||
@Test
|
||||
public void shouldCreateIndexWithGivenSettings() {
|
||||
// given
|
||||
String settings =
|
||||
"{ " +
|
||||
"\"settings\" : { " +
|
||||
"\"index\": { " +
|
||||
"\"analysis\" :{ " +
|
||||
"\"analyzer\": { " +
|
||||
"\"email-analyzer\": { " +
|
||||
"\"type\" : \"custom\"," +
|
||||
"\"tokenizer\" : \"uax_url_email\"," +
|
||||
"\"filter\" : [\"standard\", \"lowercase\", \"stop\"]\n" +
|
||||
"}\n" +
|
||||
"}\n" +
|
||||
"}\n" +
|
||||
"}\n" +
|
||||
"}\n" +
|
||||
"}";
|
||||
String settings = "{\n" +
|
||||
" \"index\": {\n" +
|
||||
" \"number_of_shards\": \"1\",\n" +
|
||||
" \"number_of_replicas\": \"0\",\n" +
|
||||
" \"analysis\": {\n" +
|
||||
" \"analyzer\": {\n" +
|
||||
" \"emailAnalyzer\": {\n" +
|
||||
" \"type\": \"custom\",\n" +
|
||||
" \"tokenizer\": \"uax_url_email\"\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
elasticsearchTemplate.deleteIndex("test-index");
|
||||
// when
|
||||
elasticsearchTemplate.createIndex("test-index", settings);
|
||||
// then
|
||||
Map map = elasticsearchTemplate.getSetting("test-index");
|
||||
boolean hasAnalyzer = map.containsKey("index.settings.index.analysis.analyzer.email-analyzer.tokenizer");
|
||||
String emailAnalyzer = (String) map.get("index.settings.index.analysis.analyzer.email-analyzer.tokenizer");
|
||||
boolean hasAnalyzer = map.containsKey("index.analysis.analyzer.emailAnalyzer.tokenizer");
|
||||
String emailAnalyzer = (String) map.get("index.analysis.analyzer.emailAnalyzer.tokenizer");
|
||||
assertThat(elasticsearchTemplate.indexExists("test-index"), is(true));
|
||||
assertThat(hasAnalyzer, is(true));
|
||||
assertThat(emailAnalyzer, is("uax_url_email"));
|
||||
@ -1426,6 +1425,39 @@ public class ElasticsearchTemplateTests {
|
||||
assertThat((String) map.get("index.store.type"), is("memory"));
|
||||
}
|
||||
|
||||
/*
|
||||
DATAES-88
|
||||
*/
|
||||
@Test
|
||||
public void shouldCreateIndexWithGivenClassAndSettings() {
|
||||
//given
|
||||
String settings = "{\n" +
|
||||
" \"index\": {\n" +
|
||||
" \"number_of_shards\": \"1\",\n" +
|
||||
" \"number_of_replicas\": \"0\",\n" +
|
||||
" \"analysis\": {\n" +
|
||||
" \"analyzer\": {\n" +
|
||||
" \"emailAnalyzer\": {\n" +
|
||||
" \"type\": \"custom\",\n" +
|
||||
" \"tokenizer\": \"uax_url_email\"\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
elasticsearchTemplate.deleteIndex(SampleEntity.class);
|
||||
elasticsearchTemplate.createIndex(SampleEntity.class, settings);
|
||||
elasticsearchTemplate.refresh(SampleEntity.class, true);
|
||||
|
||||
// then
|
||||
Map map = elasticsearchTemplate.getSetting(SampleEntity.class);
|
||||
assertThat(elasticsearchTemplate.indexExists("test-index"), is(true));
|
||||
assertThat(map.containsKey("index.number_of_replicas"), is(true));
|
||||
assertThat(map.containsKey("index.number_of_shards"), is(true));
|
||||
assertThat((String) map.get("index.number_of_replicas"), is("0"));
|
||||
assertThat((String) map.get("index.number_of_shards"), is("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldTestResultsAcrossMultipleIndices() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user