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(hybrid): fail-loud 400 for learned fusion instead of silent RRF

xerj-org2c6d36cac044
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 2726f1407..810edd2e0 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -3701,9 +3701,15 @@ impl Index {
let fused = match fusion {
xerj_query::ast::FusionStrategy::Rrf { k } => fuse_rrf(&sub_results, k),
xerj_query::ast::FusionStrategy::Linear => fuse_linear(&sub_results),
+ // Defense in depth: the parser already rejects `fusion: learned`
+ // with a 400 (see xerj-query parser.rs::parse_hybrid), so this
+ // arm is unreachable via the ES API. Fail loud rather than
+ // silently substituting RRF, in case a future non-parser caller
+ // constructs the AST directly.
xerj_query::ast::FusionStrategy::Learned => {
- warn!("Hybrid: Learned fusion not implemented, falling back to RRF(k=60)");
- fuse_rrf(&sub_results, 60)
+ return Err(EngineError::Common(xerj_common::XerjError::invalid_query(
+ "hybrid fusion learned is not yet supported; use rrf or linear",
+ )));
}
};
diff --git a/engine/crates/xerj-query/src/parser.rs b/engine/crates/xerj-query/src/parser.rs
index 5366238d1..5c0645bb9 100644
--- a/engine/crates/xerj-query/src/parser.rs
+++ b/engine/crates/xerj-query/src/parser.rs
@@ -2233,7 +2233,12 @@ fn parse_hybrid(params: &Value) -> Result<QueryNode> {
Some(Value::String(s)) => match s.as_str() {
"rrf" => FusionStrategy::Rrf { k: 60 },
"linear" => FusionStrategy::Linear,
- "learned" => FusionStrategy::Learned,
+ // Learned fusion is not implemented — fail loud rather than
+ // silently substituting RRF (which would misrepresent the
+ // ranking the caller asked for). AST variant kept for future.
+ "learned" => {
+ return invalid("hybrid fusion learned is not yet supported; use rrf or linear")
+ }
other => return invalid(format!("unknown hybrid fusion strategy `{other}`")),
},
Some(Value::Object(m)) => {
@@ -2244,7 +2249,10 @@ fn parse_hybrid(params: &Value) -> Result<QueryNode> {
FusionStrategy::Rrf { k }
}
"linear" => FusionStrategy::Linear,
- "learned" => FusionStrategy::Learned,
+ // Learned fusion is not implemented — fail loud (see above).
+ "learned" => {
+ return invalid("hybrid fusion learned is not yet supported; use rrf or linear")
+ }
other => return invalid(format!("unknown hybrid fusion strategy `{other}`")),
}
}
@@ -4327,6 +4335,28 @@ mod tests {
}
}
+ #[test]
+ fn test_hybrid_learned_rejected() {
+ // Learned fusion is not implemented — must fail loud rather than
+ // silently substituting RRF. Both the string and object forms.
+ for fusion in [json!("learned"), json!({ "type": "learned" })] {
+ let err = parse_query(&json!({
+ "hybrid": {
+ "queries": [
+ {"query": {"match": {"title": "rust"}}},
+ {"query": {"match": {"title": "go"}}}
+ ],
+ "fusion": fusion
+ }
+ }))
+ .unwrap_err();
+ assert!(
+ format!("{err}").contains("learned is not yet supported"),
+ "unexpected error: {err}"
+ );
+ }
+ }
+
#[test]
fn test_nested_bool() {
let node = q(json!({
diff --git a/engine/tests/es-compat-yaml/yaml/vectors/90_hybrid_learned_rejected.yml b/engine/tests/es-compat-yaml/yaml/vectors/90_hybrid_learned_rejected.yml
new file mode 100644
index 000000000..3e77f2cce
--- /dev/null
+++ b/engine/tests/es-compat-yaml/yaml/vectors/90_hybrid_learned_rejected.yml
@@ -0,0 +1,83 @@
+setup:
+
+ - do:
+ indices.create:
+ index: hybrid_learned_test
+ body:
+ settings:
+ number_of_shards: 1
+ mappings:
+ properties:
+ title:
+ type: text
+
+ - do:
+ bulk:
+ refresh: true
+ body:
+ - '{ "index": { "_index": "hybrid_learned_test", "_id": "1" } }'
+ - '{ "title": "quick brown fox" }'
+ - '{ "index": { "_index": "hybrid_learned_test", "_id": "2" } }'
+ - '{ "title": "lazy brown dog" }'
+
+---
+"hybrid query with learned fusion is rejected (400)":
+
+ # Learned fusion is not implemented. XERJ fails loud rather than
+ # silently substituting RRF, which would misrepresent the ranking.
+ - do:
+ catch: bad_request
+ search:
+ index: hybrid_learned_test
+ body:
+ query:
+ hybrid:
+ queries:
+ - query:
+ match:
+ title: "brown"
+ - query:
+ match:
+ title: "fox"
+ fusion: "learned"
+
+---
+"hybrid query with learned fusion object form is rejected (400)":
+
+ - do:
+ catch: bad_request
+ search:
+ index: hybrid_learned_test
+ body:
+ query:
+ hybrid:
+ queries:
+ - query:
+ match:
+ title: "brown"
+ - query:
+ match:
+ title: "fox"
+ fusion:
+ type: "learned"
+
+---
+"hybrid query with rrf fusion still works":
+
+ - do:
+ search:
+ rest_total_hits_as_int: true
+ index: hybrid_learned_test
+ body:
+ query:
+ hybrid:
+ queries:
+ - query:
+ match:
+ title: "brown"
+ - query:
+ match:
+ title: "fox"
+ fusion: "rrf"
+
+ - match: { hits.total: 2 }