Skip to content

Commit

Permalink
Adjust exception thrown when unable to load hunspell dict (#123743) (#…
Browse files Browse the repository at this point in the history
…124143)

On index creation, its possible to configure an hunspell analyzer, but
reference a locale file that actually doesn't exist or isn't accessible.

This error, like our other user dictionary errors, should be an IAE not
an ISE.

closes: #123729
(cherry picked from commit a92b1d6)
  • Loading branch information
benwtrent authored Mar 6, 2025
1 parent 1475ace commit 961c0e3
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/123743.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 123743
summary: Adjust exception thrown when unable to load hunspell dict
area: Analysis
type: bug
issues: []
3 changes: 1 addition & 2 deletions modules/analysis-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import org.elasticsearch.gradle.Version

apply plugin: 'elasticsearch.internal-yaml-rest-test'
apply plugin: 'elasticsearch.yaml-rest-compat-test'
apply plugin: 'elasticsearch.internal-cluster-test'
Expand Down Expand Up @@ -36,6 +34,7 @@ artifacts {

tasks.named("yamlRestCompatTestTransform").configure { task ->
task.replaceValueInMatch("tokens.0.token", "absenț", "romanian")
task.skipTest("indices.analyze/15_analyze/Custom analyzer is not buildable", "error response changed with #123743")
}

tasks.named("yamlRestTest").configure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@
"Custom analyzer is not buildable":
- requires:
test_runner_features: [ capabilities ]
reason: This capability required to run test
capabilities:
- method: GET
path: /_analyze
capabilities: [ wrong_custom_analyzer_returns_400 ]
- method: PUT
path: /{index}
capabilities: [ hunspell_dict_400 ]
reason: "bugfix 'hunspell_dict_400' capability required"

- do:
catch: bad_request
Expand All @@ -80,7 +80,3 @@
filter:
type: hunspell
locale: en_US

- match: { status: 400 }
- match: { error.type: illegal_argument_exception }
- match: { error.reason: "Can not build a custom analyzer" }
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,30 @@
index.mode: lookup
index.number_of_shards: 2

---
"Create index with hunspell missing dict":
- requires:
test_runner_features: [ capabilities ]
capabilities:
- method: PUT
path: /{index}
capabilities: [ hunspell_dict_400 ]
reason: "bugfix 'hunspell_dict_400' capability required"

- do:
catch: bad_request
indices.create:
index: bad_hunspell_index
body:
settings:
analysis:
analyzer:
en:
tokenizer: standard
filter:
- my_en_US_dict_stemmer
filter:
my_en_US_dict_stemmer:
type: hunspell
locale: en_US
dedup: false
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public HunspellService(final Settings settings, final Environment env, final Map
try {
return loadDictionary(locale, settings, env);
} catch (Exception e) {
throw new IllegalStateException("failed to load hunspell dictionary for locale: " + locale, e);
throw new IllegalArgumentException("failed to load hunspell dictionary for locale: " + locale, e);
}
};
if (HUNSPELL_LAZY_LOAD.get(settings) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ public class CreateIndexCapabilities {

private static final String NESTED_DENSE_VECTOR_SYNTHETIC_TEST = "nested_dense_vector_synthetic_test";

private static final String HUNSPELL_DICT_400 = "hunspell_dict_400";

public static final Set<String> CAPABILITIES = Set.of(
LOGSDB_INDEX_MODE_CAPABILITY,
LOOKUP_INDEX_MODE_CAPABILITY,
NESTED_DENSE_VECTOR_SYNTHETIC_TEST
NESTED_DENSE_VECTOR_SYNTHETIC_TEST,
HUNSPELL_DICT_400
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testDicWithNoAff() throws Exception {
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
.build();

IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
final Environment environment = new Environment(settings, getDataPath("/indices/analyze/no_aff_conf_dir"));
new HunspellService(settings, environment, emptyMap()).getDictionary("en_US");
});
Expand All @@ -78,7 +78,7 @@ public void testDicWithTwoAffs() throws Exception {
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
.build();

IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
final Environment environment = new Environment(settings, getDataPath("/indices/analyze/two_aff_conf_dir"));
new HunspellService(settings, environment, emptyMap()).getDictionary("en_US");
});
Expand Down

0 comments on commit 961c0e3

Please sign in to comment.