Skip to content

fix: make MultiMatchQuery serde lossless - #9753

Merged
jackye1995 merged 1 commit into
lance-format:mainfrom
hamersaw:fix/multi-match-serde
Oct 8, 2026
Merged

jackye1995 merged 1 commit into
lance-format:mainfrom
hamersaw:fix/multi-match-serde

Conversation

@hamersaw

@hamersaw hamersaw commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Problem

MultiMatchQuery's custom serde wrote only {query, columns, boost}, taken from the first leaf, and decoded every leaf as a default MatchQuery. Each leaf's terms, operator, fuzziness, max_expansions, prefix_length and document granularity were lost over JSON, so an AND multi-match came back as OR.

Anything that ships an FtsQuery as JSON hits this. In LanceDB that includes the remote client's structured FTS body (rust/lancedb/src/remote/table.rs), so MultiMatchQuery(operator=AND) from Python is evaluated as OR server-side.

Fix

  • Serialize: every leaf goes out as a full MatchQuery under match_queries, the shape the JSON parser (MultiMatchQuery::from_json) and the namespace spec already use.
  • Deserialize: match_queries is read first.
  • Compatibility:
    • When every leaf names a column, the legacy query/columns/boost keys are still written. A reader that predates match_queries ignores the new key and degrades exactly as it does today, so a rolling upgrade doesn't break plain multi-matches.
    • When a leaf has no column, only match_queries is written. An old reader then fails with missing field "query"; the old writer panicked on the column unwrap().
    • The legacy shape still decodes unchanged.

No other FtsQuery variant's serde loses fields. Boost and Boolean only lost data when nesting a multi-match, which the nested round-trip test covers.

Tests

  • test_multi_match_query_serde_round_trip: per-leaf AND, fuzziness, max_expansions, prefix_length, boost, granularity, and different terms per leaf.
  • test_multi_match_query_serde_nested_round_trip: a Boost wrapping a Boolean that contains a MultiMatch.
  • test_multi_match_query_legacy_serde: the legacy shape decodes as before; new output still carries the legacy keys.

All three fail without the fix. cargo test -p lance-index --lib scalar::inverted:: passes (912 tests), and clippy for lance-index is clean.

Downstream

Two LanceDB tests encode the old behaviour and will need updating on the bump:

  • python/python/tests/test_fts.py:1267 asserts the exact multi-match JSON string.
  • rust/lancedb/src/table/datafusion/udtf/fts.rs:1488 expects one row from leaves with different terms; it only passed because the second leaf's terms were dropped.

🤖 Generated with Claude Code

MultiMatchQuery serialized only `{query, columns, boost}` from its first
leaf and decoded every leaf as a default MatchQuery, so each leaf's terms,
operator, fuzziness, max_expansions, prefix_length and document
granularity were lost over JSON. An AND multi-match came back as OR.

Serialize every leaf under `match_queries` (the shape the JSON parser and
the namespace spec already use) and read it first on decode. The legacy
keys are still written when every leaf names a column, so a reader that
predates `match_queries` degrades as before, and the legacy shape still
decodes unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added A-index Vector index, linalg, tokenizer bug Something isn't working labels Oct 7, 2026
@hamersaw
hamersaw marked this pull request as ready for review October 7, 2026 16:43

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Gate recommendation: approve.

The full-leaf wire shape addresses the loss of per-field terms and options. Retaining the legacy keys supports rolling upgrades; older readers keep their existing degraded semantics.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 7, 2026

@zhangyue19921010 zhangyue19921010 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(non-blocking) We may want to pay attention to the upgrade order: upgrade servers first, then clients.

  • New server + old client: safe. Old clients only write query/columns/boost, which the new server decodes exactly as before, so behavior is unchanged.
  • Old server + new client: silently wrong. The old server doesn't recognize match_queries and rebuilds every leaf from the legacy keys (query/columns/boost). A query that relies on per-leaf settings therefore runs as a different query, and no error is raised: every leaf searches the first leaf's terms, operator changes from And to Or, fuzziness changes from auto (null) to exact (0), and document_granularity is dropped. (If any leaf has no column, the old server fails with missing field "query" instead.)

@jackye1995
jackye1995 merged commit 72e175e into lance-format:main Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-index Vector index, linalg, tokenizer bug Something isn't working K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants