Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyze API to return 400 for wrong custom analyzer #121568

Merged
merged 7 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/121568.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 121568
summary: Analyze API to return 400 for wrong custom analyzer
area: Analysis
type: bug
issues:
- 121443
2 changes: 1 addition & 1 deletion modules/analysis-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ esplugin {

restResources {
restApi {
include '_common', 'indices', 'index', 'cluster', 'search', 'nodes', 'bulk', 'termvectors', 'explain', 'count'
include '_common', 'indices', 'index', 'cluster', 'search', 'nodes', 'bulk', 'termvectors', 'explain', 'count', 'capabilities'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,28 @@
- match: { detail.tokenizer.tokens.0.token: ABc }
- match: { detail.tokenfilters.0.name: lowercase }
- match: { detail.tokenfilters.0.tokens.0.token: abc }

---
"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 ]

- do:
catch: bad_request
indices.analyze:
body:
text: the foxes jumping quickly
tokenizer:
standard
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
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.action.admin.indices.analyze;

import java.util.Set;

public final class AnalyzeCapabilities {
private AnalyzeCapabilities() {}

private static final String WRONG_CUSTOM_ANALYZER_RETURNS_400_CAPABILITY = "wrong_custom_analyzer_returns_400";

public static final Set<String> CAPABILITIES = Set.of(WRONG_CUSTOM_ANALYZER_RETURNS_400_CAPABILITY);
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public static AnalyzeAction.Response analyze(
if (analyzer != null) {
return analyze(request, analyzer, maxTokenCount);
}
} catch (IllegalStateException e) {
throw new IllegalArgumentException("Can not build a custom analyzer", e);
}

// Otherwise we use a built-in analyzer, which should not be closed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.rest.action.admin.indices;

import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeCapabilities;
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestRequest;
Expand All @@ -19,6 +20,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Set;

import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST;
Expand Down Expand Up @@ -49,4 +51,9 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
}
}

@Override
public Set<String> supportedCapabilities() {
return AnalyzeCapabilities.CAPABILITIES;
}

}