Skip to content

armgit / xerj

Public Imported from GitHub
0

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.

fix(xccode): send k on the semantic clause so the vector leg gets the fetch window (#1145)

Tom Villani64974a1c5332
diff --git a/engine/crates/xerj-common/src/xccode/mod.rs b/engine/crates/xerj-common/src/xccode/mod.rs
index c2fc9e9fa..34e8ffbd7 100644
--- a/engine/crates/xerj-common/src/xccode/mod.rs
+++ b/engine/crates/xerj-common/src/xccode/mod.rs
@@ -515,8 +515,11 @@ pub(crate) fn bm25_body(query: &str, k: usize, lang: &Option<String>, fields: &[
/// The vector-only body. Aimed ONLY at indices whose `body` is semantic_text
/// (a semantic query against plain text 400s the whole wildcard).
+///
+/// The clause carries `k` = the fetch window: without it the parser cuts the
+/// vector pool to 10 whatever `size` says (#1145).
pub(crate) fn semantic_body(query: &str, k: usize, lang: &Option<String>) -> Value {
- let q = serde_json::json!({ "semantic": { "field": "body", "query": query } });
+ let q = serde_json::json!({ "semantic": { "field": "body", "query": query, "k": k } });
let query = match lang {
Some(lg) => serde_json::json!({
"bool": { "must": [q, { "match": { "language": lg } }] }
@@ -549,8 +552,11 @@ pub(crate) fn hybrid_body(
let bm = wrap(serde_json::json!({
"multi_match": { "query": query, "fields": fields }
}));
+ // `k` on the semantic clause: the vector leg's pool is cut to its own
+ // `k` (parser default 10), not to `size`, so without it the #1137
+ // overfetch widens the BM25 leg alone (#1145).
let sem = wrap(serde_json::json!({
- "semantic": { "field": "body", "query": query }
+ "semantic": { "field": "body", "query": query, "k": k }
}));
serde_json::json!({
"size": k,
@@ -876,6 +882,28 @@ mod tests {
);
}
+ /// #1145: a `semantic` clause without `k` is cut to the parser default
+ /// (10), so the vector leg must carry the same window as `size` — else
+ /// `--mode semantic -k 20` returns 10 hits and hybrid fuses BM25@50 with
+ /// vector@10 (the #1137 overfetch widened the BM25 leg alone).
+ #[test]
+ fn the_vector_leg_carries_the_fetch_window_as_k() {
+ let sem = semantic_body("q", 50, &None);
+ assert_eq!(sem.pointer("/size"), Some(&serde_json::json!(50)));
+ assert_eq!(
+ sem.pointer("/query/semantic/k"),
+ Some(&serde_json::json!(50))
+ );
+
+ let fields = vec!["body".to_string()];
+ let hy = hybrid_body("q", 50, &None, &fields);
+ assert_eq!(
+ hy.pointer("/query/hybrid/queries/1/query/semantic/k"),
+ Some(&serde_json::json!(50)),
+ "{hy}"
+ );
+ }
+
#[test]
fn hybrid_without_a_capable_index_degrades_to_bm25_and_says_so() {
let root = root_with_state();