Browse Source

Add better error message when analyzer created without tokenizer or analyzer type (#18455)

Closes #15492
Jeff Evans 9 years ago
parent
commit
3cf4214255

+ 2 - 2
core/src/main/java/org/elasticsearch/index/analysis/AnalysisRegistry.java

@@ -321,7 +321,7 @@ public final class AnalysisRegistry implements Closeable {
                     if (currentSettings.get("tokenizer") != null) {
                         factory = (T) new CustomAnalyzerProvider(settings, name, currentSettings);
                     } else {
-                        throw new IllegalArgumentException(toBuild + " [" + name + "] must have a type associated with it");
+                        throw new IllegalArgumentException(toBuild + " [" + name + "] must specify either an analyzer type, or a tokenizer");
                     }
                 } else if (typeName.equals("custom")) {
                     factory = (T) new CustomAnalyzerProvider(settings, name, currentSettings);
@@ -335,7 +335,7 @@ public final class AnalysisRegistry implements Closeable {
                 factories.put(name, factory);
             }  else {
                 if (typeName == null) {
-                    throw new IllegalArgumentException(toBuild + " [" + name + "] must have a type associated with it");
+                    throw new IllegalArgumentException(toBuild + " [" + name + "] must specify either an analyzer type, or a tokenizer");
                 }
                 AnalysisModule.AnalysisProvider<T> type = providerMap.get(typeName);
                 if (type == null) {

+ 16 - 0
core/src/test/java/org/elasticsearch/index/analysis/AnalysisServiceTests.java

@@ -41,6 +41,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.instanceOf;
 
 public class AnalysisServiceTests extends ESTestCase {
@@ -183,4 +184,19 @@ public class AnalysisServiceTests extends ESTestCase {
             assertSame(analysisService.analyzer(preBuiltAnalyzers.name()), otherAnalysisSergice.analyzer(preBuiltAnalyzers.name()));
         }
     }
+
+    public void testNoTypeOrTokenizerErrorMessage() throws IOException {
+        Version version = VersionUtils.randomVersion(random());
+        Settings settings = Settings
+            .builder()
+            .put(IndexMetaData.SETTING_VERSION_CREATED, version)
+            .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
+            .putArray("index.analysis.analyzer.test_analyzer.filter", new String[] {"lowercase", "stop", "shingle"})
+            .putArray("index.analysis.analyzer.test_analyzer.char_filter", new String[] {"html_strip"})
+            .build();
+        IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings);
+
+        IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new AnalysisRegistry(null, new Environment(settings)).build(idxSettings));
+        assertThat(e.getMessage(), equalTo("analyzer [test_analyzer] must specify either an analyzer type, or a tokenizer"));
+    }
 }