|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * 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.apache.lucene.analysis.en.EnglishAnalyzer; |
| 23 | +import org.apache.lucene.analysis.standard.StandardAnalyzer; |
| 24 | +import org.elasticsearch.Version; |
| 25 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 26 | +import org.elasticsearch.common.settings.Settings; |
| 27 | +import org.elasticsearch.index.Index; |
| 28 | +import org.elasticsearch.indices.analysis.IndicesAnalysisService; |
| 29 | +import org.elasticsearch.test.ESTestCase; |
| 30 | +import org.elasticsearch.test.VersionUtils; |
| 31 | + |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import static org.hamcrest.Matchers.instanceOf; |
| 37 | + |
| 38 | +public class AnalysisServiceTests extends ESTestCase { |
| 39 | + |
| 40 | + private static AnalyzerProviderFactory analyzerProvider(final String name) { |
| 41 | + return new AnalyzerProviderFactory() { |
| 42 | + @Override |
| 43 | + public AnalyzerProvider create(String name, Settings settings) { |
| 44 | + return new PreBuiltAnalyzerProvider(name, AnalyzerScope.INDEX, new EnglishAnalyzer()); |
| 45 | + } |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + public void testDefaultAnalyzers() { |
| 50 | + Version version = VersionUtils.randomVersion(getRandom()); |
| 51 | + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); |
| 52 | + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); |
| 53 | + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, |
| 54 | + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); |
| 55 | + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); |
| 56 | + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); |
| 57 | + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); |
| 58 | + } |
| 59 | + |
| 60 | + public void testOverrideDefaultAnalyzer() { |
| 61 | + Version version = VersionUtils.randomVersion(getRandom()); |
| 62 | + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); |
| 63 | + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); |
| 64 | + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, |
| 65 | + Collections.singletonMap("default", analyzerProvider("default")), |
| 66 | + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); |
| 67 | + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); |
| 68 | + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); |
| 69 | + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); |
| 70 | + } |
| 71 | + |
| 72 | + public void testOverrideDefaultIndexAnalyzer() { |
| 73 | + Version version = VersionUtils.randomVersionBetween(getRandom(), Version.V_3_0_0, Version.CURRENT); |
| 74 | + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); |
| 75 | + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); |
| 76 | + try { |
| 77 | + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, |
| 78 | + Collections.singletonMap("default_index", new PreBuiltAnalyzerProviderFactory("default_index", AnalyzerScope.INDEX, new EnglishAnalyzer())), |
| 79 | + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); |
| 80 | + fail("Expected ISE"); |
| 81 | + } catch (IllegalArgumentException e) { |
| 82 | + // expected |
| 83 | + assertTrue(e.getMessage().contains("[index.analysis.analyzer.default_index] is not supported")); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public void testBackCompatOverrideDefaultIndexAnalyzer() { |
| 88 | + Version version = VersionUtils.randomVersionBetween(getRandom(), VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(Version.V_3_0_0)); |
| 89 | + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); |
| 90 | + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); |
| 91 | + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, |
| 92 | + Collections.singletonMap("default_index", analyzerProvider("default_index")), |
| 93 | + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); |
| 94 | + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); |
| 95 | + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); |
| 96 | + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); |
| 97 | + } |
| 98 | + |
| 99 | + public void testOverrideDefaultSearchAnalyzer() { |
| 100 | + Version version = VersionUtils.randomVersion(getRandom()); |
| 101 | + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); |
| 102 | + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); |
| 103 | + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, |
| 104 | + Collections.singletonMap("default_search", analyzerProvider("default_search")), |
| 105 | + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); |
| 106 | + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class)); |
| 107 | + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); |
| 108 | + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); |
| 109 | + } |
| 110 | + |
| 111 | + public void testBackCompatOverrideDefaultIndexAndSearchAnalyzer() { |
| 112 | + Version version = VersionUtils.randomVersionBetween(getRandom(), VersionUtils.getFirstVersion(), VersionUtils.getPreviousVersion(Version.V_3_0_0)); |
| 113 | + Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build(); |
| 114 | + IndicesAnalysisService indicesAnalysisService = new IndicesAnalysisService(settings); |
| 115 | + Map<String, AnalyzerProviderFactory> analyzers = new HashMap<>(); |
| 116 | + analyzers.put("default_index", analyzerProvider("default_index")); |
| 117 | + analyzers.put("default_search", analyzerProvider("default_search")); |
| 118 | + AnalysisService analysisService = new AnalysisService(new Index("index"), settings, indicesAnalysisService, |
| 119 | + analyzers, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap()); |
| 120 | + assertThat(analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); |
| 121 | + assertThat(analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); |
| 122 | + assertThat(analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class)); |
| 123 | + } |
| 124 | +} |
0 commit comments