Skip to content

Commit

Permalink
Analyze API to return 400 for wrong custom analyzer (elastic#121568) (e…
Browse files Browse the repository at this point in the history
…lastic#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 elastic#121443
  • Loading branch information
mayya-sharipova authored Feb 5, 2025
1 parent bca02ec commit f7bd10b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
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;
}

}

0 comments on commit f7bd10b

Please sign in to comment.