Skip to content

Commit 1e3ec3e

Browse files
committed
DATAES-64 - Add dynamic settings using @setting annotation
1 parent 403930d commit 1e3ec3e

File tree

9 files changed

+330
-1
lines changed

9 files changed

+330
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.annotations;
17+
18+
import java.lang.annotation.*;
19+
20+
import org.springframework.data.annotation.Persistent;
21+
22+
/**
23+
* Elasticsearch Setting
24+
*
25+
* @author Mohsin Husen
26+
*/
27+
28+
@Persistent
29+
@Inherited
30+
@Retention(RetentionPolicy.RUNTIME)
31+
@Target({ElementType.TYPE})
32+
public @interface Setting {
33+
34+
String settingPath() default "";
35+
36+
}

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import static org.elasticsearch.index.VersionType.*;
2424
import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
2525

26+
import java.io.BufferedReader;
2627
import java.io.IOException;
28+
import java.io.InputStreamReader;
2729
import java.lang.reflect.Method;
2830
import java.util.*;
2931

@@ -64,10 +66,14 @@
6466
import org.elasticsearch.search.highlight.HighlightBuilder;
6567
import org.elasticsearch.search.sort.SortBuilder;
6668
import org.elasticsearch.search.sort.SortOrder;
69+
import org.slf4j.Logger;
70+
import org.slf4j.LoggerFactory;
71+
import org.springframework.core.io.ClassPathResource;
6772
import org.springframework.data.domain.Page;
6873
import org.springframework.data.domain.Sort;
6974
import org.springframework.data.elasticsearch.ElasticsearchException;
7075
import org.springframework.data.elasticsearch.annotations.Document;
76+
import org.springframework.data.elasticsearch.annotations.Setting;
7177
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
7278
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
7379
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
@@ -87,6 +93,7 @@
8793

8894
public class ElasticsearchTemplate implements ElasticsearchOperations {
8995

96+
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchTemplate.class);
9097
private Client client;
9198
private ElasticsearchConverter elasticsearchConverter;
9299
private ResultsMapper resultsMapper;
@@ -554,6 +561,17 @@ private <T> boolean createIndexIfNotCreated(Class<T> clazz) {
554561
}
555562

556563
private <T> boolean createIndexWithSettings(Class<T> clazz) {
564+
if(clazz.isAnnotationPresent(Setting.class)) {
565+
String settingPath = clazz.getAnnotation(Setting.class).settingPath();
566+
if(isNotBlank(settingPath)) {
567+
String settings = readFileFromClasspath(settingPath);
568+
if(isNotBlank(settings)) {
569+
return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings);
570+
}
571+
} else {
572+
logger.info("settingPath in @Setting has to be defined. Using default instead.");
573+
}
574+
}
557575
return createIndex(getPersistentEntityFor(clazz).getIndexName(), getDefaultSettings(getPersistentEntityFor(clazz)));
558576
}
559577

@@ -790,4 +808,33 @@ protected ResultsMapper getResultsMapper() {
790808
private boolean isDocument(Class clazz) {
791809
return clazz.isAnnotationPresent(Document.class);
792810
}
811+
812+
public static String readFileFromClasspath(String url) {
813+
StringBuilder stringBuilder = new StringBuilder();
814+
815+
BufferedReader bufferedReader = null;
816+
817+
try {
818+
ClassPathResource classPathResource = new ClassPathResource(url);
819+
InputStreamReader inputStreamReader = new InputStreamReader(classPathResource.getInputStream());
820+
bufferedReader = new BufferedReader(inputStreamReader);
821+
String line;
822+
823+
while ((line = bufferedReader.readLine()) != null) {
824+
stringBuilder.append(line);
825+
}
826+
} catch (Exception e) {
827+
logger.debug(String.format("Failed to load file from url: %s: %s", url, e.getMessage()));
828+
return null;
829+
} finally {
830+
if (bufferedReader != null)
831+
try {
832+
bufferedReader.close();
833+
} catch (IOException e) {
834+
logger.debug(String.format("Unable to close buffered reader.. %s", e.getMessage()));
835+
}
836+
}
837+
838+
return stringBuilder.toString();
839+
}
793840
}

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,4 +43,6 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
4343
String getParentType();
4444

4545
ElasticsearchPersistentProperty getParentIdProperty();
46+
47+
String settingPath();
4648
}

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.context.expression.BeanFactoryResolver;
2727
import org.springframework.data.elasticsearch.annotations.Document;
2828
import org.springframework.data.elasticsearch.annotations.Parent;
29+
import org.springframework.data.elasticsearch.annotations.Setting;
2930
import org.springframework.data.mapping.model.BasicPersistentEntity;
3031
import org.springframework.data.util.TypeInformation;
3132
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -50,6 +51,7 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
5051
private String indexStoreType;
5152
private String parentType;
5253
private ElasticsearchPersistentProperty parentIdProperty;
54+
private String settingPath;
5355

5456
public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
5557
super(typeInformation);
@@ -66,6 +68,9 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
6668
this.refreshInterval = typeInformation.getType().getAnnotation(Document.class).refreshInterval();
6769
this.indexStoreType = typeInformation.getType().getAnnotation(Document.class).indexStoreType();
6870
}
71+
if(clazz.isAnnotationPresent(Setting.class)) {
72+
this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath();
73+
}
6974
}
7075

7176
@Override
@@ -115,6 +120,11 @@ public ElasticsearchPersistentProperty getParentIdProperty() {
115120
return parentIdProperty;
116121
}
117122

