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(api): accept highlight.fields as an array of field objects (#1946) (#1950)

xerj-org36c4400f5eb5
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 35c8fb061..1903eaca4 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -7547,7 +7547,18 @@ fn parse_highlight(hl_val: &Value) -> Option<xerj_query::ast::HighlightRequest>
use xerj_query::ast::{HighlightFieldOptions, HighlightRequest};
let obj = hl_val.as_object()?;
- let fields_val = obj.get("fields")?.as_object()?;
+ // `fields` is an object, or ES's ordered form: an array of single-field
+ // objects (`[{"title": {}}, {"body": {}}]`). The array used to fail
+ // `as_object()` and drop highlighting with no error (#1946).
+ let field_entries: Vec<(&String, &Value)> = match obj.get("fields")? {
+ Value::Object(m) => m.iter().collect(),
+ Value::Array(items) => items
+ .iter()
+ .filter_map(Value::as_object)
+ .flat_map(|m| m.iter())
+ .collect(),
+ _ => return None,
+ };
fn pull_tag(
obj: &serde_json::Map<String, Value>,
@@ -7563,7 +7574,7 @@ fn parse_highlight(hl_val: &Value) -> Option<xerj_query::ast::HighlightRequest>
}
let mut fields = std::collections::HashMap::new();
- for (field_name, field_opts) in fields_val {
+ for (field_name, field_opts) in field_entries {
let opts = if let Some(o) = field_opts.as_object() {
HighlightFieldOptions {
pre_tag: pull_tag(o, "pre_tags", "pre_tag"),
diff --git a/engine/crates/xerj-api/tests/highlight_fields_array_form.rs b/engine/crates/xerj-api/tests/highlight_fields_array_form.rs
new file mode 100644
index 000000000..0d8b437bc
--- /dev/null
+++ b/engine/crates/xerj-api/tests/highlight_fields_array_form.rs
@@ -0,0 +1,111 @@
+//! Issue #1946: `highlight.fields` given as an array of single-field objects
+//! (the ES form that fixes field order) was silently ignored: no `highlight`
+//! in the hits and no error. Only the object form worked.
+//!
+//! Elasticsearch is referenced for wire semantics only; no ES code is
+//! reproduced here.
+
+use axum::body::Body;
+use axum::http::{Request, StatusCode};
+use serde_json::{json, Value};
+use tower::ServiceExt;
+
+async fn app() -> (axum::Router, tempfile::TempDir) {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let mut config = xerj_common::config::Config::default();
+ config.server.data_dir = dir.path().to_string_lossy().into_owned();
+ config.storage.wal_sync = xerj_common::config::WalSync::Async;
+ let metrics = xerj_common::metrics::Metrics::new().expect("metrics");
+ let engine = xerj_engine::Engine::new(config.clone()).expect("engine");
+ let state = xerj_api::state::AppState::new(config, engine, metrics);
+ (xerj_api::router::build_es_compat_router(state), dir)
+}
+
+async fn send(
+ app: &axum::Router,
+ method: &str,
+ path: &str,
+ ctype: &str,
+ body: String,
+) -> (StatusCode, Value) {
+ let mut req = Request::builder().method(method).uri(path);
+ if !body.is_empty() {
+ req = req.header("content-type", ctype);
+ }
+ let response = app
+ .clone()
+ .oneshot(req.body(Body::from(body)).unwrap())
+ .await
+ .unwrap();
+ let status = response.status();
+ let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
+ .await
+ .unwrap();
+ (
+ status,
+ serde_json::from_slice(&bytes).unwrap_or(Value::Null),
+ )
+}
+
+async fn seeded() -> (axum::Router, tempfile::TempDir) {
+ let (app, dir) = app().await;
+ let (st, resp) = send(
+ &app,
+ "PUT",
+ "/hl",
+ "application/json",
+ json!({"mappings": {"properties": {"body": {"type": "text"}}}}).to_string(),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "create: {resp}");
+ let (st, resp) = send(
+ &app,
+ "PUT",
+ "/hl/_doc/1?refresh=true",
+ "application/json",
+ json!({"body": "Pricing is per-node for Professional."}).to_string(),
+ )
+ .await;
+ assert_eq!(st, StatusCode::CREATED, "index: {resp}");
+ (app, dir)
+}
+
+#[tokio::test]
+async fn highlight_fields_array_form_matches_the_object_form() {
+ let (app, _dir) = seeded().await;
+ let object_form = json!({
+ "query": {"match": {"body": "pricing"}},
+ "highlight": {"fields": {"body": {"pre_tags": ["["], "post_tags": ["]"]}}}
+ });
+ let array_form = json!({
+ "query": {"match": {"body": "pricing"}},
+ "highlight": {"fields": [{"body": {"pre_tags": ["["], "post_tags": ["]"]}}]}
+ });
+ let (st, want) = send(
+ &app,
+ "POST",
+ "/hl/_search",
+ "application/json",
+ object_form.to_string(),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "{want}");
+ let (st, got) = send(
+ &app,
+ "POST",
+ "/hl/_search",
+ "application/json",
+ array_form.to_string(),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "{got}");
+ assert_eq!(
+ got["hits"]["hits"][0]["highlight"],
+ json!({"body": ["[Pricing] is per-node for Professional."]}),
+ "{got}"
+ );
+ assert_eq!(
+ got["hits"]["hits"][0]["highlight"],
+ want["hits"]["hits"][0]["highlight"]
+ );
+}