Repository navigation
Fixes #27990: stop malformed search input failing as a server error (backport 2.0 #33655) - #35440
Conversation
❌ PR checklist incompleteThis PR cannot be merged until the following are addressed on its linked issue:
The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically. Maintainers can bypass this check by adding the |
| String queryDsl = QueryFilterShape.requireQueryDsl(queryFilter); | ||
| Query filterQuery = Query.of(q -> q.withJson(new StringReader(queryDsl))); | ||
| Query existingQuery = requestBuilder.query(); | ||
| requestBuilder.query( | ||
| existingQuery == null ? filterQuery : filteredBy(existingQuery, filterQuery)); |
There was a problem hiding this comment.
💡 Edge Case: Elasticsearch: DSL the client cannot parse now fails as a 500
On Elasticsearch, the filter is parsed on the client with Query.withJson(new StringReader(queryDsl)). QueryFilterShape only checks the bool clause shapes. So a filter such as {"query":{"not_a_query":{}}} gets past the check, and then withJson throws a JsonpMappingException (a RuntimeException) while the request is being built. The old catch (Exception) that swallowed this was removed and nothing replaced it, so the caller gets a 500 instead of the 400 this PR promises. OpenSearch sends the filter inside a wrapper query, so the engine rejects it and the PR's mapping turns that into a 400. Fix: catch the client parse failure in the ES applyQueryFilter and rethrow it as SearchException.withStatus(BAD_REQUEST, ...) with a message naming queryFilter. Add a test for an unknown query type.
Report a filter the client cannot parse as a 400:
String queryDsl = QueryFilterShape.requireQueryDsl(queryFilter);
Query filterQuery;
try {
filterQuery = Query.of(q -> q.withJson(new StringReader(queryDsl)));
} catch (JsonpMappingException | JsonParsingException e) {
throw SearchException.withStatus(
Response.Status.BAD_REQUEST, "queryFilter is not a valid query: " + e.getMessage());
}
Was this helpful? React with 👍 / 👎
Code Review 👍 Approved with suggestions 0 closed / 1 findings🟡 Medium risk · Routing malformed Lucene searches to literal matching and rejecting invalid queryFilter as 400 could change search results and client error handling. Stops malformed search input (for example, Lucene-unparseable text like a glossary term with
💡 Edge Case: Elasticsearch: DSL the client cannot parse now fails as a 500📄 openmetadata-service/src/main/java/org/openmetadata/service/search/elasticsearch/ElasticSearchSearchManager.java:391-395 📄 openmetadata-service/src/main/java/org/openmetadata/service/search/QueryFilterShape.java:42-49 On Elasticsearch, the filter is parsed on the client with Report a filter the client cannot parse as a 400🤖 Prompt for agentsVerificationRules No rules evaluated Functional validation Not enabledAuto-approval Not enabledFor more verification details, comment OptionsDisplay: compact → Showing compact verification results. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source |
|



Issue
Cherry-pick of PR #33655 (Fixes #27990)
On 2.0.x, a search whose text Lucene cannot parse, such as a glossary term name with a
/and a parenthesis, fails with a 500 and lands in error tracking, although nothing on the server went wrong. Main has handled this since PR #33655. This backport brings the same behaviour to 2.0, so it ships in 2.0.6. It is also what #31878 asks for.What this PR changes
The same three changes as on main (see PR #33655 for the full explanation):
query_string. It is searched as plain words, so the caller gets results instead of an error.queryFilterthat is not usable as query DSL is rejected with a 400 that namesqueryFilter, instead of failing inside the search engine.One adaptation was needed. On main, the fuzzy part of the query already falls back to
simple_query_stringfor free-text endpoints (PR #31142), and the fix sends unparseable text down that same path. 2.0 does not have that change, so this PR copies itssimpleQueryStringQueryhelper, unchanged, into the 2.0OpenSearchQueryBuilderandElasticQueryBuilder, and the fuzzy branch uses it whenever the text is not well-formed Lucene. Without it, that text would still reachquery_stringand fail, only as a 400 instead of returning results.The cherry-pick conflicted in imports of both search managers (both sides kept), in the two source builder factories (the adaptation above), and in
SearchResourceIT. There, 2.0 already has the stricter negative-offset test and thehttpGetSearchhelper from PR #35302, so both were kept and the PR's identical copy of the helper was dropped, along with two imports that only main's other tests use. Every other file applies main's change line for line.One behaviour change for API callers: a
queryFilterthat is not valid JSON used to be logged and silently ignored, so the search ran without it and returned more than asked. It is now a 400, as on main since 22 September.How it was tested
On this branch:
LuceneQuerySyntaxTest,QueryFilterShapeTest,SearchEngineErrorsTest,SearchSourceBuilderFactoryTest,FuzzySearchClauseTestand 12 other search classes): 297 tests, all passing.SearchResourceITtests against Postgres and OpenSearch: 12 tests, all passing. For that local run I also added the query from the production report,Uncategorized / Unknown (activity), as an extra case; it returns 200. That case is not part of this PR, to keep the tests identical to main.