Skip to content

Commit a632db2

Browse files
committed
Analysis : Allow string explain param in JSON
Move some test methods from AnalylzeActionIT to RestAnalyzeActionTest Allow string explain param if it can parse Fix wrong param name in rest-api-spec Fix typo Remove unused import Backport of #16977 Related to #16925
1 parent 34f0559 commit a632db2

File tree

5 files changed

+113
-65
lines changed

5 files changed

+113
-65
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ public static void buildFromContent(BytesReference content, AnalyzeRequest analy
144144
charFilters.add(parser.text());
145145
}
146146
analyzeRequest.charFilters(charFilters.toArray(new String[charFilters.size()]));
147-
} else if (parseFieldMatcher.match(currentFieldName, Fields.EXPLAIN) && token == XContentParser.Token.VALUE_BOOLEAN) {
148-
analyzeRequest.explain(parser.booleanValue());
147+
} else if (parseFieldMatcher.match(currentFieldName, Fields.EXPLAIN)) {
148+
if (parser.isBooleanValue()) {
149+
analyzeRequest.explain(parser.booleanValue());
150+
} else {
151+
throw new IllegalArgumentException(currentFieldName + " must be either 'true' or 'false'");
152+
}
149153
} else if (parseFieldMatcher.match(currentFieldName, Fields.ATTRIBUTES) && token == XContentParser.Token.START_ARRAY){
150154
List<String> attributes = new ArrayList<>();
151155
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {

core/src/test/java/org/elasticsearch/indices/analyze/AnalyzeActionIT.java

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@
1919
package org.elasticsearch.indices.analyze;
2020

2121
import org.elasticsearch.action.admin.indices.alias.Alias;
22-
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest;
2322
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder;
2423
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
25-
import org.elasticsearch.common.ParseFieldMatcher;
26-
import org.elasticsearch.common.bytes.BytesArray;
27-
import org.elasticsearch.common.bytes.BytesReference;
28-
import org.elasticsearch.common.settings.Settings;
29-
import org.elasticsearch.common.xcontent.XContentFactory;
30-
import org.elasticsearch.rest.action.admin.indices.analyze.RestAnalyzeAction;
3124
import org.elasticsearch.test.ESIntegTestCase;
3225
import org.junit.Test;
3326

@@ -198,59 +191,7 @@ private static String indexOrAlias() {
198191
return randomBoolean() ? "test" : "alias";
199192
}
200193

201-
@Test
202-
public void testParseXContentForAnalyzeReuqest() throws Exception {
203-
BytesReference content = XContentFactory.jsonBuilder()
204-
.startObject()
205-
.field("text", "THIS IS A TEST")
206-
.field("tokenizer", "keyword")
207-
.array("filters", "lowercase")
208-
.endObject().bytes();
209-
210-
AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test");
211-
212-
RestAnalyzeAction.buildFromContent(content, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY));
213-
214-
assertThat(analyzeRequest.text().length, equalTo(1));
215-
assertThat(analyzeRequest.text(), equalTo(new String[]{"THIS IS A TEST"}));
216-
assertThat(analyzeRequest.tokenizer(), equalTo("keyword"));
217-
assertThat(analyzeRequest.tokenFilters(), equalTo(new String[]{"lowercase"}));
218-
}
219-
220-
@Test
221-
public void testParseXContentForAnalyzeRequestWithInvalidJsonThrowsException() throws Exception {
222-
AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test");
223-
224-
try {
225-
RestAnalyzeAction.buildFromContent(new BytesArray("{invalid_json}"), analyzeRequest, new ParseFieldMatcher(Settings.EMPTY));
226-
fail("shouldn't get here");
227-
} catch (Exception e) {
228-
assertThat(e, instanceOf(IllegalArgumentException.class));
229-
assertThat(e.getMessage(), equalTo("Failed to parse request body"));
230-
}
231-
}
232-
233-
@Test
234-
public void testParseXContentForAnalyzeRequestWithUnknownParamThrowsException() throws Exception {
235-
AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test");
236-
BytesReference invalidContent =XContentFactory.jsonBuilder()
237-
.startObject()
238-
.field("text", "THIS IS A TEST")
239-
.field("unknown", "keyword")
240-
.endObject().bytes();
241-
242-
try {
243-
RestAnalyzeAction.buildFromContent(invalidContent, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY));
244-
fail("shouldn't get here");
245-
} catch (Exception e) {
246-
assertThat(e, instanceOf(IllegalArgumentException.class));
247-
assertThat(e.getMessage(), startsWith("Unknown parameter [unknown]"));
248-
}
249-
}
250-
251-
@Test
252-
public void analyzerWithMultiValues() throws Exception {
253-
194+
public void testAnalyzerWithMultiValues() throws Exception {
254195
assertAcked(prepareCreate("test").addAlias(new Alias("alias")));
255196
ensureGreen();
256197

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.rest.action.admin.indices.analyze;
20+
21+
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest;
22+
import org.elasticsearch.common.ParseFieldMatcher;
23+
import org.elasticsearch.common.bytes.BytesArray;
24+
import org.elasticsearch.common.bytes.BytesReference;
25+
import org.elasticsearch.common.settings.Settings;
26+
import org.elasticsearch.common.xcontent.XContentFactory;
27+
import org.elasticsearch.test.ESTestCase;
28+
import org.junit.Test;
29+
30+
import static org.hamcrest.Matchers.equalTo;
31+
import static org.hamcrest.Matchers.instanceOf;
32+
import static org.hamcrest.Matchers.startsWith;
33+
34+
public class RestAnalyzeActionTests extends ESTestCase {
35+
36+
@Test
37+
public void testParseXContentForAnalyzeRequest() throws Exception {
38+
BytesReference content = XContentFactory.jsonBuilder()
39+
.startObject()
40+
.field("text", "THIS IS A TEST")
41+
.field("tokenizer", "keyword")
42+
.array("filters", "lowercase")
43+
.endObject().bytes();
44+
45+
AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test");
46+
47+
RestAnalyzeAction.buildFromContent(content, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY));
48+
49+
assertThat(analyzeRequest.text().length, equalTo(1));
50+
assertThat(analyzeRequest.text(), equalTo(new String[]{"THIS IS A TEST"}));
51+
assertThat(analyzeRequest.tokenizer(), equalTo("keyword"));
52+
assertThat(analyzeRequest.tokenFilters(), equalTo(new String[]{"lowercase"}));
53+
}
54+
55+
@Test
56+
public void testParseXContentForAnalyzeRequestWithInvalidJsonThrowsException() throws Exception {
57+
AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test");
58+
59+
try {
60+
RestAnalyzeAction.buildFromContent(new BytesArray("{invalid_json}"), analyzeRequest, new ParseFieldMatcher(Settings.EMPTY));
61+
fail("shouldn't get here");
62+
} catch (Exception e) {
63+
assertThat(e, instanceOf(IllegalArgumentException.class));
64+
assertThat(e.getMessage(), equalTo("Failed to parse request body"));
65+
}
66+
}
67+
68+
@Test
69+
public void testParseXContentForAnalyzeRequestWithUnknownParamThrowsException() throws Exception {
70+
AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test");
71+
BytesReference invalidContent = XContentFactory.jsonBuilder()
72+
.startObject()
73+
.field("text", "THIS IS A TEST")
74+
.field("unknown", "keyword")
75+
.endObject().bytes();
76+
77+
try {
78+
RestAnalyzeAction.buildFromContent(invalidContent, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY));
79+
fail("shouldn't get here");
80+
} catch (Exception e) {
81+
assertThat(e, instanceOf(IllegalArgumentException.class));
82+
assertThat(e.getMessage(), startsWith("Unknown parameter [unknown]"));
83+
}
84+
}
85+
86+
@Test
87+
public void testParseXContentForAnalyzeRequestWithInvalidStringExplainParamThrowsException() throws Exception {
88+
AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test");
89+
BytesReference invalidExplain = XContentFactory.jsonBuilder()
90+
.startObject()
91+
.field("explain", "fals")
92+
.endObject().bytes();
93+
try {
94+
RestAnalyzeAction.buildFromContent(invalidExplain, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY));
95+
fail("shouldn't get here");
96+
} catch (Exception e) {
97+
assertThat(e, instanceOf(IllegalArgumentException.class));
98+
assertThat(e.getMessage(), startsWith("explain must be either 'true' or 'false'"));
99+
}
100+
}
101+
102+
103+
}

rest-api-spec/src/main/resources/rest-api-spec/api/indices.analyze.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
"type" : "string",
4545
"description" : "The name of the tokenizer to use for the analysis"
4646
},
47-
"detail": {
47+
"explain": {
4848
"type" : "boolean",
4949
"description" : "With `true`, outputs more advanced details. (default: false)"
5050
},
5151
"attributes": {
5252
"type" : "list",
53-
"description" : "A comma-separated list of token attributes to output, this parameter works only with `detail=true`"
53+
"description" : "A comma-separated list of token attributes to output, this parameter works only with `explain=true`"
5454
},
5555
"format": {
5656
"type": "enum",

rest-api-spec/src/main/resources/rest-api-spec/test/indices.analyze/10_analyze.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ setup:
7575
"Detail response with Analyzer":
7676
- do:
7777
indices.analyze:
78-
body: {"text": "This is troubled", "analyzer": standard, "explain": true}
78+
body: {"text": "This is troubled", "analyzer": standard, "explain": "true"}
7979
- length: { detail.analyzer.tokens: 3 }
8080
- match: { detail.analyzer.name: standard }
8181
- match: { detail.analyzer.tokens.0.token: this }

0 commit comments

Comments
 (0)