-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport] Fix error handling while reading analyzer mapping rules
Add new parseWordList method that takes a parser as a parameter. It reads custom rules from settings or a file, parses and handles errors. Make error messages less verbose for rules files outside config directory. Signed-off-by: Rabi Panda <adnapibar@gmail.com>
- Loading branch information
Showing
26 changed files
with
394 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...is-common/src/test/java/org/opensearch/analysis/common/MappingCharFilterFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.analysis.common; | ||
|
||
import org.apache.lucene.analysis.CharFilter; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.env.Environment; | ||
import org.opensearch.index.analysis.AnalysisTestsHelper; | ||
import org.opensearch.index.analysis.CharFilterFactory; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.Arrays; | ||
|
||
public class MappingCharFilterFactoryTests extends OpenSearchTestCase { | ||
public static CharFilterFactory create(String... rules) throws IOException { | ||
OpenSearchTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings( | ||
Settings.builder() | ||
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) | ||
.put("index.analysis.analyzer.my_analyzer.tokenizer", "standard") | ||
.put("index.analysis.analyzer.my_analyzer.char_filter", "my_mappings_char_filter") | ||
.put("index.analysis.char_filter.my_mappings_char_filter.type", "mapping") | ||
.putList("index.analysis.char_filter.my_mappings_char_filter.mappings", rules) | ||
.build(), | ||
new CommonAnalysisPlugin() | ||
); | ||
|
||
return analysis.charFilter.get("my_mappings_char_filter"); | ||
} | ||
|
||
public void testRulesOk() throws IOException { | ||
MappingCharFilterFactory mappingCharFilterFactory = (MappingCharFilterFactory) create( | ||
"# This is a comment", | ||
":) => _happy_", | ||
":( => _sad_" | ||
); | ||
CharFilter inputReader = (CharFilter) mappingCharFilterFactory.create(new StringReader("I'm so :)")); | ||
char[] tempBuff = new char[14]; | ||
StringBuilder output = new StringBuilder(); | ||
while (true) { | ||
int length = inputReader.read(tempBuff); | ||
if (length == -1) break; | ||
output.append(tempBuff, 0, length); | ||
} | ||
assertEquals("I'm so _happy_", output.toString()); | ||
} | ||
|
||
public void testRuleError() { | ||
for (String rule : Arrays.asList( | ||
"", // empty | ||
"a", // no arrow | ||
"a:>b" // invalid delimiter | ||
)) { | ||
RuntimeException ex = expectThrows(RuntimeException.class, () -> create(rule)); | ||
assertEquals("Line [1]: Invalid mapping rule : [" + rule + "]", ex.getMessage()); | ||
} | ||
} | ||
|
||
public void testRulePartError() { | ||
RuntimeException ex = expectThrows(RuntimeException.class, () -> create("# This is a comment", ":) => _happy_", "a:b")); | ||
assertEquals("Line [3]: Invalid mapping rule : [a:b]", ex.getMessage()); | ||
} | ||
} |
Oops, something went wrong.