浏览代码

analyzers: custom analyzers names and aliases must not start with _

closes #9596
Britta Weber 10 年之前
父节点
当前提交
37782c1745

+ 1 - 0
docs/reference/analysis/analyzers/custom-analyzer.asciidoc

@@ -5,6 +5,7 @@ An analyzer of type `custom` that allows to combine a `Tokenizer` with
 zero or more `Token Filters`, and zero or more `Char Filters`. The
 custom analyzer accepts a logical/registered name of the tokenizer to
 use, and a list of logical/registered names of token filters.
+The name of the custom analyzer must not start mit "_".
 
 The following are settings that can be set for a `custom` analyzer type:
 

+ 5 - 0
src/main/java/org/elasticsearch/index/analysis/AnalysisService.java

@@ -251,6 +251,11 @@ public class AnalysisService extends AbstractIndexComponent implements Closeable
         defaultSearchAnalyzer = analyzers.containsKey("default_search") ? analyzers.get("default_search") : analyzers.get("default");
         defaultSearchQuoteAnalyzer = analyzers.containsKey("default_search_quote") ? analyzers.get("default_search_quote") : defaultSearchAnalyzer;
 
+        for (Map.Entry<String, NamedAnalyzer> analyzer : analyzers.entrySet()) {
+            if (analyzer.getKey().startsWith("_")) {
+                throw new IllegalArgumentException("analyzer name must not start with _. got \"" + analyzer.getKey() + "\"");
+            }
+        }
         this.analyzers = ImmutableMap.copyOf(analyzers);
     }
 

+ 34 - 1
src/test/java/org/elasticsearch/index/analysis/AnalysisModuleTests.java

@@ -31,6 +31,7 @@ import org.elasticsearch.Version;
 import org.elasticsearch.cluster.metadata.IndexMetaData;
 import org.elasticsearch.common.inject.Injector;
 import org.elasticsearch.common.inject.ModulesBuilder;
+import org.elasticsearch.common.inject.ProvisionException;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.common.settings.SettingsModule;
 import org.elasticsearch.env.Environment;
@@ -94,7 +95,7 @@ public class AnalysisModuleTests extends ElasticsearchTestCase {
         Settings settings = loadFromClasspath("org/elasticsearch/index/analysis/test1.yml");
         testSimpleConfiguration(settings);
     }
-    
+
     @Test
     public void testDefaultFactoryTokenFilters() throws IOException {
         assertTokenFilter("keyword_repeat", KeywordRepeatFilter.class);
@@ -238,4 +239,36 @@ public class AnalysisModuleTests extends ElasticsearchTestCase {
         return wordListFile;
     }
 
+    @Test
+    public void testUnderscoreInAnalyzerName() {
+        Settings settings = Settings.builder()
+                .put("index.analysis.analyzer._invalid_name.tokenizer", "keyword")
+                .put("path.home", createTempDir().toString())
+                .put(IndexMetaData.SETTING_VERSION_CREATED, "1")
+                .build();
+        try {
+            getAnalysisService(settings);
+            fail("This should fail with IllegalArgumentException because the analyzers name starts with _");
+        } catch (ProvisionException e) {
+            assertTrue(e.getCause() instanceof IllegalArgumentException);
+            assertThat(e.getCause().getMessage(), equalTo("analyzer name must not start with _. got \"_invalid_name\""));
+        }
+    }
+
+    @Test
+    public void testUnderscoreInAnalyzerNameAlias() {
+        Settings settings = Settings.builder()
+                .put("index.analysis.analyzer.valid_name.tokenizer", "keyword")
+                .put("index.analysis.analyzer.valid_name.alias", "_invalid_name")
+                .put("path.home", createTempDir().toString())
+                .put(IndexMetaData.SETTING_VERSION_CREATED, "1")
+                .build();
+        try {
+            getAnalysisService(settings);
+            fail("This should fail with IllegalArgumentException because the analyzers alias starts with _");
+        } catch (ProvisionException e) {
+            assertTrue(e.getCause() instanceof IllegalArgumentException);
+            assertThat(e.getCause().getMessage(), equalTo("analyzer name must not start with _. got \"_invalid_name\""));
+        }
+    }
 }