From 3d9f8ed764495ab85198f89baea9c06c72311275 Mon Sep 17 00:00:00 2001 From: Jun Ohtani Date: Fri, 2 Sep 2016 00:04:13 +0900 Subject: [PATCH 1/2] Remove `token_filter` in _analyze API Remove the param and change docs Closes #20283 --- .../action/admin/indices/RestAnalyzeAction.java | 6 +++--- .../admin/indices/RestAnalyzeActionTests.java | 15 +++++++++++++++ docs/reference/indices/analyze.asciidoc | 6 +++--- .../reference/migration/migrate_5_0/rest.asciidoc | 4 ++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeAction.java index 7801aad8e0b..04d0bf57612 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeAction.java @@ -50,7 +50,7 @@ public class RestAnalyzeAction extends BaseRestHandler { public static final ParseField TEXT = new ParseField("text"); public static final ParseField FIELD = new ParseField("field"); public static final ParseField TOKENIZER = new ParseField("tokenizer"); - public static final ParseField TOKEN_FILTERS = new ParseField("filter", "token_filter"); + public static final ParseField TOKEN_FILTERS = new ParseField("filter"); public static final ParseField CHAR_FILTERS = new ParseField("char_filter"); public static final ParseField EXPLAIN = new ParseField("explain"); public static final ParseField ATTRIBUTES = new ParseField("attributes"); @@ -77,7 +77,7 @@ public class RestAnalyzeAction extends BaseRestHandler { if (request.hasParam("tokenizer")) { analyzeRequest.tokenizer(request.param("tokenizer")); } - for (String filter : request.paramAsStringArray("filter", request.paramAsStringArray("token_filter", Strings.EMPTY_ARRAY))) { + for (String filter : request.paramAsStringArray("filter", Strings.EMPTY_ARRAY)) { analyzeRequest.addTokenFilter(filter); } for (String charFilter : request.paramAsStringArray("char_filter", Strings.EMPTY_ARRAY)) { @@ -144,7 +144,7 @@ public class RestAnalyzeAction extends BaseRestHandler { analyzeRequest.addTokenFilter(parser.map()); } else { throw new IllegalArgumentException(currentFieldName - + " array element should contain token_filter's name or setting"); + + " array element should contain filter's name or setting"); } } } else if (parseFieldMatcher.match(currentFieldName, Fields.CHAR_FILTERS) diff --git a/core/src/test/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeActionTests.java b/core/src/test/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeActionTests.java index de188ba9e91..5795002892a 100644 --- a/core/src/test/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeActionTests.java +++ b/core/src/test/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeActionTests.java @@ -181,6 +181,21 @@ public class RestAnalyzeActionTests extends ESTestCase { assertThat(e.getMessage(), startsWith("Unknown parameter [char_filters]")); } + content = XContentFactory.jsonBuilder() + .startObject() + .field("text", "THIS IS A TEST") + .field("tokenizer", "keyword") + .array("token_filter", "lowercase") + .endObject().bytes(); + + analyzeRequest = new AnalyzeRequest("for test"); + + try { + RestAnalyzeAction.buildFromContent(content, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY)); + } catch (Exception e) { + assertThat(e, instanceOf(IllegalArgumentException.class)); + assertThat(e.getMessage(), startsWith("Unknown parameter [token_filter]")); + } } } diff --git a/docs/reference/indices/analyze.asciidoc b/docs/reference/indices/analyze.asciidoc index ee8b856ef41..e2a5cf1661b 100644 --- a/docs/reference/indices/analyze.asciidoc +++ b/docs/reference/indices/analyze.asciidoc @@ -43,13 +43,13 @@ curl -XGET 'localhost:9200/_analyze' -d ' curl -XGET 'localhost:9200/_analyze' -d ' { "tokenizer" : "keyword", - "token_filter" : ["lowercase"], + "filter" : ["lowercase"], "char_filter" : ["html_strip"], "text" : "this is a test" }' -------------------------------------------------- -deprecated[5.0.0, Use `filter`/`token_filter`/`char_filter` instead of `filters`/`token_filters`/`char_filters`] +deprecated[5.0.0, Use `filter`/`char_filter` instead of `filters`/`char_filters` and `token_filters` has been removed] Custom tokenizers, token filters, and character filters can be specified in the request body as follows: @@ -112,7 +112,7 @@ provided it doesn't start with `{` : [source,js] -------------------------------------------------- -curl -XGET 'localhost:9200/_analyze?tokenizer=keyword&token_filter=lowercase&char_filter=html_strip' -d 'this is a test' +curl -XGET 'localhost:9200/_analyze?tokenizer=keyword&filter=lowercase&char_filter=html_strip' -d 'this is a test' -------------------------------------------------- === Explain Analyze diff --git a/docs/reference/migration/migrate_5_0/rest.asciidoc b/docs/reference/migration/migrate_5_0/rest.asciidoc index 278acd52c43..c17effeb04a 100644 --- a/docs/reference/migration/migrate_5_0/rest.asciidoc +++ b/docs/reference/migration/migrate_5_0/rest.asciidoc @@ -67,8 +67,8 @@ removed in Elasticsearch 6.0.0. ==== Analyze API changes -The deprecated `filters`/`token_filters`/`char_filters` parameter has been -renamed `filter`/`token_filter`/`char_filter`. +The deprecated `filters`/`char_filters` parameter has been +renamed `filter`/`char_filter` and `token_filters` parameter has been removed. ==== `DELETE /_query` endpoint removed From aef2e5d90ef99b4ede25804ef9b3b024146f5d88 Mon Sep 17 00:00:00 2001 From: Jun Ohtani Date: Fri, 2 Sep 2016 11:47:29 +0900 Subject: [PATCH 2/2] Remove `token_filter` in _analyze API Fix wording in docs Refactoring RestAnalyzeActionTests using expectThrows() Closes #20283 --- .../admin/indices/RestAnalyzeActionTests.java | 132 +++++++----------- .../migration/migrate_5_0/rest.asciidoc | 4 +- 2 files changed, 52 insertions(+), 84 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeActionTests.java b/core/src/test/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeActionTests.java index 5795002892a..9b7d4073d0d 100644 --- a/core/src/test/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeActionTests.java +++ b/core/src/test/java/org/elasticsearch/rest/action/admin/indices/RestAnalyzeActionTests.java @@ -27,7 +27,6 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.test.ESTestCase; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.startsWith; @@ -90,14 +89,10 @@ public class RestAnalyzeActionTests extends ESTestCase { public void testParseXContentForAnalyzeRequestWithInvalidJsonThrowsException() throws Exception { AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test"); - - try { - RestAnalyzeAction.buildFromContent(new BytesArray("{invalid_json}"), analyzeRequest, new ParseFieldMatcher(Settings.EMPTY)); - fail("shouldn't get here"); - } catch (Exception e) { - assertThat(e, instanceOf(IllegalArgumentException.class)); - assertThat(e.getMessage(), equalTo("Failed to parse request body")); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> RestAnalyzeAction.buildFromContent( + new BytesArray("{invalid_json}"), analyzeRequest, new ParseFieldMatcher(Settings.EMPTY))); + assertThat(e.getMessage(), equalTo("Failed to parse request body")); } public void testParseXContentForAnalyzeRequestWithUnknownParamThrowsException() throws Exception { @@ -107,14 +102,9 @@ public class RestAnalyzeActionTests extends ESTestCase { .field("text", "THIS IS A TEST") .field("unknown", "keyword") .endObject().bytes(); - - try { - RestAnalyzeAction.buildFromContent(invalidContent, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY)); - fail("shouldn't get here"); - } catch (Exception e) { - assertThat(e, instanceOf(IllegalArgumentException.class)); - assertThat(e.getMessage(), startsWith("Unknown parameter [unknown]")); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> RestAnalyzeAction.buildFromContent(invalidContent, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY))); + assertThat(e.getMessage(), startsWith("Unknown parameter [unknown]")); } public void testParseXContentForAnalyzeRequestWithInvalidStringExplainParamThrowsException() throws Exception { @@ -123,79 +113,57 @@ public class RestAnalyzeActionTests extends ESTestCase { .startObject() .field("explain", "fals") .endObject().bytes(); - try { - RestAnalyzeAction.buildFromContent(invalidExplain, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY)); - fail("shouldn't get here"); - } catch (Exception e) { - assertThat(e, instanceOf(IllegalArgumentException.class)); - assertThat(e.getMessage(), startsWith("explain must be either 'true' or 'false'")); - } + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> RestAnalyzeAction.buildFromContent(invalidExplain, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY))); + assertThat(e.getMessage(), startsWith("explain must be either 'true' or 'false'")); } public void testDeprecatedParamException() throws Exception { - BytesReference content = XContentFactory.jsonBuilder() - .startObject() - .field("text", "THIS IS A TEST") - .field("tokenizer", "keyword") - .array("filters", "lowercase") - .endObject().bytes(); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> RestAnalyzeAction.buildFromContent( + XContentFactory.jsonBuilder() + .startObject() + .field("text", "THIS IS A TEST") + .field("tokenizer", "keyword") + .array("filters", "lowercase") + .endObject().bytes(), + new AnalyzeRequest("for test"), new ParseFieldMatcher(Settings.EMPTY))); + assertThat(e.getMessage(), startsWith("Unknown parameter [filters]")); - AnalyzeRequest analyzeRequest = new AnalyzeRequest("for test"); - try { - RestAnalyzeAction.buildFromContent(content, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY)); - } catch (Exception e) { - assertThat(e, instanceOf(IllegalArgumentException.class)); - assertThat(e.getMessage(), startsWith("Unknown parameter [filters]")); - } + e = expectThrows(IllegalArgumentException.class, + () -> RestAnalyzeAction.buildFromContent( + XContentFactory.jsonBuilder() + .startObject() + .field("text", "THIS IS A TEST") + .field("tokenizer", "keyword") + .array("token_filters", "lowercase") + .endObject().bytes(), + new AnalyzeRequest("for test"), new ParseFieldMatcher(Settings.EMPTY))); + assertThat(e.getMessage(), startsWith("Unknown parameter [token_filters]")); - content = XContentFactory.jsonBuilder() - .startObject() - .field("text", "THIS IS A TEST") - .field("tokenizer", "keyword") - .array("token_filters", "lowercase") - .endObject().bytes(); - analyzeRequest = new AnalyzeRequest("for test"); + e = expectThrows(IllegalArgumentException.class, + () -> RestAnalyzeAction.buildFromContent( + XContentFactory.jsonBuilder() + .startObject() + .field("text", "THIS IS A TEST") + .field("tokenizer", "keyword") + .array("char_filters", "lowercase") + .endObject().bytes(), + new AnalyzeRequest("for test"), new ParseFieldMatcher(Settings.EMPTY))); + assertThat(e.getMessage(), startsWith("Unknown parameter [char_filters]")); - try { - RestAnalyzeAction.buildFromContent(content, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY)); - } catch (Exception e) { - assertThat(e, instanceOf(IllegalArgumentException.class)); - assertThat(e.getMessage(), startsWith("Unknown parameter [token_filters]")); - } - - content = XContentFactory.jsonBuilder() - .startObject() - .field("text", "THIS IS A TEST") - .field("tokenizer", "keyword") - .array("char_filters", "lowercase") - .endObject().bytes(); - - analyzeRequest = new AnalyzeRequest("for test"); - - try { - RestAnalyzeAction.buildFromContent(content, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY)); - } catch (Exception e) { - assertThat(e, instanceOf(IllegalArgumentException.class)); - assertThat(e.getMessage(), startsWith("Unknown parameter [char_filters]")); - } - - content = XContentFactory.jsonBuilder() - .startObject() - .field("text", "THIS IS A TEST") - .field("tokenizer", "keyword") - .array("token_filter", "lowercase") - .endObject().bytes(); - - analyzeRequest = new AnalyzeRequest("for test"); - - try { - RestAnalyzeAction.buildFromContent(content, analyzeRequest, new ParseFieldMatcher(Settings.EMPTY)); - } catch (Exception e) { - assertThat(e, instanceOf(IllegalArgumentException.class)); - assertThat(e.getMessage(), startsWith("Unknown parameter [token_filter]")); - } + e = expectThrows(IllegalArgumentException.class, + () -> RestAnalyzeAction.buildFromContent( + XContentFactory.jsonBuilder() + .startObject() + .field("text", "THIS IS A TEST") + .field("tokenizer", "keyword") + .array("token_filter", "lowercase") + .endObject().bytes() + , new AnalyzeRequest("for test"), new ParseFieldMatcher(Settings.EMPTY))); + assertThat(e.getMessage(), startsWith("Unknown parameter [token_filter]")); } } diff --git a/docs/reference/migration/migrate_5_0/rest.asciidoc b/docs/reference/migration/migrate_5_0/rest.asciidoc index c17effeb04a..6b4b4bb66c5 100644 --- a/docs/reference/migration/migrate_5_0/rest.asciidoc +++ b/docs/reference/migration/migrate_5_0/rest.asciidoc @@ -67,8 +67,8 @@ removed in Elasticsearch 6.0.0. ==== Analyze API changes -The deprecated `filters`/`char_filters` parameter has been -renamed `filter`/`char_filter` and `token_filters` parameter has been removed. +The `filters` and `char_filters` parameters have been renamed `filter` and `char_filter`. +The `token_filters` parameter has been removed. Use `filter` instead. ==== `DELETE /_query` endpoint removed