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(search): print a heading when a hit has no passage (#1665)

xerj-orgf4e99d96a5fe
diff --git a/engine/crates/xerj-autoindex/src/search.rs b/engine/crates/xerj-autoindex/src/search.rs
index 11082b05e..d0eda325e 100644
--- a/engine/crates/xerj-autoindex/src/search.rs
+++ b/engine/crates/xerj-autoindex/src/search.rs
@@ -67,6 +67,29 @@ pub(crate) fn passage_text(hit: &Value) -> Option<String> {
}
}
+/// Text to print for one hit.
+///
+/// Passage first, then the symbol `code`. When the engine matched a document
+/// but selected no passage and there is no `code`, a section `heading` or
+/// document `title` is what the header would otherwise sit on top of — a
+/// blank block.
+pub(crate) fn hit_display_text(hit: &Value) -> Option<String> {
+ if let Some(text) = passage_text(hit) {
+ return Some(text);
+ }
+ let src = hit.get("_source")?;
+ for key in ["code", "heading", "title"] {
+ if let Some(text) = src
+ .get(key)
+ .and_then(Value::as_str)
+ .filter(|s| !s.is_empty())
+ {
+ return Some(text.to_string());
+ }
+ }
+ None
+}
+
/// True when the query looks like a single code identifier (`IOSIntfLine`,
/// `euler_to_rotationmatrix`) rather than a plain-words phrase — the case
/// where `xerj def` is the better tool.
@@ -316,7 +339,7 @@ fn build_search_query(
"query": { "bool": { "should": should }},
// No `body` here: whole-file bodies are the token flood (measured ~9x
// the size of the matching passage). `_passage` carries the snippet.
- "_source": ["ax_path", "line", "start_line", "language", "kind", "code", "title"],
+ "_source": ["ax_path", "line", "start_line", "language", "kind", "code", "title", "heading"],
"fields": ["_passage"]
})
}
@@ -806,12 +829,10 @@ pub fn run_search_cli() -> i32 {
// Passage-first: `_passage` is the engine's matching snippet (an
// array-wrapped object with a text payload); `code` is the symbol
// declaration. Never the whole file body — that is the 9x token flood
- // this command exists to avoid.
- let passage_owned = passage_text(h);
- let passage = passage_owned
- .as_deref()
- .or_else(|| src.get("code").and_then(Value::as_str));
- if let Some(text) = passage {
+ // this command exists to avoid. A hit with neither prints its heading
+ // or title rather than a blank block.
+ let passage_owned = hit_display_text(h);
+ if let Some(text) = passage_owned.as_deref() {
let snippet: String = text.chars().take(full).collect();
for l in snippet.lines() {
println!(" {l}");
@@ -956,6 +977,56 @@ mod tests {
}
});
assert_eq!(passage_text(&hit), None);
+ assert_eq!(
+ hit_display_text(&hit).as_deref(),
+ Some("de <- function(col)")
+ );
+ }
+
+ /// A notebook hit can match on BM25 and still come back with no `_passage`.
+ /// The human line then has a path and a score and nothing under them.
+ /// Heading wins over title; a real passage still wins over both.
+ #[test]
+ fn a_hit_with_no_passage_prints_its_heading_or_title() {
+ let headed = json!({
+ "_source": {
+ "ax_path": "10-Iterators.ipynb",
+ "title": "Iterators",
+ "heading": "map and filter"
+ }
+ });
+ assert_eq!(hit_display_text(&headed).as_deref(), Some("map and filter"));
+
+ let titled = json!({
+ "_source": { "ax_path": "Index.ipynb", "title": "Index" }
+ });
+ assert_eq!(hit_display_text(&titled).as_deref(), Some("Index"));
+
+ let with_passage = json!({
+ "_source": {
+ "ax_path": "a.ipynb",
+ "title": "should not win",
+ "heading": "nope"
+ },
+ "fields": { "_passage": [{ "field": "body", "text": "the matching cell" }] }
+ });
+ assert_eq!(
+ hit_display_text(&with_passage).as_deref(),
+ Some("the matching cell")
+ );
+
+ let blank = json!({ "_source": { "ax_path": "empty.ipynb" } });
+ assert_eq!(hit_display_text(&blank), None);
+
+ let source = search_query_body("map and filter", 5);
+ assert!(
+ source["_source"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .any(|f| f == "heading"),
+ "{source}"
+ );
}
/// #1365: whole-file hits (`ax-code-*`, `ax-docs-md-*`, `ax-text-*`) carry