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(engine): geo_point aggs/queries reject string-encoded lat/lon

Vincenzo Lombardo3ba48e3e7c20
diff --git a/engine/crates/xerj-engine/src/aggs.rs b/engine/crates/xerj-engine/src/aggs.rs
index e0d4904c5..245d21b73 100644
--- a/engine/crates/xerj-engine/src/aggs.rs
+++ b/engine/crates/xerj-engine/src/aggs.rs
@@ -8571,11 +8571,22 @@ fn encode_geohash(lat: f64, lon: f64, precision: usize) -> String {
/// - "POINT (lon lat)"
/// - "lat,lon"
/// - GeoHash string (not parsed — returns None)
+/// Coerce a `lat`/`lon` object member to `f64`. Real ES accepts numeric
+/// OR string-encoded lat/lon inside a `{"lat": .., "lon": ..}` geo_point
+/// object (normalised to numbers at index time) — Kibana's own flights
+/// sample data ships exactly this shape (`{"lat": "50.033333", ...}`),
+/// which `.as_f64()` alone silently rejects (`None` for every doc, so
+/// every geo agg/query on that field returns empty).
+fn geo_coord(v: &Value) -> Option<f64> {
+ v.as_f64()
+ .or_else(|| v.as_str().and_then(|s| s.trim().parse().ok()))
+}
+
fn parse_geo_point(v: &Value) -> Option<(f64, f64)> {
match v {
Value::Object(o) => {
- let lat = o.get("lat")?.as_f64()?;
- let lon = o.get("lon")?.as_f64()?;
+ let lat = geo_coord(o.get("lat")?)?;
+ let lon = geo_coord(o.get("lon")?)?;
Some((lat, lon))
}
Value::Array(arr) if arr.len() == 2 => {
@@ -9872,8 +9883,10 @@ fn run_extended_stats<'d>(params: &Value, _docs: &'d [Value], cache: &mut FieldC
fn extract_geo_point(v: &Value) -> Option<(f64, f64)> {
match v {
Value::Object(obj) => {
- let lat = obj.get("lat").and_then(Value::as_f64)?;
- let lon = obj.get("lon").and_then(Value::as_f64)?;
+ // See `geo_coord`: string-encoded lat/lon (Kibana's own flights
+ // sample data) must be accepted, not just numeric.
+ let lat = geo_coord(obj.get("lat")?)?;
+ let lon = geo_coord(obj.get("lon")?)?;
Some((lat, lon))
}
Value::String(s) => {
@@ -9909,13 +9922,13 @@ fn run_geo_bounds(params: &Value, docs: &[Value]) -> Value {
let mut max_lon = f64::NEG_INFINITY;
for doc in docs {
- if let Some(v) = doc.get(field) {
- if let Some((lat, lon)) = extract_geo_point(v) {
- min_lat = min_lat.min(lat);
- max_lat = max_lat.max(lat);
- min_lon = min_lon.min(lon);
- max_lon = max_lon.max(lon);
- }
+ // See `run_geo_centroid` — nested geo_point fields (e.g.
+ // `geoip.location`) are invisible to a flat `doc.get(field)`.
+ if let Some((lat, lon)) = extract_geo_point(get_nested_field(doc, field)) {
+ min_lat = min_lat.min(lat);
+ max_lat = max_lat.max(lat);
+ min_lon = min_lon.min(lon);
+ max_lon = max_lon.max(lon);
}
}
@@ -9943,12 +9956,14 @@ fn run_geo_centroid(params: &Value, docs: &[Value]) -> Value {
let mut count = 0usize;
for doc in docs {
- if let Some(v) = doc.get(field) {
- if let Some((lat, lon)) = extract_geo_point(v) {
- sum_lat += lat;
- sum_lon += lon;
- count += 1;
- }
+ // `get_nested_field`, not a flat `doc.get(field)` — geo_point
+ // fields are routinely nested (e.g. eCommerce's `geoip.location`),
+ // and a flat lookup never finds them (silent `null` centroid, which
+ // Kibana's map viz then crashes reading `.lon` off).
+ if let Some((lat, lon)) = extract_geo_point(get_nested_field(doc, field)) {
+ sum_lat += lat;
+ sum_lon += lon;
+ count += 1;
}
}
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 0233b97b7..65b077af5 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -20504,6 +20504,17 @@ fn postings_union_expand(
/// Evaluate a query against a single stored document source value.
///
/// Returns true if the document matches the query.
+/// Coerce a `lat`/`lon` object member to `f64`. Real ES accepts numeric OR
+/// string-encoded lat/lon inside a `{"lat": .., "lon": ..}` geo_point object
+/// (normalised to numbers at index time) — Kibana's own flights sample data
+/// ships exactly this shape (`{"lat": "50.033333", ...}`), which `.as_f64()`
+/// alone silently rejects, turning every `geo_distance`/`geo_bounding_box`
+/// query against that field into an unconditional non-match.
+fn geo_coord(v: &Value) -> Option<f64> {
+ v.as_f64()
+ .or_else(|| v.as_str().and_then(|s| s.trim().parse().ok()))
+}
+
fn doc_matches_query(q: &QueryNode, source: &Value) -> bool {
match q {
QueryNode::MatchAll => true,
@@ -21164,8 +21175,8 @@ fn doc_matches_query(q: &QueryNode, source: &Value) -> bool {
// Accept {"lat": f64, "lon": f64} or [lon, lat] or "lat,lon".
let (doc_lat, doc_lon) = match &v {
Value::Object(obj) => {
- let dlat = obj.get("lat").and_then(|x| x.as_f64())?;
- let dlon = obj.get("lon").and_then(|x| x.as_f64())?;
+ let dlat = geo_coord(obj.get("lat")?)?;
+ let dlon = geo_coord(obj.get("lon")?)?;
(dlat, dlon)
}
Value::Array(arr) if arr.len() == 2 => {
@@ -21363,8 +21374,8 @@ fn doc_matches_query(q: &QueryNode, source: &Value) -> bool {
.and_then(|v| {
let (doc_lat, doc_lon) = match &v {
Value::Object(obj) => {
- let dlat = obj.get("lat").and_then(|x| x.as_f64())?;
- let dlon = obj.get("lon").and_then(|x| x.as_f64())?;
+ let dlat = geo_coord(obj.get("lat")?)?;
+ let dlon = geo_coord(obj.get("lon")?)?;
(dlat, dlon)
}
Value::Array(arr) if arr.len() == 2 => {