123+
@Override
124+
public String settingPath() {
125+
return settingPath;
126+
}
127+
118128
@Override
119129
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
120130
super.addPersistentProperty(property);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.entities;
17+
18+
import org.springframework.data.annotation.Id;
19+
import org.springframework.data.elasticsearch.annotations.Document;
20+
import org.springframework.data.elasticsearch.annotations.Field;
21+
import org.springframework.data.elasticsearch.annotations.FieldType;
22+
import org.springframework.data.elasticsearch.annotations.Setting;
23+
24+
/**
25+
* Sample SettingEntity for test out dynamic setting using @Setting Annotation
26+
*
27+
* @author Mohsin Husen
28+
*/
29+
@Setting(settingPath = "/settings/test-settings.json")
30+
@Document(indexName = "test-setting-index", type = "test-setting-type")
31+
public class SettingEntity {
32+
33+
@Id
34+
private String id;
35+
private String name;
36+
@Field(type = FieldType.String, searchAnalyzer = "emailAnalyzer", indexAnalyzer = "emailAnalyzer")
37+
private String email;
38+
39+
public String getId() {
40+
return id;
41+
}
42+
43+
public void setId(String id) {
44+
this.id = id;
45+
}
46+
47+
public String getName() {
48+
return name;
49+
}
50+
51+
public void setName(String name) {
52+
this.name = name;
53+
}
54+
55+
public String getEmail() {
56+
return email;
57+
}
58+
59+
public void setEmail(String email) {
60+
this.email = email;
61+
}
62+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.repositories.setting;
17+
18+
import org.springframework.data.elasticsearch.entities.SettingEntity;
19+
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
20+
21+
/**
22+
* SettingEntityRepository
23+
*
24+
* @author Mohsin Husen
25+
*/
26+
public interface SettingEntityRepository extends ElasticsearchCrudRepository<SettingEntity, String>{
27+
28+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.repositories.setting;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.assertThat;
20+
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.apache.commons.lang.RandomStringUtils;
25+
import org.elasticsearch.index.query.QueryBuilders;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
31+
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
32+
import org.springframework.data.elasticsearch.core.query.SearchQuery;
33+
import org.springframework.data.elasticsearch.entities.SettingEntity;
34+
import org.springframework.test.context.ContextConfiguration;
35+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36+
37+
/**
38+
* SettingEntityRepositoryTest
39+
*
40+
* @author Mohsin Husen
41+
*/
42+
@RunWith(SpringJUnit4ClassRunner.class)
43+
@ContextConfiguration("classpath:dynamic-settings-test.xml")
44+
public class SettingEntityRepositoryTest {
45+
46+
@Autowired
47+
private SettingEntityRepository repository;
48+
49+
@Autowired
50+
private ElasticsearchTemplate elasticsearchTemplate;
51+
52+
@Before
53+
public void before() {
54+
elasticsearchTemplate.deleteIndex(SettingEntity.class);
55+
elasticsearchTemplate.createIndex(SettingEntity.class);
56+
elasticsearchTemplate.putMapping(SettingEntity.class);
57+
elasticsearchTemplate.refresh(SettingEntity.class, true);
58+
}
59+
60+
/*
61+
DATAES-64
62+
*/
63+
@Test
64+
public void shouldCreateGivenDynamicSettingsForGivenIndex() {
65+
//given
66+
//delete , create and apply mapping in before method
67+
68+
// then
69+
assertThat(elasticsearchTemplate.indexExists(SettingEntity.class), is(true));
70+
Map map = elasticsearchTemplate.getSetting(SettingEntity.class);
71+
assertThat(map.containsKey("index.number_of_replicas"), is(true));
72+
assertThat(map.containsKey("index.number_of_shards"), is(true));
73+
assertThat(map.containsKey("index.analysis.analyzer.emailAnalyzer.tokenizer"), is(true));
74+
assertThat((String) map.get("index.number_of_replicas"), is("0"));
75+
assertThat((String) map.get("index.number_of_shards"), is("1"));
76+
assertThat((String) map.get("index.analysis.analyzer.emailAnalyzer.tokenizer"), is("uax_url_email"));
77+
}
78+
79+
/*
80+
DATAES-64
81+
*/
82+
@Test
83+
public void shouldSearchOnGivenTokenizerUsingGivenDynamicSettingsForGivenIndex() {
84+
//given
85+
SettingEntity settingEntity1 = new SettingEntity();
86+
settingEntity1.setId(RandomStringUtils.randomNumeric(5));
87+
settingEntity1.setName("test-setting1");
88+
settingEntity1.setEmail("[email protected]");
89+
90+
repository.save(settingEntity1);
91+
92+
SettingEntity settingEntity2 = new SettingEntity();
93+
settingEntity2.setId(RandomStringUtils.randomNumeric(5));
94+
settingEntity2.setName("test-setting2");
95+
settingEntity2.setEmail("[email protected]");
96+
97+
repository.save(settingEntity2);
98+
99+
//when
100+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
101+
.withQuery(QueryBuilders.termQuery("email", settingEntity1.getEmail())).build();
102+
103+
long count = elasticsearchTemplate.count(searchQuery, SettingEntity.class);
104+
List<SettingEntity> entityList = elasticsearchTemplate.queryForList(searchQuery, SettingEntity.class);
105+
106+
//then
107+
assertThat(count, is(1L));
108+
assertThat(entityList, is(notNullValue()));
109+
assertThat(entityList.size(), is(1));
110+
assertThat(entityList.get(0).getEmail(), is(settingEntity1.getEmail()));
111+
}
112+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
7+
8+
<import resource="infrastructure.xml"/>
9+
10+
<bean name="elasticsearchTemplate"
11+
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
12+
<constructor-arg name="client" ref="client"/>
13+
</bean>
14+
15+
<elasticsearch:repositories
16+
base-package="org.springframework.data.elasticsearch.repositories.setting"/>
17+
18+
</beans>

0 commit comments

Comments
 (0)