Skip to content

Commit f7bd10b

Browse files
Analyze API to return 400 for wrong custom analyzer (#121568) (#121815)
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 specifications are wrong. Closes #121443
1 parent bca02ec commit f7bd10b

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

docs/changelog/121568.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 121568
2+
summary: Analyze API to return 400 for wrong custom analyzer
3+
area: Analysis
4+
type: bug
5+
issues:
6+
- 121443

modules/analysis-common/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ esplugin {
2020

2121
restResources {
2222
restApi {
23-
include '_common', 'indices', 'index', 'cluster', 'search', 'nodes', 'bulk', 'termvectors', 'explain', 'count'
23+
include '_common', 'indices', 'index', 'cluster', 'search', 'nodes', 'bulk', 'termvectors', 'explain', 'count', 'capabilities'
2424
}
2525
}
2626

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,28 @@
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+
test_runner_features: [ capabilities ]
67+
reason: This capability required to run test
68+
capabilities:
69+
- method: GET
70+
path: /_analyze
71+
capabilities: [ wrong_custom_analyzer_returns_400 ]
72+
73+
- do:
74+
catch: bad_request
75+
indices.analyze:
76+
body:
77+
text: the foxes jumping quickly
78+
tokenizer:
79+
standard
80+
filter:
81+
type: hunspell
82+
locale: en_US
83+
84+
- match: { status: 400 }
85+
- match: { error.type: illegal_argument_exception }
86+
- match: { error.reason: "Can not build a custom analyzer" }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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.indices.analyze;
11+
12+
import java.util.Set;
13+
14+
public final class AnalyzeCapabilities {
15+
private AnalyzeCapabilities() {}
16+
17+
private static final String WRONG_CUSTOM_ANALYZER_RETURNS_400_CAPABILITY = "wrong_custom_analyzer_returns_400";
18+
19+
public static final Set<String> CAPABILITIES = Set.of(WRONG_CUSTOM_ANALYZER_RETURNS_400_CAPABILITY);
20+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ public static AnalyzeAction.Response analyze(
144144
if (analyzer != null) {
145145
return analyze(request, analyzer, maxTokenCount);
146146
}
147+
} catch (IllegalStateException e) {
148+
throw new IllegalArgumentException("Can not build a custom analyzer", e);
147149
}
148150

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

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeAction.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.elasticsearch.rest.action.admin.indices;
1010

1111
import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction;
12+
import org.elasticsearch.action.admin.indices.analyze.AnalyzeCapabilities;
1213
import org.elasticsearch.client.internal.node.NodeClient;
1314
import org.elasticsearch.rest.BaseRestHandler;
1415
import org.elasticsearch.rest.RestRequest;
@@ -19,6 +20,7 @@
1920

2021
import java.io.IOException;
2122
import java.util.List;
23+
import java.util.Set;
2224

2325
import static org.elasticsearch.rest.RestRequest.Method.GET;
2426
import static org.elasticsearch.rest.RestRequest.Method.POST;
@@ -49,4 +51,9 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
4951
}
5052
}
5153

54+
@Override
55+
public Set<String> supportedCapabilities() {
56+
return AnalyzeCapabilities.CAPABILITIES;
57+
}
58+
5259
}

0 commit comments

Comments
 (0)