mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-07-05 02:02:12 +00:00
Additional tests to cover synonyms
This commit is contained in:
parent
4a90020dc1
commit
a3828c6161
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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<SynonymEntity, String> {
|
||||||
|
|
||||||
|
}
|
@ -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<SynonymEntity> synonymEntities = elasticsearchTemplate.queryForList(new NativeSearchQueryBuilder().withQuery(QueryBuilders.termQuery("text", "british")).build(), SynonymEntity.class);
|
||||||
|
assertThat(synonymEntities.size(), is(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
18
src/test/resources/synonym-test.xml
Normal file
18
src/test/resources/synonym-test.xml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
|
||||||
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||||
|
|
||||||
|
<import resource="infrastructure.xml"/>
|
||||||
|
|
||||||
|
<bean name="elasticsearchTemplate"
|
||||||
|
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
|
||||||
|
<constructor-arg name="client" ref="client"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<elasticsearch:repositories
|
||||||
|
base-package="org.springframework.data.elasticsearch.repositories.synonym"/>
|
||||||
|
|
||||||
|
</beans>
|
13
src/test/resources/synonyms/mappings.json
Normal file
13
src/test/resources/synonyms/mappings.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"synonym-type": {
|
||||||
|
"_all": {
|
||||||
|
"enabled": true
|
||||||
|
},
|
||||||
|
"properties": {
|
||||||
|
"text": {
|
||||||
|
"type": "string",
|
||||||
|
"analyzer": "synonym_analyzer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
src/test/resources/synonyms/settings-with-external-file.json
Normal file
23
src/test/resources/synonyms/settings-with-external-file.json
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
src/test/resources/synonyms/settings.json
Normal file
26
src/test/resources/synonyms/settings.json
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
src/test/resources/synonyms/synonyms.txt
Normal file
3
src/test/resources/synonyms/synonyms.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
british,english
|
||||||
|
queen,monarch
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user