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(query): fail-loud 400 for has_child / has_parent join queries

xerj-org81ecde7cdbf7
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 8299f6d31..2726f1407 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -13262,11 +13262,13 @@ fn doc_matches_query(q: &QueryNode, source: &Value) -> bool {
tokens[..limit].iter().any(|t| t == &v_lower)
}
- // ── Join queries ──────────────────────────────────────────────────────
-
- // HasChild / HasParent: single-index simplification — run the inner query.
- // In a real parent-child setup we'd query the child/parent type; here we
- // treat all docs as a flat set and just run the inner query.
+ // ── Join queries — UNREACHABLE ─────────────────────────────────────────
+ // has_child/has_parent are rejected with a 400 at parse time
+ // (see xerj-query parser.rs::parse_has_child), so these AST variants
+ // are never built and these branches never execute. Previously they
+ // silently ran the inner query on the flat doc set, which returned
+ // wrong results (no parent/child join is materialized). Kept for
+ // exhaustiveness / a future real join executor.
QueryNode::HasChild { query, .. } => doc_matches_query(query, source),
QueryNode::HasParent { query, .. } => doc_matches_query(query, source),
diff --git a/engine/crates/xerj-query/src/parser.rs b/engine/crates/xerj-query/src/parser.rs
index e1473e3f8..5366238d1 100644
--- a/engine/crates/xerj-query/src/parser.rs
+++ b/engine/crates/xerj-query/src/parser.rs
@@ -3019,60 +3019,32 @@ fn parse_span_containing_like(query_type: &str, params: &Value) -> Result<QueryN
/// Parse a `has_child` query.
///
/// `{"has_child": {"type": "answer", "query": {...}, "score_mode": "avg"}}`
-fn parse_has_child(params: &Value) -> Result<QueryNode> {
- let obj = params
- .as_object()
- .ok_or_else(|| qerr("`has_child` must be an object"))?;
-
- let child_type = obj
- .get("type")
- .and_then(|v| v.as_str())
- .unwrap_or("_doc")
- .to_string();
-
- let query = obj
- .get("query")
- .ok_or_else(|| qerr("`has_child` requires a `query`"))
- .and_then(parse_query)?;
-
- let score_mode = obj
- .get("score_mode")
- .and_then(|v| v.as_str())
- .map(str::to_string);
-
- Ok(QueryNode::HasChild {
- child_type,
- query: Box::new(query),
- score_mode,
- })
+///
+/// Parent-child joins are NOT supported. XERJ is single-type per index
+/// and never materializes the parent/child join structure, so both the
+/// planner and executor branches for `HasChild` would silently fall back
+/// to running the inner query on flat docs (wrong results). We fail loud
+/// (400) at parse time instead — the `QueryNode::HasChild` AST variant is
+/// therefore never built, and the downstream branches are unreachable.
+fn parse_has_child(_params: &Value) -> Result<QueryNode> {
+ invalid(
+ "parent-child join queries (has_child/has_parent) are not supported; \
+ index as a single flat type or denormalize the relationship",
+ )
}
/// Parse a `has_parent` query.
///
/// `{"has_parent": {"parent_type": "question", "query": {...}, "score": true}}`
-fn parse_has_parent(params: &Value) -> Result<QueryNode> {
- let obj = params
- .as_object()
- .ok_or_else(|| qerr("`has_parent` must be an object"))?;
-
- let parent_type = obj
- .get("parent_type")
- .and_then(|v| v.as_str())
- .unwrap_or("_doc")
- .to_string();
-
- let query = obj
- .get("query")
- .ok_or_else(|| qerr("`has_parent` requires a `query`"))
- .and_then(parse_query)?;
-
- let score = obj.get("score").and_then(|v| v.as_bool()).unwrap_or(false);
-
- Ok(QueryNode::HasParent {
- parent_type,
- query: Box::new(query),
- score,
- })
+///
+/// See `parse_has_child`: parent-child joins are unsupported and rejected
+/// with a 400 at parse time, so the `QueryNode::HasParent` variant is
+/// never constructed.
+fn parse_has_parent(_params: &Value) -> Result<QueryNode> {
+ invalid(
+ "parent-child join queries (has_child/has_parent) are not supported; \
+ index as a single flat type or denormalize the relationship",
+ )
}
/// Parse a `geo_polygon` query.
@@ -4007,6 +3979,41 @@ mod tests {
assert!(matches!(node, QueryNode::Term { .. }));
}
+ // ── parent-child join (unsupported) ────────────────────────────────────────
+
+ #[test]
+ fn test_has_child_rejected() {
+ // Parent-child joins are not materialized in XERJ; parsing must
+ // fail loud (400) rather than build an AST node that silently runs
+ // the inner query on flat docs.
+ let err = parse_query(&json!({
+ "has_child": {
+ "type": "answer",
+ "query": { "match": { "body": "hello" } }
+ }
+ }))
+ .unwrap_err();
+ assert!(
+ format!("{err}").contains("parent-child join queries"),
+ "unexpected error: {err}"
+ );
+ }
+
+ #[test]
+ fn test_has_parent_rejected() {
+ let err = parse_query(&json!({
+ "has_parent": {
+ "parent_type": "question",
+ "query": { "match": { "body": "hello" } }
+ }
+ }))
+ .unwrap_err();
+ assert!(
+ format!("{err}").contains("parent-child join queries"),
+ "unexpected error: {err}"
+ );
+ }
+
// ── query_string ──────────────────────────────────────────────────────────
#[test]
diff --git a/engine/crates/xerj-query/src/planner.rs b/engine/crates/xerj-query/src/planner.rs
index ddca7dec7..32da144aa 100644
--- a/engine/crates/xerj-query/src/planner.rs
+++ b/engine/crates/xerj-query/src/planner.rs
@@ -629,7 +629,11 @@ fn plan_node(query: QueryNode, schema: &Schema) -> ExecutionPlan {
QueryNode::SpanFirst { match_query, .. } => plan_node(*match_query, schema),
- // ── Join queries — run the inner query as a doc-scan ──────────────────
+ // ── Join queries — UNREACHABLE ────────────────────────────────────────
+ // has_child/has_parent are rejected with a 400 at parse time
+ // (see parser.rs::parse_has_child), so these AST variants are never
+ // built and this branch is never taken. Kept for exhaustiveness and
+ // in case the AST variant is ever wired to a real join executor.
QueryNode::HasChild { query, .. } | QueryNode::HasParent { query, .. } => {
let inner = plan_node(*query, schema);
let cost = inner.cost() + 10.0;
diff --git a/engine/tests/es-compat-yaml/yaml/search/610_parent_child_unsupported.yml b/engine/tests/es-compat-yaml/yaml/search/610_parent_child_unsupported.yml
new file mode 100644
index 000000000..026c2684f
--- /dev/null
+++ b/engine/tests/es-compat-yaml/yaml/search/610_parent_child_unsupported.yml
@@ -0,0 +1,54 @@
+setup:
+
+ - do:
+ indices.create:
+ index: parent_child_test
+ body:
+ settings:
+ number_of_shards: 1
+ mappings:
+ properties:
+ body:
+ type: text
+
+ - do:
+ bulk:
+ refresh: true
+ body:
+ - '{ "index": { "_index": "parent_child_test", "_id": "1" } }'
+ - '{ "body": "a question" }'
+ - '{ "index": { "_index": "parent_child_test", "_id": "2" } }'
+ - '{ "body": "an answer" }'
+
+---
+"has_child query is rejected (400)":
+
+ # XERJ is single-type per index and never materializes a parent/child
+ # join, so has_child is rejected at parse time rather than silently
+ # running the inner query on flat docs (which returned wrong results).
+ - do:
+ catch: bad_request
+ search:
+ index: parent_child_test
+ body:
+ query:
+ has_child:
+ type: answer
+ query:
+ match:
+ body: "answer"
+
+---
+"has_parent query is rejected (400)":
+
+ - do:
+ catch: bad_request
+ search:
+ index: parent_child_test
+ body:
+ query:
+ has_parent:
+ parent_type: question
+ query:
+ match:
+ body: "question"