mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-23 20:42:11 +00:00
DATAES-71 - Enhance Create Index in ElasticsearchTemplate
added createIndex(indexName, settings) getSetting(indexName) getSetting(class) methods
This commit is contained in:
parent
6a99d355c0
commit
07f5fab022
@ -17,6 +17,7 @@ package org.springframework.data.elasticsearch.core;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.elasticsearch.action.update.UpdateResponse;
|
||||
@ -52,6 +53,13 @@ public interface ElasticsearchOperations {
|
||||
*/
|
||||
boolean createIndex(String indexName);
|
||||
|
||||
/**
|
||||
* Create an index for given indexName and Settings
|
||||
*
|
||||
* @param indexName
|
||||
* @param settings
|
||||
*/
|
||||
boolean createIndex(String indexName, Object settings);
|
||||
|
||||
/**
|
||||
* Create mapping for a class
|
||||
@ -61,6 +69,21 @@ public interface ElasticsearchOperations {
|
||||
*/
|
||||
<T> boolean putMapping(Class<T> clazz);
|
||||
|
||||
/**
|
||||
* Get settings for a given indexName
|
||||
*
|
||||
* @param indexName
|
||||
*/
|
||||
Map getSetting(String indexName);
|
||||
|
||||
/**
|
||||
* Get settings for a given class
|
||||
*
|
||||
* @param clazz
|
||||
*/
|
||||
<T> Map getSetting(Class<T> clazz);
|
||||
|
||||
|
||||
/**
|
||||
* Execute the query against elasticsearch and return the first returned object
|
||||
*
|
||||
|
@ -30,9 +30,11 @@ import java.util.*;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
|
||||
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder;
|
||||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
|
||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||
import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
|
||||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
|
||||
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
|
||||
import org.elasticsearch.action.bulk.BulkItemResponse;
|
||||
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
||||
import org.elasticsearch.action.bulk.BulkResponse;
|
||||
@ -377,6 +379,7 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
|
||||
|
||||
@Override
|
||||
public boolean deleteIndex(String indexName) {
|
||||
Assert.notNull(indexName, "No index defined for delete operation");
|
||||
if (indexExists(indexName)) {
|
||||
return client.admin().indices().delete(new DeleteIndexRequest(indexName)).actionGet().isAcknowledged();
|
||||
}
|
||||
@ -551,19 +554,42 @@ 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(getSettings(persistentEntity)))
|
||||
.actionGet().isAcknowledged();
|
||||
return createIndex(getPersistentEntityFor(clazz).getIndexName(), getDefaultSettings(getPersistentEntityFor(clazz)));
|
||||
}
|
||||
|
||||
private <T> Map getSettings(ElasticsearchPersistentEntity<T> persistentEntity) {
|
||||
|
||||
@Override
|
||||
public boolean createIndex(String indexName, Object settings) {
|
||||
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName);
|
||||
if (settings instanceof String) {
|
||||
createIndexRequestBuilder.setSettings(String.valueOf(settings));
|
||||
} else if (settings instanceof Map) {
|
||||
createIndexRequestBuilder.setSettings((Map) settings);
|
||||
} else if (settings instanceof XContentBuilder) {
|
||||
createIndexRequestBuilder.setSettings((XContentBuilder) settings);
|
||||
}
|
||||
return createIndexRequestBuilder.execute().actionGet().isAcknowledged();
|
||||
}
|
||||
|
||||
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()))
|
||||
.put("index.refresh_interval", persistentEntity.getRefreshInterval())
|
||||
.put("index.store.type", persistentEntity.getIndexStoreType()).map();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Map getSetting(Class<T> clazz) {
|
||||
return getSetting(getPersistentEntityFor(clazz).getIndexName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map getSetting(String indexName) {
|
||||
Assert.notNull(indexName, "No index defined for getSettings");
|
||||
return client.admin().indices().getSettings(new GetSettingsRequest())
|
||||
.actionGet().getIndexToSettings().get(indexName).getAsMap();
|
||||
}
|
||||
|
||||
private <T> SearchRequestBuilder prepareSearch(Query query, Class<T> clazz) {
|
||||
if (query.getIndices().isEmpty()) {
|
||||
query.addIndices(retrieveIndexNameFromPersistentEntity(clazz));
|
||||
|
@ -1264,7 +1264,7 @@ public class ElasticsearchTemplateTests {
|
||||
|
||||
/*
|
||||
DATAES-71
|
||||
*/
|
||||
*/
|
||||
@Test
|
||||
public void shouldCreatedIndexWithSpecifiedIndexName() {
|
||||
// given
|
||||
@ -1277,7 +1277,7 @@ public class ElasticsearchTemplateTests {
|
||||
|
||||
/*
|
||||
DATAES-72
|
||||
*/
|
||||
*/
|
||||
@Test
|
||||
public void shouldDeleteIndexForSpecifiedIndexName() {
|
||||
// given
|
||||
@ -1331,7 +1331,7 @@ public class ElasticsearchTemplateTests {
|
||||
|
||||
/*
|
||||
DATAES-67
|
||||
*/
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowAnExceptionWhenNoIndexSpecifiedForCountQuery() {
|
||||
// given
|
||||
@ -1351,6 +1351,61 @@ public class ElasticsearchTemplateTests {
|
||||
assertThat(count, is(equalTo(1L)));
|
||||
}
|
||||
|
||||
/*
|
||||
DATAES-71
|
||||
*/
|
||||
@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" +
|
||||
"}";
|
||||
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");
|
||||
assertThat(elasticsearchTemplate.indexExists("test-index"), is(true));
|
||||
assertThat(hasAnalyzer, is(true));
|
||||
assertThat(emailAnalyzer, is("uax_url_email"));
|
||||
}
|
||||
|
||||
/*
|
||||
DATAES-71
|
||||
*/
|
||||
@Test
|
||||
public void shouldCreateGivenSettingsForGivenIndex() {
|
||||
//given
|
||||
//delete , create and apply mapping in before method
|
||||
|
||||
// then
|
||||
Map map = elasticsearchTemplate.getSetting(SampleEntity.class);
|
||||
assertThat(elasticsearchTemplate.indexExists("test-index"), is(true));
|
||||
assertThat(map.containsKey("index.refresh_interval"), is(true));
|
||||
assertThat(map.containsKey("index.number_of_replicas"), is(true));
|
||||
assertThat(map.containsKey("index.number_of_shards"), is(true));
|
||||
assertThat(map.containsKey("index.store.type"), is(true));
|
||||
assertThat((String) map.get("index.refresh_interval"), is("-1"));
|
||||
assertThat((String) map.get("index.number_of_replicas"), is("0"));
|
||||
assertThat((String) map.get("index.number_of_shards"), is("1"));
|
||||
assertThat((String) map.get("index.store.type"), is("memory"));
|
||||
}
|
||||
|
||||
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
|
||||
return new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity).build();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user