AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
fix(search): pin the in-order slop divergence; correct two ES citations
xerj-orgba3276d090a2
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rsindex e1c704d93..228a7702e 100644--- a/engine/crates/xerj-engine/src/index.rs+++ b/engine/crates/xerj-engine/src/index.rs@@ -28193,6 +28193,20 @@ fn phrase_positions_in_tokens(field_tokens: &[String], query_tokens: &[String],/// Keeping one walk (rather than two look-alike ones) is deliberate: the/// two evaluators must not be able to drift apart the way the slop-0-only/// prefix walk had already drifted from the sloppy phrase walk.+///+/// KNOWN DIVERGENCE FROM ES, not closed by #230: this walk is **in-order+/// only**. Lucene's sloppy phrase admits a transposition at a distance+/// cost — its own javadoc: «for query "a b"~2, a document "x a b a y" can+/// be matched twice: once for "a b" (distance=0), and once for "b a"+/// (distance=2)» (`lucene/core/.../search/SloppyPhraseMatcher.java`, class+/// javadoc; Apache-2.0) — so ES answers `match_phrase {"query": "policy+/// merge", "slop": 2}` on a document reading `merge policy`, and XERJ+/// answers it with zero hits. That is the pre-existing semantics of+/// `xerj_fts::search::phrase_positions_match`, which the segment side has+/// always used and which this walk deliberately mirrors: matching ES here+/// means changing BOTH evaluators together, and mirroring the existing one+/// is what keeps the hit set flush-invariant today. Measured, not assumed+/// (5-doc index, `"policy merge"` slop 2 and slop 3 → `[]` in both states).fn phrase_walk(field_tokens: &[String],query_tokens: &[String],diff --git a/engine/crates/xerj-engine/tests/multi_match_phrase_positions.rs b/engine/crates/xerj-engine/tests/multi_match_phrase_positions.rsindex 007861e1e..ba030e741 100644--- a/engine/crates/xerj-engine/tests/multi_match_phrase_positions.rs+++ b/engine/crates/xerj-engine/tests/multi_match_phrase_positions.rs@@ -285,6 +285,57 @@ async fn phrase_slop_is_honoured() {.await;}+/// PINS A KNOWN DIVERGENCE FROM ES, so nothing in this PR can be read as+/// claiming it away: XERJ's sloppy phrase is **in-order only**, at every+/// entry point and in both states.+///+/// Lucene's is not. `SloppyPhraseMatcher`'s class javadoc: «for query "a+/// b"~2, a document "x a b a y" can be matched twice: once for "a b"+/// (distance=0), and once for "b a" (distance=2)» — so ES answers+/// `{"query": "policy merge", "slop": 2}` with docs 1 and 2, and XERJ+/// answers with none. That is the pre-existing behaviour of+/// `xerj_fts::search::phrase_positions_match` (which single-field+/// `match_phrase` has always used) and the stored-scan walk added by #230+/// mirrors it deliberately, because the two evaluators disagreeing is the+/// flush-variance class this PR exists to remove.+///+/// It is NOT a regression: before #230 the `multi_match` phrase arms tested+/// raw-substring containment, which does not match a transposition either.+/// When the walk learns transpositions, both evaluators change together and+/// this test flips — that is the point of pinning it.+#[tokio::test]+async fn sloppy_phrase_is_in_order_only_unlike_lucene() {+ let dir = TempDir::new().unwrap();+ let engine = make_engine(&dir);+ let idx = seed(&engine, "mmpp_inorder").await;++ assert_both_states(+ &idx,+ &[+ (+ json!({"multi_match": {"query": "policy merge", "fields": ["body"],+ "type": "phrase", "slop": 2}}),+ &[],+ "transposed phrase, slop 2: ES matches, XERJ does not",+ ),+ (+ json!({"match_phrase": {"body": {"query": "policy merge", "slop": 3}}}),+ &[],+ "transposed phrase, slop 3, single-field spelling: same answer",+ ),+ // The in-order control, so the case above cannot pass by+ // accident (e.g. if slop stopped being honoured at all).+ (+ json!({"multi_match": {"query": "log policy", "fields": ["body"],+ "type": "phrase", "slop": 2}}),+ &["1"],+ "in-order control at the same slop: matches",+ ),+ ],+ )+ .await;+}+/// A memtable phrase hit must also SCORE above zero — `score_query_against_doc`/// carried its own copy of the substring predicate, so a doc admitted by the/// (fixed) membership test would still score 0.0 and be dropped by scored paths.@@ -392,7 +443,8 @@ fn negative_slop_is_rejected_at_the_three_phrase_entry_points() {/// A float-encoded `slop` (`2.0`) must be honoured as 2, not refused and not/// dropped. ES reads slop with `XContentParser.intValue()` under its default/// coercing policy, which truncates a float token and narrows a numeric string-/// (`AbstractXContentParser.java:74`/`:162`/`:177`), so `{"slop": 2.0}` is a+/// (`AbstractXContentParser.java:171` `intValue(coerce)` → `:162` `parseInt`,+/// with the truncation note at `:70`), so `{"slop": 2.0}` is a/// query ES answers with slop 2.////// This test exists because an earlier revision of this branch tightened thediff --git a/engine/crates/xerj-fts/src/search.rs b/engine/crates/xerj-fts/src/search.rsindex 0da55c0b2..1ad15d53f 100644--- a/engine/crates/xerj-fts/src/search.rs+++ b/engine/crates/xerj-fts/src/search.rs@@ -331,8 +331,9 @@ pub struct PhrasePrefixQuery {/// `match_phrase_prefix`: `TextFieldMapper.createPhrasePrefixQuery` builds/// a `MultiPhrasePrefixQuery` and calls `setSlop(slop)` alongside/// `setMaxExpansions` (elasticsearch/server/src/main/java/org/- /// elasticsearch/index/mapper/TextFieldMapper.java:1269 — AGPL, read for- /// semantics only, no code copied). Defaults to 0 (adjacency), which is+ /// elasticsearch/index/mapper/TextFieldMapper.java:2019, the two setters+ /// at :2028-:2029, reached from `phrasePrefixQuery` at :1269 — AGPL, read+ /// for semantics only, no code copied). Defaults to 0 (adjacency), which is/// the pre-existing behaviour for every caller that does not set it.#[serde(default)]pub slop: u32,diff --git a/engine/crates/xerj-query/src/parser.rs b/engine/crates/xerj-query/src/parser.rsindex 43f8560d4..fe8c19318 100644--- a/engine/crates/xerj-query/src/parser.rs+++ b/engine/crates/xerj-query/src/parser.rs@@ -561,10 +561,12 @@ fn parse_match_phrase(params: &Value) -> Result<QueryNode> {/// does under its default number policy (`DEFAULT_NUMBER_COERCE_POLICY = true`):/// an integer is taken as written, a **float is truncated** toward zero, and a/// numeric **string** is read as a double and then narrowed-/// (`libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java:74`-/// — "the 3rd party parsers we rely on are known to silently truncate-/// fractions" — plus `:162` `parseInt` and `:177` `intValue`; AGPL, read for-/// semantics only, nothing copied).+/// (`libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java`:+/// `:70` — "the 3rd party parsers we rely on are known to silently truncate+/// fractions", guarding `ensureNumberConversion` at `:74`, which only raises+/// when `coerce == false`; `:171` `intValue(coerce)`, whose `VALUE_STRING`+/// arm calls `:162` `parseInt`, itself a `Double.parseDouble` narrowed with+/// `(int)`; AGPL, read for semantics only, nothing copied).////// Why it exists: JSON encoders that carry every number as a double emit/// `"slop": 2.0`, and ES answers that query with slop 2. Reading it with@@ -714,7 +716,9 @@ fn parse_multi_match(params: &Value) -> Result<QueryNode> {// `bool_prefix` (…/index/query/MultiMatchQueryBuilder.java:673) and// `TextFieldMapper.createPhrasePrefixQuery` calls// `MultiPhrasePrefixQuery.setSlop(slop)` (…/index/mapper/- // TextFieldMapper.java:1269 — AGPL, read for semantics only). Both XERJ+ // TextFieldMapper.java:2019, `setSlop` at :2028, reached from the+ // `phrasePrefixQuery` entry at :1269 — AGPL, read for semantics only).+ // Both XERJ// evaluators now carry it: `PhrasePrefixQuery.slop` on the segment side// and the sloppy prefix walk in the stored-doc scan. Refusing it here// would have been a wire-compat break on a query ES answers, and would@@ -4590,7 +4594,8 @@ mod tests {/// Values ES *accepts by coercing* must be accepted here too. ES reads/// `slop` and `max_expansions` with `XContentParser.intValue()` under the/// default coercing policy, which truncates a float token and narrows a- /// numeric string (`AbstractXContentParser.java:74`/`:162`/`:177`), so+ /// numeric string (`AbstractXContentParser.java:171` `intValue(coerce)`,+ /// `:162` `parseInt`, `:70`), so/// `{"slop": 2.0}` is a request ES answers with slop 2. Refusing it would/// be a new 400 on a query that works today, and reading it as 0 would be/// the accept-and-ignore this parser exists to remove — both are wrong.