Repository navigation
Fixes #27990: stop malformed search input failing as a server error - #33655
Conversation
/v1/search/query reported malformed caller input as a 500. Two distinct failures are recorded on that Sentry group; this fixes both, plus the shared mapping that turned them into server errors. A malformed Lucene expression in `q` failed the whole search. containsQuerySyntax routes anything carrying Lucene syntax into query_string with the raw text, so an unparseable expression came back as `all shards failed` and the caller got nothing. Routing now also requires the expression to be parseable. Well-formed Lucene keeps its meaning exactly as before; text Lucene cannot parse is searched as literal words, which is what a partially typed query means anyway. A queryFilter that is valid JSON but not query DSL failed the search with `[bool] failed to parse field [must]`. applyQueryFilter only guarded client-side JSON parsing, so such a filter reached the engine inside a wrapper query. It is now rejected as a 400 naming the parameter rather than dropped, since a missing filter widens the result set past what the caller asked for. buildSearchException special-cased only 404, so every other engine status became SearchException(String), which hardcodes INTERNAL_SERVER_ERROR. Engine 400s now map to 400; 429 and 5xx stay 500s. This also covers any malformed form the syntax check does not model. The syntax check is structural, not a Lucene parser, and deliberately conservative: every rule was derived from a live engine, since `foo~` parses while `foo^` does not and `a + b` parses while a lone `+` does not. Cross-checked over a 1,001-query corpus (913 reaching the Lucene path) against whether the engine really parses the text: zero false positives. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
✅ PR checks passedThe linked issue has a description and all required Shipping project fields set. Thanks! |
Both encode behaviour this PR deliberately changes, and both failed on every integration lane. testSearchWithMalformedQueryFilter asserted that a malformed queryFilter is accepted: it was logged, dropped, and the search returned 200 carrying every row the filter was meant to exclude. Silently widening a result set is worse than failing, so it is now a 400 and the test says so. testSearchWithNegativeOffset asserted ApiException. A negative offset is the caller's error and the engine answers 400, which the SDK maps to InvalidRequestException -- a sibling of ApiException, not a subclass, so the old assertion no longer matches. The test now pins the 400. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Integration lane failures — fixed in d352ea9All three lanes (mysql-elasticsearch, postgres-opensearch, postgres-elasticsearch-redis) failed on the same two tests, and both are pre-existing tests encoding behaviour this PR deliberately changes — not the new ones. My three added tests passed on every lane.
Neither change weakens a test — both now assert a stricter, more specific contract than before. Checks I ran before pushing, to avoid another round trip:
|
Code Review ✅ Approved 1 closed / 1 findings🟡 Medium risk Prevents malformed search input from surfacing as server errors by routing unparseable Lucene expressions through free-text search, validating query-filter shapes before forwarding to the engine, and mapping engine 400s to caller-facing bad requests. Comprehensive unit and integration test coverage added. No issues found. ✅ 1 closed✅ Edge Case: QueryFilterShape validates bool keys anywhere, not only in bool
OptionsDisplay: compact → Counting what did not apply, without listing it. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source |
|



