|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch (the "Author") under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. Author licenses this |
| 6 | + * file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.index.analysis; |
| 21 | + |
| 22 | +import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse; |
| 23 | +import org.elasticsearch.action.search.SearchResponse; |
| 24 | +import org.elasticsearch.common.settings.ImmutableSettings; |
| 25 | +import org.elasticsearch.common.settings.Settings; |
| 26 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 27 | +import org.elasticsearch.index.query.QueryBuilders; |
| 28 | +import org.elasticsearch.test.ElasticsearchIntegrationTest; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.concurrent.ExecutionException; |
| 33 | + |
| 34 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 35 | +import static org.hamcrest.CoreMatchers.is; |
| 36 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 37 | + |
| 38 | +@ElasticsearchIntegrationTest.ClusterScope(numDataNodes = 1, scope = ElasticsearchIntegrationTest.Scope.SUITE) |
| 39 | +public class SimplePhoneticIntegrationTests extends ElasticsearchIntegrationTest { |
| 40 | + |
| 41 | + @Override |
| 42 | + public Settings indexSettings() { |
| 43 | + Settings settings = ImmutableSettings.builder() |
| 44 | + .put(super.indexSettings()) |
| 45 | + .put("index.analysis.analyzer.my_analyzer.tokenizer", "standard") |
| 46 | + .putArray("index.analysis.analyzer.my_analyzer.filter", "standard", "lowercase", "my_metaphone") |
| 47 | + .put("index.analysis.filter.my_metaphone.type", "phonetic") |
| 48 | + .put("index.analysis.filter.my_metaphone.encoder", "metaphone") |
| 49 | + .put("index.analysis.filter.my_metaphone.replace", false) |
| 50 | + .build(); |
| 51 | + |
| 52 | + return settings; |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testPhoneticAnalyzer() throws ExecutionException, InterruptedException { |
| 57 | + createIndex("test"); |
| 58 | + ensureGreen("test"); |
| 59 | + AnalyzeResponse response = client().admin().indices() |
| 60 | + .prepareAnalyze("hello world") |
| 61 | + .setIndex("test") |
| 62 | + .setAnalyzer("my_analyzer") |
| 63 | + .execute().get(); |
| 64 | + |
| 65 | + assertThat(response, notNullValue()); |
| 66 | + assertThat(response.getTokens().size(), is(4)); |
| 67 | + assertThat(response.getTokens().get(0).getTerm(), is("HL")); |
| 68 | + assertThat(response.getTokens().get(1).getTerm(), is("hello")); |
| 69 | + assertThat(response.getTokens().get(2).getTerm(), is("WRLT")); |
| 70 | + assertThat(response.getTokens().get(3).getTerm(), is("world")); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testPhoneticAnalyzerInMapping() throws ExecutionException, InterruptedException, IOException { |
| 75 | + createIndex("test"); |
| 76 | + ensureGreen("test"); |
| 77 | + final XContentBuilder mapping = jsonBuilder().startObject() |
| 78 | + .startObject("type") |
| 79 | + .startObject("properties") |
| 80 | + .startObject("foo") |
| 81 | + .field("type", "string") |
| 82 | + .field("analyzer", "my_analyzer") |
| 83 | + .endObject() |
| 84 | + .endObject() |
| 85 | + .endObject() |
| 86 | + .endObject(); |
| 87 | + |
| 88 | + client().admin().indices().preparePutMapping("test").setType("type").setSource(mapping).get(); |
| 89 | + |
| 90 | + index("test", "type", "1", "foo", "hello world"); |
| 91 | + refresh(); |
| 92 | + |
| 93 | + SearchResponse response = client().prepareSearch("test").setQuery( |
| 94 | + QueryBuilders.matchQuery("foo", "helllo") |
| 95 | + ).execute().actionGet(); |
| 96 | + |
| 97 | + assertThat(response.getHits().getTotalHits(), is(1L)); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments