From a3828c6161daa9fe31eacdcc93a286119ac470fd Mon Sep 17 00:00:00 2001 From: Artur Konczak Date: Fri, 22 May 2015 13:19:57 +0200 Subject: [PATCH] Additional tests to cover synonyms --- .../elasticsearch/entities/SynonymEntity.java | 52 +++++++++++++ .../synonym/SynonymRepository.java | 28 +++++++ .../synonym/SynonymRepositoryTests.java | 77 +++++++++++++++++++ src/test/resources/synonym-test.xml | 18 +++++ src/test/resources/synonyms/mappings.json | 13 ++++ .../synonyms/settings-with-external-file.json | 23 ++++++ src/test/resources/synonyms/settings.json | 26 +++++++ src/test/resources/synonyms/synonyms.txt | 3 + 8 files changed, 240 insertions(+) create mode 100644 src/test/java/org/springframework/data/elasticsearch/entities/SynonymEntity.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/repositories/synonym/SynonymRepository.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/repositories/synonym/SynonymRepositoryTests.java create mode 100644 src/test/resources/synonym-test.xml create mode 100644 src/test/resources/synonyms/mappings.json create mode 100644 src/test/resources/synonyms/settings-with-external-file.json create mode 100644 src/test/resources/synonyms/settings.json create mode 100644 src/test/resources/synonyms/synonyms.txt diff --git a/src/test/java/org/springframework/data/elasticsearch/entities/SynonymEntity.java b/src/test/java/org/springframework/data/elasticsearch/entities/SynonymEntity.java new file mode 100644 index 000000000..ddb61b99f --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/entities/SynonymEntity.java @@ -0,0 +1,52 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.entities; + +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.Mapping; +import org.springframework.data.elasticsearch.annotations.Setting; + +/** + * Sample DynamicSettingAndMappingEntity for test out dynamic setting using @Setting Annotation + * + * @author Mohsin Husen + */ +@Document(indexName = "synonym-index", type = "synonym-type") +@Setting(settingPath = "/synonyms/settings.json") +@Mapping(mappingPath = "/synonyms/mappings.json") +public class SynonymEntity { + + @Id + private String id; + private String text; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/synonym/SynonymRepository.java b/src/test/java/org/springframework/data/elasticsearch/repositories/synonym/SynonymRepository.java new file mode 100644 index 000000000..8be19e09e --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/synonym/SynonymRepository.java @@ -0,0 +1,28 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.repositories.synonym; + +import org.springframework.data.elasticsearch.entities.SynonymEntity; +import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository; + +/** + * SynonymRepository + * + * @author Artur Konczak + */ +public interface SynonymRepository extends ElasticsearchCrudRepository { + +} diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/synonym/SynonymRepositoryTests.java b/src/test/java/org/springframework/data/elasticsearch/repositories/synonym/SynonymRepositoryTests.java new file mode 100644 index 000000000..fb6790ff8 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/synonym/SynonymRepositoryTests.java @@ -0,0 +1,77 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.repositories.synonym; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import java.util.List; + +import org.elasticsearch.index.query.QueryBuilders; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; +import org.springframework.data.elasticsearch.entities.SynonymEntity; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * SynonymRepositoryTests + * + * @author Artur Konczak + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:synonym-test.xml") +public class SynonymRepositoryTests { + + @Autowired + private SynonymRepository repository; + + @Autowired + private ElasticsearchTemplate elasticsearchTemplate; + + @Before + public void before() { + elasticsearchTemplate.deleteIndex(SynonymEntity.class); + elasticsearchTemplate.createIndex(SynonymEntity.class); + elasticsearchTemplate.putMapping(SynonymEntity.class); + elasticsearchTemplate.refresh(SynonymEntity.class, true); + } + + + @Test + public void shouldDo() { + //given + SynonymEntity entry1 = new SynonymEntity(); + entry1.setText("Elizabeth is the English queen"); + SynonymEntity entry2 = new SynonymEntity(); + entry2.setText("Other text"); + + repository.save(entry1); + repository.save(entry2); + //when + + //then + assertThat(repository.count(),is(2L)); + + List synonymEntities = elasticsearchTemplate.queryForList(new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("text", "british")).build(), SynonymEntity.class); + assertThat(synonymEntities.size(), is(1)); + } + +} diff --git a/src/test/resources/synonym-test.xml b/src/test/resources/synonym-test.xml new file mode 100644 index 000000000..858c40129 --- /dev/null +++ b/src/test/resources/synonym-test.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + diff --git a/src/test/resources/synonyms/mappings.json b/src/test/resources/synonyms/mappings.json new file mode 100644 index 000000000..815892a6a --- /dev/null +++ b/src/test/resources/synonyms/mappings.json @@ -0,0 +1,13 @@ +{ + "synonym-type": { + "_all": { + "enabled": true + }, + "properties": { + "text": { + "type": "string", + "analyzer": "synonym_analyzer" + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/synonyms/settings-with-external-file.json b/src/test/resources/synonyms/settings-with-external-file.json new file mode 100644 index 000000000..22ce439d2 --- /dev/null +++ b/src/test/resources/synonyms/settings-with-external-file.json @@ -0,0 +1,23 @@ +{ + "index": { + "number_of_shards": "1", + "number_of_replicas": "0", + "analysis": { + "analyzer": { + "synonym_analyzer": { + "tokenizer": "whitespace", + "filter": [ + "synonym_filter" + ] + } + }, + "filter": { + "synonym_filter": { + "type": "synonym", + "synonyms_path": "synonyms.txt", + "ignore_case": "true" + } + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/synonyms/settings.json b/src/test/resources/synonyms/settings.json new file mode 100644 index 000000000..d53189fb5 --- /dev/null +++ b/src/test/resources/synonyms/settings.json @@ -0,0 +1,26 @@ +{ + "index": { + "number_of_shards": "1", + "number_of_replicas": "0", + "analysis": { + "analyzer": { + "synonym_analyzer": { + "tokenizer": "whitespace", + "filter": [ + "synonym_filter" + ] + } + }, + "filter": { + "synonym_filter": { + "type": "synonym", + "synonyms": [ + "british,english", + "queen,monarch" + ], + "ignore_case": "true" + } + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/synonyms/synonyms.txt b/src/test/resources/synonyms/synonyms.txt new file mode 100644 index 000000000..df092c4a0 --- /dev/null +++ b/src/test/resources/synonyms/synonyms.txt @@ -0,0 +1,3 @@ +british,english +queen,monarch +