Describe your changes:
Fixes #27990
/v1/search/queryreported malformed caller input as a server fault. Two distinct failures are recorded on that Sentry group, and this PR fixes both plus the shared amplifier that turned them into 500s.1. A malformed Lucene expression in
qfailed the whole search.containsQuerySyntax()routes anything containing* ? ( ) ~ ^ " + - :intoquery_stringwith the raw text. Lucene then rejects an unparseable expression, which comes back asall shards failedand the caller gets nothing — this is theparse_exception ... Encountered " ":" ": "" at line 1, column 0in the issue body.Routing now also requires the expression to be parseable (
LuceneQuerySyntax). Well-formed Lucene keeps its Lucene meaning exactly as before; text that Lucene cannot parse is searched as literal words, which is what a partially typed query means anyway.2. A
queryFilterthat is valid JSON but not query DSL failed the search.This is the newer failure on the same group:
x_content_parse_exception: [1:220] [bool] failed to parse field [must].applyQueryFilter'stry/catchonly covered client-side JSON parsing, so a filter that parsed as JSON but was not a query sailed through into awrapperquery and was rejected by the engine.QueryFilterShapenow checks the one invariant the DSL actually guarantees —must/should/filter/must_nothold a query object or an array of query objects — and rejects anything else as a 400 namingqueryFilter.3. Engine 400s are no longer reported as 500s.
buildSearchExceptionspecial-cased only404, so every other engine status becameSearchException(String), which hardcodesINTERNAL_SERVER_ERROR.SearchEngineErrorsnow maps an engine400to a 400. Everything else, including the429a tripped circuit breaker returns, stays a 500 — those are not the caller's doing. This also covers any malformed form the syntax check above does not model: it is an honest 400 instead of a 500.Type of change:
High-level design:
The routing guard is a structural check, not a Lucene parser — Lucene's
queryparseris not on the service classpath. It is deliberately conservative: it only reports text that Lucene definitely cannot parse, so it can never downgrade a valid expression. Every rule was derived from a live engine rather than from the grammar, which mattered —foo~parses (default edit distance) whilefoo^does not,a + bparses while a lone+does not, and colons inside a quoted phrase are literal.Alternatives considered:
q— silently changes the meaning of valid Lucene expressions. Rejected.executeRankedSearch. The pre-check gets the same user-visible result with no extra round trip, and change 3 catches whatever it misses.queryFilter(the previous behaviour for malformed JSON) — a dropped filter widens the result set past what the caller asked for, which is worse than an error. Rejected.Behaviour change worth calling out: a malformed
queryFilterwas previously logged and ignored, returning 200 with unfiltered results. It is now a 400. This is intentional for the reason above, but it is a visible change for any caller that has been sending a filter that never applied.Tests:
Use cases covered
revenue (draft)in Explore gets results at every intermediate keystroke instead of a failed search:foo*,"unclosed,foo AND,name:(fooreturns results instead ofall shards failedname:orders AND service:snowflake,foo~^2,"a:b:c") keeps its Lucene meaningqueryFiltergets a 400 naming the parameter, not a 500429) is still reported as a server errorUnit tests
LuceneQuerySyntaxTest,QueryFilterShapeTest,SearchEngineErrorsTestSearchSourceBuilderFactoryTest(routing: unparseable syntax must not reachquery_string; well-formed syntax still must)Backend integration tests
openmetadata-integration-tests/.SearchResourceIT— unparseableqreturns 200; malformedqueryFilterreturns 400; validqueryFilterstill appliesIngestion integration tests
Playwright (UI) tests
Manual testing performed
Verified against a real OpenSearch 3.4.0 node with OpenMetadata's own
table_index_mapping.jsonand 8,000 indexed tables, driving the realOpenSearchSourceBuilderFactorywith the shippedsearchSettings.jsonso the query bodies under test are the ones the service actually sends.query_shard_exception/all shards failed.q=":foo*"reproduced the issue'sparse_exceptionbyte-for-byte, including theWas expecting one of:token list.revenue (draft),"quoted phrase"andsales (2024), 25 failed — every state from the opening(or"until it was closed.queryFilterwith a nested array insidebool.mustproducedx_content_parse_exception: [1:200] [bool] failed to parse field [must]before, and is a 400 namingqueryFilterafter.Cross-check of the syntax guard against live Lucene — a 1,001-query corpus, restricted to the 913 that actually reach the Lucene path (
containsQuerySyntax == true), comparing the guard's verdict against whether the engine really parses the text:Zero false positives is the property that matters: no valid Lucene expression loses its meaning. The 135 residuals are exotic forms the guard does not model (
orders[~1,orders!^2); they are genuinely malformed, and change 3 makes them an honest 400.Across the three input corpora (38 malformed, 42 keystroke prefixes, 20 realistic), server-side failures go from 48 to 0.
UI screen recording / screenshots:
Not applicable.
Checklist:
I have read the CONTRIBUTING document.
My PR title is
Fixes <issue-number>: <short explanation>My PR is linked to a GitHub issue via
Fixes #<issue-number>above.I have commented on my code, particularly in hard-to-understand areas.
For JSON Schema changes: not applicable.
For UI changes: not applicable.
I have added tests (unit / integration) and listed them above.
I have added a test that covers the exact scenario we are fixing. Issue SearchException: Search failed due to Request failed: [search_phase_execution_exception] all shards failed | Root cause: [parse_exception: parse_exception: Encountered " ":" ": "" at line 1, column 0. #27990 is referenced in
LuceneQuerySyntaxTest,QueryFilterShapeTestandSearchResourceIT.🤖 Generated with Claude Code
Open workspace in Conductor
The PR appears safe to merge; the previous findings are resolved and no new actionable defects were identified.
Summary
This PR prevents malformed search input from being reported as a server failure.
Diagram
%%{init: {'theme': 'neutral'}}%% flowchart TD Request["Search request"] --> Q{"Malformed q?"} Q -->|Yes| Literal["Literal/simple query path"] Q -->|No| Lucene["Lucene query path"] Request --> Filter{"queryFilter valid?"} Filter -->|No| BadRequest["HTTP 400"] Filter -->|Yes| Engine["Search engine"] Literal --> Engine Lucene --> Engine Engine --> Status{"Engine status"} Status -->|400| BadRequest Status -->|Other failure| ServerError["HTTP 500"] Status -->|Success| Results["Search results"]Reviews (4) · Last reviewed commit: "Update two SearchResourceIT assertions t..."