Skip to content

Commit cc2107d

Browse files
Analyze API to return 400 for wrong custom analyzer
If a custom analyzer provided in _analyze API can not be built, return 400 instead of the current 500. This most probably means that the user's provided analyzer specificatons are wrong. Closes #121443
1 parent f7901f0 commit cc2107d

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

modules/analysis-common/src/yamlRestTest/resources/rest-api-spec/test/indices.analyze/15_analyze.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,24 @@
5959
- match: { detail.tokenizer.tokens.0.token: ABc }
6060
- match: { detail.tokenfilters.0.name: lowercase }
6161
- match: { detail.tokenfilters.0.tokens.0.token: abc }
62+
63+
---
64+
"Custom analyzer is not buildable":
65+
- requires:
66+
cluster_features: [ "wrong_custom_analyzer_return_400" ]
67+
reason: "Returning 400 for wrong custom analyzer was added in this version"
68+
69+
- do:
70+
catch: bad_request
71+
indices.analyze:
72+
body:
73+
text: the foxes jumping quickly
74+
tokenizer:
75+
standard
76+
filter:
77+
type: hunspell
78+
locale: en_US
79+
80+
- match: { status: 400 }
81+
- match: { error.type: illegal_argument_exception }
82+
- match: { error.reason: "Can not build a custom analyzer" }

server/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@
426426
provides org.elasticsearch.features.FeatureSpecification
427427
with
428428
org.elasticsearch.action.bulk.BulkFeatures,
429+
org.elasticsearch.action.admin.ActionFeatures,
429430
org.elasticsearch.features.FeatureInfrastructureFeatures,
430431
org.elasticsearch.rest.action.admin.cluster.ClusterRerouteFeatures,
431432
org.elasticsearch.index.mapper.MapperFeatures,
@@ -476,4 +477,5 @@
476477
exports org.elasticsearch.monitor.metrics;
477478
exports org.elasticsearch.plugins.internal.rewriter to org.elasticsearch.inference;
478479
exports org.elasticsearch.lucene.util.automaton;
480+
exports org.elasticsearch.action.admin;
479481
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.action.admin;
11+
12+
import org.elasticsearch.features.FeatureSpecification;
13+
import org.elasticsearch.features.NodeFeature;
14+
15+
import java.util.Set;
16+
17+
import static org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction.WRONG_CUSTOM_ANALYZER_RETURN_400;
18+
19+
public class ActionFeatures implements FeatureSpecification {
20+
21+
@Override
22+
public Set<NodeFeature> getFeatures() {
23+
return Set.of();
24+
}
25+
26+
27+
@Override
28+
public Set<NodeFeature> getTestFeatures() {
29+
return Set.of(WRONG_CUSTOM_ANALYZER_RETURN_400);
30+
}
31+
}

server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.common.io.stream.Writeable;
3030
import org.elasticsearch.common.settings.Settings;
3131
import org.elasticsearch.core.IOUtils;
32+
import org.elasticsearch.features.NodeFeature;
3233
import org.elasticsearch.index.IndexService;
3334
import org.elasticsearch.index.IndexService.IndexCreationContext;
3435
import org.elasticsearch.index.IndexSettings;
@@ -65,6 +66,8 @@
6566
*/
6667
public class TransportAnalyzeAction extends TransportSingleShardAction<AnalyzeAction.Request, AnalyzeAction.Response> {
6768

69+
public static final NodeFeature WRONG_CUSTOM_ANALYZER_RETURN_400 = new NodeFeature("wrong_custom_analyzer_return_400");
70+
6871
private final Settings settings;
6972
private final IndicesService indicesService;
7073

@@ -144,6 +147,8 @@ public static AnalyzeAction.Response analyze(
144147
if (analyzer != null) {
145148
return analyze(request, analyzer, maxTokenCount);
146149
}
150+
} catch (IllegalStateException e) {
151+
throw new IllegalArgumentException("Can not build a custom analyzer", e);
147152
}
148153

149154
// Otherwise we use a built-in analyzer, which should not be closed

server/src/main/resources/META-INF/services/org.elasticsearch.features.FeatureSpecification

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#
99

1010
org.elasticsearch.action.bulk.BulkFeatures
11+
org.elasticsearch.action.admin.ActionFeatures
1112
org.elasticsearch.features.FeatureInfrastructureFeatures
1213
org.elasticsearch.rest.action.admin.cluster.ClusterRerouteFeatures
1314
org.elasticsearch.index.IndexFeatures

0 commit comments

Comments
 (0)