Skip to content

Commit a3828c6

Browse files
committed
Additional tests to cover synonyms
1 parent 4a90020 commit a3828c6

File tree

8 files changed

+240
-0
lines changed

8 files changed

+240
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.Mapping;
21+
import org.springframework.data.elasticsearch.annotations.Setting;
22+
23+
/**
24+
* Sample DynamicSettingAndMappingEntity for test out dynamic setting using @Setting Annotation
25+
*
26+
* @author Mohsin Husen
27+
*/
28+
@Document(indexName = "synonym-index", type = "synonym-type")
29+
@Setting(settingPath = "/synonyms/settings.json")
30+
@Mapping(mappingPath = "/synonyms/mappings.json")
31+
public class SynonymEntity {
32+
33+
@Id
34+
private String id;
35+
private String text;
36+
37+
public String getId() {
38+
return id;
39+
}
40+
41+
public void setId(String id) {
42+
this.id = id;
43+
}
44+
45+
public String getText() {
46+
return text;
47+
}
48+
49+
public void setText(String text) {
50+
this.text = text;
51+
}
52+
}
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.synonym;
17+
18+
import org.springframework.data.elasticsearch.entities.SynonymEntity;
19+
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
20+
21+
/**
22+
* SynonymRepository
23+
*
24+
* @author Artur Konczak
25+
*/
26+
public interface SynonymRepository extends ElasticsearchCrudRepository<SynonymEntity, String> {
27+
28+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.synonym;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import java.util.List;
22+
23+
import org.elasticsearch.index.query.QueryBuilders;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
29+
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
30+
import org.springframework.data.elasticsearch.entities.SynonymEntity;
31+
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33+
34+
/**
35+
* SynonymRepositoryTests
36+
*
37+
* @author Artur Konczak
38+
*/
39+
@RunWith(SpringJUnit4ClassRunner.class)
40+
@ContextConfiguration("classpath:synonym-test.xml")
41+
public class SynonymRepositoryTests {
42+
43+
@Autowired
44+
private SynonymRepository repository;
45+
46+
@Autowired
47+
private ElasticsearchTemplate elasticsearchTemplate;
48+
49+
@Before
50+
public void before() {
51+
elasticsearchTemplate.deleteIndex(SynonymEntity.class);
52+
elasticsearchTemplate.createIndex(SynonymEntity.class);
53+
elasticsearchTemplate.putMapping(SynonymEntity.class);
54+
elasticsearchTemplate.refresh(SynonymEntity.class, true);
55+
}
56+
57+
58+
@Test
59+
public void shouldDo() {
60+
//given
61+
SynonymEntity entry1 = new SynonymEntity();
62+
entry1.setText("Elizabeth is the English queen");
63+
SynonymEntity entry2 = new SynonymEntity();
64+
entry2.setText("Other text");
65+
66+
repository.save(entry1);
67+
repository.save(entry2);
68+
//when
69+
70+
//then
71+
assertThat(repository.count(),is(2L));
72+
73+
List<SynonymEntity> synonymEntities = elasticsearchTemplate.queryForList(new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("text", "british")).build(), SynonymEntity.class);
74+
assertThat(synonymEntities.size(), is(1));
75+
}
76+
77+
}

src/test/resources/synonym-test.xml

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.synonym"/>
17+
18+
</beans>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"synonym-type": {
3+
"_all": {
4+
"enabled": true
5+
},
6+
"properties": {
7+
"text": {
8+
"type": "string",
9+
"analyzer": "synonym_analyzer"
10+
}
11+
}
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"index": {
3+
"number_of_shards": "1",
4+
"number_of_replicas": "0",
5+
"analysis": {
6+
"analyzer": {
7+
"synonym_analyzer": {
8+
"tokenizer": "whitespace",
9+
"filter": [
10+
"synonym_filter"
11+
]
12+
}
13+
},
14+
"filter": {
15+
"synonym_filter": {
16+
"type": "synonym",
17+
"synonyms_path": "synonyms.txt",
18+
"ignore_case": "true"
19+
}
20+
}
21+
}
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"index": {
3+
"number_of_shards": "1",
4+
"number_of_replicas": "0",
5+
"analysis": {
6+
"analyzer": {
7+
"synonym_analyzer": {
8+
"tokenizer": "whitespace",
9+
"filter": [
10+
"synonym_filter"
11+
]
12+
}
13+
},
14+
"filter": {
15+
"synonym_filter": {
16+
"type": "synonym",
17+
"synonyms": [
18+
"british,english",
19+
"queen,monarch"
20+
],
21+
"ignore_case": "true"
22+
}
23+
}
24+
}
25+
}
26+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
british,english
2+
queen,monarch
3+

0 commit comments

Comments
 (0)