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.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
fix(query): ES-exact date resolution for range bounds (rounding, format, date math)
xerj-org5a1ab59149f8
diff --git a/engine/crates/xerj-api/src/error.rs b/engine/crates/xerj-api/src/error.rsindex 65e74be1a..59b4558dd 100644--- a/engine/crates/xerj-api/src/error.rs+++ b/engine/crates/xerj-api/src/error.rs@@ -106,10 +106,37 @@ impl IntoResponse for ApiError {// Try to extract index name for root_cause annotation.let index_name = extract_index_name(&self.inner);+ // ES date-resolution errors from the range parser (invalid `format`+ // pattern, value that fails an explicit format, malformed date+ // math). The query crate's Display wraps them as+ // `parse error: <msg>`; strip that so the reason matches ES+ // byte-for-byte, and surface ES's exception types+ // (`illegal_argument_exception` for a bad format string,+ // `parse_exception` in root_cause for unparseable values/math).+ let mut reason = reason;+ let mut date_root: Option<&'static str> = None;+ {+ let stripped = reason.strip_prefix("parse error: ").unwrap_or(&reason);+ if stripped.starts_with("Invalid format: [") {+ reason = stripped.to_string();+ error_type = "illegal_argument_exception".into();+ status_code = 400;+ date_root = Some("illegal_argument_exception");+ } else if stripped.starts_with("failed to parse date field [")+ || stripped.starts_with("operator not supported for date math [")+ {+ reason = stripped.to_string();+ status_code = 400;+ date_root = Some("parse_exception");+ }+ }+// ES specific-case: `failed to create query:` comes from shard-// level query builders and reports `query_shard_exception` in// root_cause while keeping the outer wrapper as its usual type.- let root_type = if reason.starts_with("failed to create query:") {+ let root_type = if let Some(rt) = date_root {+ rt.to_string()+ } else if reason.starts_with("failed to create query:") {"query_shard_exception".to_string()} else if reason.starts_with("function score query returned an invalid score:") {// ES validates function_score results and raisesdiff --git a/engine/crates/xerj-engine/src/aggs.rs b/engine/crates/xerj-engine/src/aggs.rsindex 38e891c12..88267fd00 100644--- a/engine/crates/xerj-engine/src/aggs.rs+++ b/engine/crates/xerj-engine/src/aggs.rs@@ -3870,6 +3870,29 @@ pub(crate) fn parse_date_ms(val: &Value) -> Option<i64> {if let Ok(dt) = chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d") {return Some(dt.and_hms_opt(0, 0, 0)?.and_utc().timestamp_millis());}+ // Partial ISO dates (`strict_date_optional_time` accepts them and+ // ES indexes them min-filled): `2026-02` → 2026-02-01T00:00:00,+ // `2026-02-15T08` / `2026-02-15T08:30` → minutes/seconds zeroed.+ // Bare 4-digit years are intentionally NOT parsed here — a doc+ // value of "2026" is far more likely a numeric string, and the+ // final i64 fallback below keeps its numeric interpretation.+ if let Ok(ym) = chrono::NaiveDate::parse_from_str(&format!("{s}-01"), "%Y-%m-%d") {+ if s.len() == 7 && s.as_bytes()[4] == b'-' {+ return Some(ym.and_hms_opt(0, 0, 0)?.and_utc().timestamp_millis());+ }+ }+ if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M") {+ return Some(dt.and_utc().timestamp_millis());+ }+ if s.len() == 13 && s.as_bytes()[10] == b'T' {+ // Hour-partial (`2026-02-15T08`): append minutes so the+ // pattern terminates cleanly, then treat as HH:00.+ if let Ok(dt) =+ chrono::NaiveDateTime::parse_from_str(&format!("{s}:00"), "%Y-%m-%dT%H:%M")+ {+ return Some(dt.and_utc().timestamp_millis());+ }+ }// Slash-separated form `YYYY/MM/DD` is accepted by ES via the// default date mapping — treat it as YMD at UTC midnight.if let Ok(dt) = chrono::NaiveDate::parse_from_str(s, "%Y/%m/%d") {diff --git a/engine/crates/xerj-query/src/dates.rs b/engine/crates/xerj-query/src/dates.rsnew file mode 100644index 000000000..92fa85d21--- /dev/null+++ b/engine/crates/xerj-query/src/dates.rs@@ -0,0 +1,989 @@+//! ES-compatible date parsing and date-math resolution for range bounds.+//!+//! Elasticsearch resolves range-query date bounds in three steps:+//!+//! 1. **Parse** the value with the field's format (default+//! `strict_date_optional_time||epoch_millis`, overridable per-query via the+//! range clause's `format` parameter). Partial dates leave trailing+//! components unset.+//! 2. **Fill** missing components. The round-*down* parser fills every+//! missing field with its minimum. The round-*up* parser mirrors ES's+//! `JavaDateFormatter` round-up defaults: `month → 1`, `day → 1`,+//! `hour → 23`, `minute → 59`, `second → 59`, `milli → 999`. (So+//! `lte: "2026-02"` covers up to `2026-02-01T23:59:59.999` — the first+//! *day*, not the whole month. Verified against live ES 8.13.4 and pinned+//! by the `500_date_range.yml` conformance tests.)+//! 3. **Date math** — `now+1d/M` or `<anchor>||+1M/d`. The anchor parses+//! with *min* fill (no implicit round-up; verified live: `gt+//! "2026-02-14||+1d"` behaves exactly like `gt "2026-02-14T00:00:00||+1d"`),+//! then each `+N<unit>` / `-N<unit>` applies calendar-aware, and `/<unit>`+//! rounds — down to the start of the unit, or (for round-up bounds) to the+//! last millisecond of the unit.+//!+//! Round-up applies to `lte` (include the whole covered interval) and `gt`+//! (exclude the whole covered interval); `gte` and `lt` round down.+//!+//! Everything here works on UTC epoch milliseconds (ES date resolution).++use chrono::{Datelike, Duration, Months, NaiveDate, NaiveDateTime, Timelike};++// ─────────────────────────────────────────────────────────────────────────────+// Errors+// ─────────────────────────────────────────────────────────────────────────────++/// Why a bound could not be resolved as a date.+#[derive(Debug, PartialEq)]+pub enum DateResolveError {+ /// The `format` parameter itself is invalid (unknown pattern letter).+ /// Carries the offending letter; the caller formats the full ES message+ /// (`Invalid format: [banana]: Unknown pattern letter: b`).+ UnknownPatternLetter(char),+ /// The value failed to parse under an *explicit* `format`. Carries the+ /// original value text (the caller knows the format string).+ UnparseableValue(String),+ /// The date-math suffix contains an unsupported operator/unit. Carries+ /// the math substring for ES's+ /// `operator not supported for date math [-5ms]` message.+ BadDateMath(String),+}++// ─────────────────────────────────────────────────────────────────────────────+// Parsed (possibly partial) date+// ─────────────────────────────────────────────────────────────────────────────++/// A parsed date with per-component presence. Missing components are filled+/// according to the round mode when converting to an instant.+#[derive(Debug, Default, Clone)]+struct DateParts {+ /// Year (default 1970 for time-only formats, mirroring java.time's epoch+ /// base date).+ year: Option<i32>,+ month: Option<u32>,+ day: Option<u32>,+ hour: Option<u32>,+ minute: Option<u32>,+ second: Option<u32>,+ milli: Option<u32>,+ /// UTC offset in seconds (parsed from `Z` / `±hh[:mm]`). `None` → UTC.+ tz_secs: Option<i32>,+}++impl DateParts {+ /// Resolve to epoch milliseconds. `round_up` selects the ES round-up+ /// fill for missing fields (month/day → 1, time-of-day → max).+ fn to_epoch_ms(&self, round_up: bool) -> Option<i64> {+ let year = self.year.unwrap_or(1970);+ let month = self.month.unwrap_or(1);+ let day = self.day.unwrap_or(1);+ let (hour, minute, second, milli) = if round_up {+ (+ self.hour.unwrap_or(23),+ self.minute.unwrap_or(59),+ self.second.unwrap_or(59),+ self.milli.unwrap_or(999),+ )+ } else {+ (+ self.hour.unwrap_or(0),+ self.minute.unwrap_or(0),+ self.second.unwrap_or(0),+ self.milli.unwrap_or(0),+ )+ };+ let dt = NaiveDate::from_ymd_opt(year, month, day)?+ .and_hms_milli_opt(hour, minute, second, milli)?;+ let ms = dt.and_utc().timestamp_millis();+ Some(ms - i64::from(self.tz_secs.unwrap_or(0)) * 1000)+ }+}++// ─────────────────────────────────────────────────────────────────────────────+// Format compilation+// ─────────────────────────────────────────────────────────────────────────────++/// One member of a (possibly `||`-joined) date format list.+#[derive(Debug, Clone)]+pub enum DateFmt {+ EpochMillis,+ EpochSecond,+ /// `strict_date_optional_time` / `date_optional_time` / `iso8601` — the+ /// partial ISO parser (also the default when no `format` is given).+ IsoOptionalTime,+ /// A compiled Java-style pattern (`dd/MM/yyyy`, `uuuu`, `basic_date`, …).+ Pattern(Vec<PatTok>),+}++/// One token of a compiled Java date pattern.+#[derive(Debug, Clone, PartialEq)]+pub enum PatTok {+ /// Year; payload = minimum digit count (`yyyy` → 4).+ Year(usize),+ /// Numeric month; payload = exact-width flag (`MM` → true, `M` → false).+ Month { two_digit: bool },+ /// Text month name (`MMM`/`MMMM`) — English abbreviations/full names.+ MonthName,+ Day { two_digit: bool },+ Hour { two_digit: bool },+ Minute { two_digit: bool },+ Second { two_digit: bool },+ /// Fractional seconds (`S+`); stores as milliseconds.+ Fraction,+ /// `Z`/`X`/`x`/`z` — accepts `Z` or `±hh[:mm]` / `±hhmm` (also `UTC`/`GMT`).+ TzOffset,+ /// Day-of-week name (`E`/`e`) — consumed and discarded.+ WeekdayName,+ /// AM/PM marker (`a`); shifts a 12-hour value.+ AmPm,+ /// Verbatim text that must match exactly.+ Literal(String),+ /// A *valid* Java pattern letter this engine does not implement (e.g.+ /// week-of-year `w`). Compiling succeeds (ES accepts the format); any+ /// value parsed against it fails, producing ES's `failed to parse date+ /// field` error rather than an invalid-format error.+ Unsupported,+}++/// Java pattern letters that are valid in `java.time.format.DateTimeFormatter`+/// patterns. Anything alphabetic outside this set produces+/// `Unknown pattern letter: <c>` (ES's `Invalid format` 400).+const VALID_JAVA_PATTERN_LETTERS: &str = "GuyDMLdgQqYwWEecFahKkHmsSAnNVvzOXxZpB";++/// Compile an ES `format` string (possibly `||`-joined) into a format list.+pub fn compile_formats(format: &str) -> Result<Vec<DateFmt>, DateResolveError> {+ format.split("||").map(compile_one_format).collect()+}++fn compile_one_format(name: &str) -> Result<DateFmt, DateResolveError> {+ // Named builtins first (both strict_ and lenient joda names).+ let pattern: &str = match name {+ "epoch_millis" => return Ok(DateFmt::EpochMillis),+ "epoch_second" => return Ok(DateFmt::EpochSecond),+ "strict_date_optional_time"+ | "date_optional_time"+ | "strict_date_optional_time_nanos"+ | "date_optional_time_nanos"+ | "iso8601" => return Ok(DateFmt::IsoOptionalTime),+ "basic_date" => "yyyyMMdd",+ "basic_date_time" => "yyyyMMdd'T'HHmmss.SSSXX",+ "basic_date_time_no_millis" => "yyyyMMdd'T'HHmmssXX",+ "date" | "strict_date" | "year_month_day" | "strict_year_month_day" => "yyyy-MM-dd",+ "year" | "strict_year" => "yyyy",+ "year_month" | "strict_year_month" => "yyyy-MM",+ "date_time" | "strict_date_time" => "yyyy-MM-dd'T'HH:mm:ss.SSSXX",+ "date_time_no_millis" | "strict_date_time_no_millis" => "yyyy-MM-dd'T'HH:mm:ssXX",+ "date_hour_minute_second" | "strict_date_hour_minute_second" => "yyyy-MM-dd'T'HH:mm:ss",+ "date_hour_minute_second_millis"+ | "strict_date_hour_minute_second_millis"+ | "date_hour_minute_second_fraction"+ | "strict_date_hour_minute_second_fraction" => "yyyy-MM-dd'T'HH:mm:ss.SSS",+ "date_hour_minute" | "strict_date_hour_minute" => "yyyy-MM-dd'T'HH:mm",+ "hour_minute_second" | "strict_hour_minute_second" => "HH:mm:ss",+ "rfc3339" | "rfc3339_lenient" => return Ok(DateFmt::IsoOptionalTime),+ other => other,+ };+ compile_pattern(pattern).map(DateFmt::Pattern)+}++/// Compile a Java-style date pattern into tokens.+fn compile_pattern(pattern: &str) -> Result<Vec<PatTok>, DateResolveError> {+ let chars: Vec<char> = pattern.chars().collect();+ let mut toks: Vec<PatTok> = Vec::new();+ let mut i = 0usize;+ while i < chars.len() {+ let c = chars[i];+ if c == '\'' {+ // Quoted literal ('' = escaped quote).+ let mut lit = String::new();+ i += 1;+ loop {+ if i >= chars.len() {+ break; // Unterminated quote — treat gathered text as literal.+ }+ if chars[i] == '\'' {+ if i + 1 < chars.len() && chars[i + 1] == '\'' {+ lit.push('\'');+ i += 2;+ continue;+ }+ i += 1;+ break;+ }+ lit.push(chars[i]);+ i += 1;+ }+ if lit.is_empty() {+ lit.push('\''); // '' outside text = literal quote+ }+ push_literal(&mut toks, &lit);+ continue;+ }+ if c.is_ascii_alphabetic() {+ if !VALID_JAVA_PATTERN_LETTERS.contains(c) {+ return Err(DateResolveError::UnknownPatternLetter(c));+ }+ let mut run = 1usize;+ while i + run < chars.len() && chars[i + run] == c {+ run += 1;+ }+ i += run;+ let tok = match c {+ 'y' | 'u' | 'Y' => PatTok::Year(run.min(4)),+ 'M' | 'L' => {+ if run >= 3 {+ PatTok::MonthName+ } else {+ PatTok::Month { two_digit: run == 2 }+ }+ }+ 'd' => PatTok::Day { two_digit: run >= 2 },+ 'H' | 'k' => PatTok::Hour { two_digit: run >= 2 },+ 'h' | 'K' => PatTok::Hour { two_digit: run >= 2 },+ 'm' => PatTok::Minute { two_digit: run >= 2 },+ 's' => PatTok::Second { two_digit: run >= 2 },+ 'S' | 'n' | 'A' | 'N' => PatTok::Fraction,+ 'Z' | 'X' | 'x' | 'z' | 'V' | 'O' | 'v' => PatTok::TzOffset,+ 'E' | 'e' | 'c' => PatTok::WeekdayName,+ 'a' | 'B' => PatTok::AmPm,+ // Valid Java letters without an implementation here (era,+ // week-of-year, day-of-year, quarter, …).+ _ => PatTok::Unsupported,+ };+ toks.push(tok);+ continue;+ }+ // Separator / literal character.+ push_literal(&mut toks, &c.to_string());+ i += 1;+ }+ Ok(toks)+}++fn push_literal(toks: &mut Vec<PatTok>, text: &str) {+ if let Some(PatTok::Literal(prev)) = toks.last_mut() {+ prev.push_str(text);+ } else {+ toks.push(PatTok::Literal(text.to_string()));+ }+}++// ─────────────────────────────────────────────────────────────────────────────+// Value parsing+// ─────────────────────────────────────────────────────────────────────────────++/// Parse a value against one compiled format. Returns epoch-ms with the+/// round mode applied to missing components.+fn parse_with_format(fmt: &DateFmt, value: &str, round_up: bool) -> Option<i64> {+ match fmt {+ DateFmt::EpochMillis => {+ let v: i64 = value.parse().ok()?;+ Some(v)+ }+ DateFmt::EpochSecond => {+ let v: i64 = value.parse().ok()?;+ v.checked_mul(1000)+ }+ DateFmt::IsoOptionalTime => parse_iso_partial(value)?.to_epoch_ms(round_up),+ DateFmt::Pattern(toks) => parse_pattern(toks, value)?.to_epoch_ms(round_up),+ }+}++/// Parse a (possibly partial) strict ISO-8601 date:+/// `yyyy[-MM[-dd['T'HH[:mm[:ss[.SSS…]]][tz]]]]` where `tz` is `Z` or+/// `±hh[:mm]` / `±hhmm`. Trailing input → fail.+fn parse_iso_partial(s: &str) -> Option<DateParts> {+ let b = s.as_bytes();+ let mut i = 0usize;+ let mut parts = DateParts::default();++ // Year: optional sign + exactly 4 digits (strict_date_optional_time).+ let neg = if b.first() == Some(&b'-') {+ i += 1;+ true+ } else {+ false+ };+ let year_digits = take_digits(b, &mut i, 4, 4)?;+ let mut year: i32 = year_digits.parse().ok()?;+ if neg {+ year = -year;+ }+ parts.year = Some(year);+ if i == b.len() {+ return Some(parts);+ }++ // -MM+ if b[i] != b'-' {+ return None;+ }+ i += 1;+ parts.month = Some(take_digits(b, &mut i, 2, 2)?.parse().ok()?);+ if i == b.len() {+ return Some(parts);+ }++ // -dd+ if b[i] != b'-' {+ return None;+ }+ i += 1;+ parts.day = Some(take_digits(b, &mut i, 2, 2)?.parse().ok()?);+ if i == b.len() {+ return Some(parts);+ }++ // 'T'HH+ if b[i] != b'T' {+ return None;+ }+ i += 1;+ parts.hour = Some(take_digits(b, &mut i, 2, 2)?.parse().ok()?);++ // [:mm[:ss[.fff…]]]+ if i < b.len() && b[i] == b':' {+ i += 1;+ parts.minute = Some(take_digits(b, &mut i, 2, 2)?.parse().ok()?);+ if i < b.len() && b[i] == b':' {+ i += 1;+ parts.second = Some(take_digits(b, &mut i, 2, 2)?.parse().ok()?);+ if i < b.len() && (b[i] == b'.' || b[i] == b',') {+ i += 1;+ let frac = take_digits(b, &mut i, 1, 9)?;+ parts.milli = Some(frac_to_millis(&frac));+ }+ }+ }++ // Optional timezone.+ if i < b.len() {+ let (tz, used) = parse_tz(&s[i..])?;+ parts.tz_secs = Some(tz);+ i += used;+ }+ if i != b.len() {+ return None;+ }+ Some(parts)+}++/// Parse a value against compiled pattern tokens.+fn parse_pattern(toks: &[PatTok], value: &str) -> Option<DateParts> {+ let b = value.as_bytes();+ let mut i = 0usize;+ let mut parts = DateParts::default();+ let mut pm = false;+ let mut has_ampm = false;++ for (t_idx, tok) in toks.iter().enumerate() {+ match tok {+ PatTok::Year(min_digits) => {+ let neg = if b.get(i) == Some(&b'-') {+ i += 1;+ true+ } else {+ false+ };+ // Adjacent numeric fields (`yyyyMMdd`) parse fixed-width,+ // like java.time: when the next token also consumes digits,+ // the year takes exactly its pattern width.+ let numeric_follows = matches!(+ toks.get(t_idx + 1),+ Some(+ PatTok::Month { .. }+ | PatTok::Day { .. }+ | PatTok::Hour { .. }+ | PatTok::Minute { .. }+ | PatTok::Second { .. }+ | PatTok::Fraction+ )+ );+ let max = if numeric_follows { *min_digits } else { 9 };+ let digits = take_digits(b, &mut i, *min_digits, max)?;+ let mut y: i32 = digits.parse().ok()?;+ if neg {+ y = -y;+ }+ parts.year = Some(y);+ }+ PatTok::Month { two_digit } => {+ let d = take_digits(b, &mut i, if *two_digit { 2 } else { 1 }, 2)?;+ parts.month = Some(d.parse().ok()?);+ }+ PatTok::MonthName => {+ let name = take_alpha(b, &mut i)?;+ parts.month = Some(month_from_name(&name)?);+ }+ PatTok::Day { two_digit } => {+ let d = take_digits(b, &mut i, if *two_digit { 2 } else { 1 }, 2)?;+ parts.day = Some(d.parse().ok()?);+ }+ PatTok::Hour { two_digit } => {+ let d = take_digits(b, &mut i, if *two_digit { 2 } else { 1 }, 2)?;+ parts.hour = Some(d.parse().ok()?);+ }+ PatTok::Minute { two_digit } => {+ let d = take_digits(b, &mut i, if *two_digit { 2 } else { 1 }, 2)?;+ parts.minute = Some(d.parse().ok()?);+ }+ PatTok::Second { two_digit } => {+ let d = take_digits(b, &mut i, if *two_digit { 2 } else { 1 }, 2)?;+ parts.second = Some(d.parse().ok()?);+ }+ PatTok::Fraction => {+ let d = take_digits(b, &mut i, 1, 9)?;+ parts.milli = Some(frac_to_millis(&d));+ }+ PatTok::TzOffset => {+ let (tz, used) = parse_tz(&value[i..])?;+ parts.tz_secs = Some(tz);+ i += used;+ }+ PatTok::WeekdayName => {+ take_alpha(b, &mut i)?;+ }+ PatTok::AmPm => {+ let a = take_alpha(b, &mut i)?;+ has_ampm = true;+ match a.to_ascii_lowercase().as_str() {+ "am" => pm = false,+ "pm" => pm = true,+ _ => return None,+ }+ }+ PatTok::Literal(lit) => {+ let lb = lit.as_bytes();+ if b.len() < i + lb.len() || &b[i..i + lb.len()] != lb {+ return None;+ }+ i += lb.len();+ }+ PatTok::Unsupported => return None,+ }+ }+ if i != b.len() {+ return None; // ES: "unparsed text found at index N"+ }+ if has_ampm && pm {+ if let Some(h) = parts.hour {+ if h < 12 {+ parts.hour = Some(h + 12);+ }+ }+ }+ Some(parts)+}++fn take_digits(b: &[u8], i: &mut usize, min: usize, max: usize) -> Option<String> {+ let start = *i;+ while *i < b.len() && b[*i].is_ascii_digit() && *i - start < max {+ *i += 1;+ }+ if *i - start < min {+ return None;+ }+ Some(String::from_utf8_lossy(&b[start..*i]).into_owned())+}++fn take_alpha(b: &[u8], i: &mut usize) -> Option<String> {+ let start = *i;+ while *i < b.len() && b[*i].is_ascii_alphabetic() {+ *i += 1;+ }+ if *i == start {+ return None;+ }+ Some(String::from_utf8_lossy(&b[start..*i]).into_owned())+}++fn frac_to_millis(digits: &str) -> u32 {+ // ".5" → 500 ms, ".123456789" → 123 ms.+ let padded = format!("{:0<3}", &digits[..digits.len().min(3)]);+ padded.parse().unwrap_or(0)+}++fn month_from_name(name: &str) -> Option<u32> {+ const MONTHS: [&str; 12] = [+ "january",+ "february",+ "march",+ "april",+ "may",+ "june",+ "july",+ "august",+ "september",+ "october",+ "november",+ "december",+ ];+ let lc = name.to_ascii_lowercase();+ MONTHS+ .iter()+ .position(|m| *m == lc || m[..3] == lc)+ .map(|p| p as u32 + 1)+}++/// Parse a timezone suffix; returns (offset seconds, bytes consumed).+fn parse_tz(s: &str) -> Option<(i32, usize)> {+ let b = s.as_bytes();+ if b.is_empty() {+ return None;+ }+ if b[0] == b'Z' {+ return Some((0, 1));+ }+ if s.starts_with("UTC") || s.starts_with("GMT") {+ return Some((0, 3));+ }+ let sign: i32 = match b[0] {+ b'+' => 1,+ b'-' => -1,+ _ => return None,+ };+ let mut i = 1usize;+ let hh = take_digits(b, &mut i, 2, 2)?;+ let hours: i32 = hh.parse().ok()?;+ let mut minutes = 0i32;+ if i < b.len() {+ if b[i] == b':' {+ i += 1;+ minutes = take_digits(b, &mut i, 2, 2)?.parse().ok()?;+ } else if b[i].is_ascii_digit() {+ minutes = take_digits(b, &mut i, 2, 2)?.parse().ok()?;+ }+ }+ Some((sign * (hours * 3600 + minutes * 60), i))+}++// ─────────────────────────────────────────────────────────────────────────────+// Date math+// ─────────────────────────────────────────────────────────────────────────────++/// Apply an ES date-math suffix (`+1M`, `-2w/d`, `/M`, chains thereof) to a+/// base instant. `round_up` picks the rounding direction for `/unit`.+///+/// Errors carry the *whole* math substring, mirroring ES's+/// `operator not supported for date math [-5ms]`.+pub fn apply_date_math(base_ms: i64, math: &str, round_up: bool) -> Result<i64, DateResolveError> {+ let mut dt = ms_to_naive(base_ms).ok_or_else(|| DateResolveError::BadDateMath(math.into()))?;+ let b = math.as_bytes();+ let mut i = 0usize;+ let err = || DateResolveError::BadDateMath(math.to_string());++ while i < b.len() {+ match b[i] {+ b'/' => {+ i += 1;+ if i >= b.len() {+ return Err(err());+ }+ let unit = b[i] as char;+ i += 1;+ dt = round_naive(dt, unit, round_up).ok_or_else(err)?;+ }+ b'+' | b'-' => {+ let sign: i64 = if b[i] == b'+' { 1 } else { -1 };+ i += 1;+ let start = i;+ while i < b.len() && b[i].is_ascii_digit() {+ i += 1;+ }+ let n: i64 = if i == start {+ 1 // `now+y` == `now+1y`+ } else {+ std::str::from_utf8(&b[start..i])+ .ok()+ .and_then(|s| s.parse().ok())+ .ok_or_else(err)?+ };+ if i >= b.len() {+ return Err(err());+ }+ let unit = b[i] as char;+ i += 1;+ // A trailing 's' after 'm' would mean the (unsupported in+ // date math) `ms` unit — the next loop iteration rejects the+ // stray 's' as an operator, matching ES.+ dt = add_unit(dt, sign * n, unit).ok_or_else(err)?;+ }+ _ => return Err(err()),+ }+ }+ Ok(dt.and_utc().timestamp_millis())+}++fn ms_to_naive(ms: i64) -> Option<NaiveDateTime> {+ chrono::DateTime::from_timestamp_millis(ms).map(|dt| dt.naive_utc())+}++fn add_unit(dt: NaiveDateTime, n: i64, unit: char) -> Option<NaiveDateTime> {+ match unit {+ 'y' => shift_months(dt, n.checked_mul(12)?),+ 'M' => shift_months(dt, n),+ 'w' => dt.checked_add_signed(Duration::weeks(n)),+ 'd' => dt.checked_add_signed(Duration::days(n)),+ 'h' | 'H' => dt.checked_add_signed(Duration::hours(n)),+ 'm' => dt.checked_add_signed(Duration::minutes(n)),+ 's' => dt.checked_add_signed(Duration::seconds(n)),+ _ => None,+ }+}++fn shift_months(dt: NaiveDateTime, n: i64) -> Option<NaiveDateTime> {+ let months = u32::try_from(n.unsigned_abs()).ok()?;+ if n >= 0 {+ dt.checked_add_months(Months::new(months))+ } else {+ dt.checked_sub_months(Months::new(months))+ }+}++/// Round an instant to `unit`. Round-down → first millisecond of the unit;+/// round-up → last millisecond (ES range semantics).+fn round_naive(dt: NaiveDateTime, unit: char, round_up: bool) -> Option<NaiveDateTime> {+ let floor: NaiveDateTime = match unit {+ 'y' => NaiveDate::from_ymd_opt(dt.year(), 1, 1)?.and_hms_opt(0, 0, 0)?,+ 'M' => NaiveDate::from_ymd_opt(dt.year(), dt.month(), 1)?.and_hms_opt(0, 0, 0)?,+ 'w' => {+ // ISO weeks start on Monday (java.time / ES Rounding).+ let days_back = dt.weekday().num_days_from_monday() as i64;+ (dt.date() - Duration::days(days_back)).and_hms_opt(0, 0, 0)?+ }+ 'd' => dt.date().and_hms_opt(0, 0, 0)?,+ 'h' | 'H' => dt.date().and_hms_opt(dt.hour(), 0, 0)?,+ 'm' => dt.date().and_hms_opt(dt.hour(), dt.minute(), 0)?,+ 's' => dt.date().and_hms_opt(dt.hour(), dt.minute(), dt.second())?,+ _ => return None,+ };+ if !round_up {+ return Some(floor);+ }+ let next: NaiveDateTime = match unit {+ 'y' => shift_months(floor, 12)?,+ 'M' => shift_months(floor, 1)?,+ 'w' => floor.checked_add_signed(Duration::weeks(1))?,+ 'd' => floor.checked_add_signed(Duration::days(1))?,+ 'h' | 'H' => floor.checked_add_signed(Duration::hours(1))?,+ 'm' => floor.checked_add_signed(Duration::minutes(1))?,+ 's' => floor.checked_add_signed(Duration::seconds(1))?,+ _ => return None,+ };+ next.checked_sub_signed(Duration::milliseconds(1))+}++// ─────────────────────────────────────────────────────────────────────────────+// Bound resolution (public entry)+// ─────────────────────────────────────────────────────────────────────────────++/// Format an epoch-ms instant as the canonical ISO string the engine's+/// comparators parse back via `parse_date_ms`.+fn ms_to_iso(ms: i64) -> Option<String> {+ let dt = chrono::DateTime::from_timestamp_millis(ms)?;+ Some(dt.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string())+}++/// Resolve one range bound string to a canonical ISO instant.+///+/// * `round_up` — true for `lte` / `gt` (ES rounds those bounds up).+/// * `formats` — the compiled explicit `format` list, or `None` for the+/// default (`strict_date_optional_time||epoch_millis`, where the epoch half+/// is left to the engine's numeric comparator).+///+/// Returns:+/// * `Ok(Some(iso))` — the bound resolved as a date; substitute it.+/// * `Ok(None)` — not date-shaped under the *default* format; leave the bound+/// unchanged (keyword / numeric ranges must keep their semantics — the+/// parser has no mapping information).+/// * `Err(…)` — unparseable under an *explicit* format, or malformed date+/// math (both are hard 400s in ES regardless of field type).+pub fn resolve_date_bound_str(+ value: &str,+ round_up: bool,+ formats: Option<&[DateFmt]>,+) -> Result<Option<String>, DateResolveError> {+ let v = value.trim();++ // `now`-anchored math (never subject to `format`).+ if let Some(math) = v.strip_prefix("now") {+ let base = chrono::Utc::now().timestamp_millis();+ let ms = apply_date_math(base, math, round_up)?;+ return Ok(ms_to_iso(ms));+ }++ // `<anchor>||<math>` — the anchor parses with MIN fill (no implicit+ // round-up; verified live against ES 8.13.4), then math applies with+ // per-operator rounding.+ if let Some((anchor, math)) = v.split_once("||") {+ let anchor_ms = parse_anchor_min(anchor, formats);+ let Some(anchor_ms) = anchor_ms else {+ // A `||` value is unambiguously date math — unparseable anchors+ // are hard errors (ES: failed to parse date field), even under+ // the default format.+ return Err(DateResolveError::UnparseableValue(v.to_string()));+ };+ let ms = apply_date_math(anchor_ms, math, round_up)?;+ return Ok(ms_to_iso(ms));+ }++ // Plain value.+ match formats {+ Some(fmts) => {+ for f in fmts {+ if let Some(ms) = parse_with_format(f, v, round_up) {+ return Ok(ms_to_iso(ms));+ }+ }+ Err(DateResolveError::UnparseableValue(v.to_string()))+ }+ None => {+ // Default format: only the ISO half rewrites; anything else+ // (bare numbers, keyword data) keeps its existing comparator+ // semantics.+ match parse_iso_partial(v).and_then(|p| p.to_epoch_ms(round_up)) {+ Some(ms) => Ok(ms_to_iso(ms)),+ None => Ok(None),+ }+ }+ }+}++/// Parse a date-math anchor with min fill under `formats` (or the default).+fn parse_anchor_min(anchor: &str, formats: Option<&[DateFmt]>) -> Option<i64> {+ match formats {+ Some(fmts) => fmts+ .iter()+ .find_map(|f| parse_with_format(f, anchor, false)),+ None => {+ // Default `strict_date_optional_time||epoch_millis`.+ if let Some(p) = parse_iso_partial(anchor) {+ return p.to_epoch_ms(false);+ }+ anchor.parse::<i64>().ok()+ }+ }+}++/// Resolve a numeric range bound under an *explicit* format list.+///+/// ES stringifies numbers when the field/query has a non-epoch date format+/// (`gte: 1500, format: "uuuu"` parses "1500" as a year); epoch formats scale+/// numerically. Returns the same tri-state as [`resolve_date_bound_str`].+pub fn resolve_date_bound_num(+ value: &serde_json::Number,+ round_up: bool,+ formats: &[DateFmt],+) -> Result<Option<String>, DateResolveError> {+ for f in formats {+ match f {+ DateFmt::EpochMillis => {+ if let Some(ms) = value.as_i64() {+ return Ok(ms_to_iso(ms));+ }+ }+ DateFmt::EpochSecond => {+ if let Some(s) = value.as_i64() {+ return Ok(ms_to_iso(s.checked_mul(1000).ok_or_else(|| {+ DateResolveError::UnparseableValue(value.to_string())+ })?));+ }+ }+ other => {+ let s = value.to_string();+ if let Some(ms) = parse_with_format(other, &s, round_up) {+ return Ok(ms_to_iso(ms));+ }+ }+ }+ }+ Err(DateResolveError::UnparseableValue(value.to_string()))+}++// ─────────────────────────────────────────────────────────────────────────────+// Tests+// ─────────────────────────────────────────────────────────────────────────────++#[cfg(test)]+mod tests {+ use super::*;++ fn iso(v: &str, up: bool) -> String {+ resolve_date_bound_str(v, up, None).unwrap().unwrap()+ }++ #[test]+ fn partial_month_fills() {+ // gte/lt (round down): first ms of the parsed fields, min fill.+ assert_eq!(iso("2026-02", false), "2026-02-01T00:00:00.000Z");+ // lte/gt (round up): ES round-up parser fills day → 1, time → max.+ assert_eq!(iso("2026-02", true), "2026-02-01T23:59:59.999Z");+ }++ #[test]+ fn partial_year_fills() {+ assert_eq!(iso("2026", false), "2026-01-01T00:00:00.000Z");+ assert_eq!(iso("2026", true), "2026-01-01T23:59:59.999Z");+ }++ #[test]+ fn day_partial_fills() {+ assert_eq!(iso("2026-02-15", false), "2026-02-15T00:00:00.000Z");+ assert_eq!(iso("2026-02-15", true), "2026-02-15T23:59:59.999Z");+ }++ #[test]+ fn hour_minute_partials() {+ assert_eq!(iso("2026-02-15T08", true), "2026-02-15T08:59:59.999Z");+ assert_eq!(iso("2026-02-15T08:30", true), "2026-02-15T08:30:59.999Z");+ assert_eq!(iso("2026-02-15T08:30:05", true), "2026-02-15T08:30:05.999Z");+ assert_eq!(+ iso("2026-02-15T08:30:05.123", true),+ "2026-02-15T08:30:05.123Z"+ );+ }++ #[test]+ fn tz_offsets() {+ assert_eq!(+ iso("2026-02-15T08:30:00+02:00", false),+ "2026-02-15T06:30:00.000Z"+ );+ assert_eq!(+ iso("2026-02-15T08:30:00Z", false),+ "2026-02-15T08:30:00.000Z"+ );+ assert_eq!(+ iso("2026-02-15T08:30:00-0330", false),+ "2026-02-15T12:00:00.000Z"+ );+ }++ #[test]+ fn anchored_math_min_fill_then_round() {+ // Anchor gets min fill even for round-up bounds (live-verified).+ assert_eq!(iso("2026-02-14||+1d", true), "2026-02-15T00:00:00.000Z");+ // /M rounding is true month-end for round-up …+ assert_eq!(iso("2026-02-15||/M", true), "2026-02-28T23:59:59.999Z");+ // … and month-start for round-down.+ assert_eq!(iso("2026-02-15||/M", false), "2026-02-01T00:00:00.000Z");+ // +1M/d chains.+ assert_eq!(iso("2026-01-01||+1M/d", true), "2026-02-01T23:59:59.999Z");+ assert_eq!(iso("2026-01-01||+1M/d", false), "2026-02-01T00:00:00.000Z");+ // Month addition clamps at end-of-month like java.time.+ assert_eq!(iso("2026-01-31||+1M", false), "2026-02-28T00:00:00.000Z");+ }++ #[test]+ fn week_rounding_is_monday_based() {+ // 2026-02-15 is a Sunday; ISO week = 02-09 (Mon) .. 02-15.+ assert_eq!(iso("2026-02-15||/w", false), "2026-02-09T00:00:00.000Z");+ assert_eq!(iso("2026-02-15||/w", true), "2026-02-15T23:59:59.999Z");+ }++ #[test]+ fn bad_math_reports_whole_suffix() {+ let e = resolve_date_bound_str("now-5ms", false, None).unwrap_err();+ assert_eq!(e, DateResolveError::BadDateMath("-5ms".into()));+ let e = resolve_date_bound_str("2026-01-01||banana", false, None).unwrap_err();+ assert_eq!(e, DateResolveError::BadDateMath("banana".into()));+ }++ #[test]+ fn bad_anchor_is_hard_error() {+ let e = resolve_date_bound_str("abc||+1d", false, None).unwrap_err();+ assert_eq!(e, DateResolveError::UnparseableValue("abc||+1d".into()));+ }++ #[test]+ fn default_format_leaves_non_dates_alone() {+ assert_eq!(resolve_date_bound_str("apple", false, None), Ok(None));+ assert_eq!(resolve_date_bound_str("123", false, None), Ok(None));+ assert_eq!(+ resolve_date_bound_str("1770000000000", false, None),+ Ok(None)+ );+ // 2-digit / 5-digit numbers are not strict 4-digit years.+ assert_eq!(resolve_date_bound_str("20261", false, None), Ok(None));+ }++ #[test]+ fn explicit_format_parses_and_errors() {+ let fmts = compile_formats("dd/MM/yyyy").unwrap();+ assert_eq!(+ resolve_date_bound_str("15/02/2026", true, Some(&fmts)).unwrap(),+ Some("2026-02-15T23:59:59.999Z".into())+ );+ let e = resolve_date_bound_str("2026-02-03", false, Some(&fmts)).unwrap_err();+ assert_eq!(e, DateResolveError::UnparseableValue("2026-02-03".into()));+ }++ #[test]+ fn format_year_numbers() {+ let fmts = compile_formats("uuuu").unwrap();+ let n = serde_json::Number::from(1500);+ assert_eq!(+ resolve_date_bound_num(&n, false, &fmts).unwrap(),+ Some("1500-01-01T00:00:00.000Z".into())+ );+ assert_eq!(+ resolve_date_bound_num(&n, true, &fmts).unwrap(),+ Some("1500-01-01T23:59:59.999Z".into())+ );+ }++ #[test]+ fn invalid_format_letter() {+ assert_eq!(+ compile_formats("banana").unwrap_err(),+ DateResolveError::UnknownPatternLetter('b')+ );+ // First invalid letter of a || list wins.+ assert_eq!(+ compile_formats("yyyy||bogus").unwrap_err(),+ DateResolveError::UnknownPatternLetter('b')+ );+ }++ #[test]+ fn epoch_formats() {+ let fmts = compile_formats("epoch_millis").unwrap();+ assert_eq!(+ resolve_date_bound_str("1770000000000", false, Some(&fmts)).unwrap(),+ Some("2026-02-02T02:40:00.000Z".into())+ );+ let fmts = compile_formats("epoch_second").unwrap();+ assert_eq!(+ resolve_date_bound_str("1770000000", false, Some(&fmts)).unwrap(),+ Some("2026-02-02T02:40:00.000Z".into())+ );+ }++ #[test]+ fn now_math_rounds() {+ // now/d for a round-up bound ends today (last ms).+ let s = iso("now/d", true);+ assert!(s.ends_with("T23:59:59.999Z"), "{s}");+ let s = iso("now/d", false);+ assert!(s.ends_with("T00:00:00.000Z"), "{s}");+ }++ #[test]+ fn basic_date_named_format() {+ let fmts = compile_formats("basic_date").unwrap();+ assert_eq!(+ resolve_date_bound_str("20260215", true, Some(&fmts)).unwrap(),+ Some("2026-02-15T23:59:59.999Z".into())+ );+ }++ #[test]+ fn strict_date_rejects_partials() {+ let fmts = compile_formats("strict_date").unwrap();+ assert!(resolve_date_bound_str("2026-02", false, Some(&fmts)).is_err());+ }++ #[test]+ fn iso_rejects_trailing_garbage() {+ assert_eq!(resolve_date_bound_str("2026-02-15X", false, None), Ok(None));+ assert_eq!(resolve_date_bound_str("2026-2-15", false, None), Ok(None));+ }+}diff --git a/engine/crates/xerj-query/src/lib.rs b/engine/crates/xerj-query/src/lib.rsindex daf98790f..5e6084f10 100644--- a/engine/crates/xerj-query/src/lib.rs+++ b/engine/crates/xerj-query/src/lib.rs@@ -49,6 +49,7 @@//! | AI-native | `knn`, `semantic`, `hybrid` |pub mod ast;+pub mod dates;pub mod error;pub mod executor;pub mod parser;diff --git a/engine/crates/xerj-query/src/parser.rs b/engine/crates/xerj-query/src/parser.rsindex e73e4d2b5..57ce2290e 100644--- a/engine/crates/xerj-query/src/parser.rs+++ b/engine/crates/xerj-query/src/parser.rs@@ -23,6 +23,7 @@ use crate::ast::{MinShouldMatch, Modifier, MultiMatchType, QueryNode, RandomScore, ScoreFunction, ScoreMode,SearchRequest, SourceFilter, TrackTotalHits, WeightedQuery,};+use crate::dates;use crate::error::{ParseError, QueryError, Result};use crate::sort::{SortField, SortMissing, SortMode, SortOrder};@@ -796,46 +797,64 @@ fn parse_terms(params: &Value) -> Result<QueryNode> {})}-/// Resolve an ES date-math expression like `now`, `now-24h`, `now+1d/d`-/// to a concrete ISO-8601 timestamp string usable by the executor's-/// JSON-scan range comparator. Returns `None` when the input is not-/// a recognised `now…` form (caller leaves the value untouched in-/// that case so non-date string bounds round-trip unchanged).-fn resolve_now_expr(expr: &str) -> Option<String> {- let rest = expr.strip_prefix("now")?;- let base = chrono::Utc::now();- let mut delta_secs: i64 = 0;- let (sign, rest) = if let Some(r) = rest.strip_prefix('+') {- (1i64, r)- } else if let Some(r) = rest.strip_prefix('-') {- (-1i64, r)- } else if rest.is_empty() || rest.starts_with('/') {- (0, rest)- } else {- return None;+/// The format string ES reports in `failed to parse date field` errors when+/// no explicit `format` was given.+const DEFAULT_DATE_FORMAT: &str = "strict_date_optional_time||epoch_millis";++/// Map a [`dates::DateResolveError`] to the exact ES error message. `fmt` is+/// the explicit `format` parameter, if any.+fn date_resolve_err(e: dates::DateResolveError, fmt: Option<&str>, value: &str) -> QueryError {+ use dates::DateResolveError::*;+ let msg = match e {+ UnknownPatternLetter(c) => format!(+ "Invalid format: [{}]: Unknown pattern letter: {c}",+ fmt.unwrap_or_default()+ ),+ UnparseableValue(v) => {+ let f = fmt.unwrap_or(DEFAULT_DATE_FORMAT);+ format!(+ "failed to parse date field [{v}] with format [{f}]: \+ [failed to parse date field [{v}] with format [{f}]]"+ )+ }+ BadDateMath(m) => {+ let _ = value; // value context already inside the math substring+ format!("operator not supported for date math [{m}]")+ }};- if sign != 0 {- let delta_end = rest.find('/').unwrap_or(rest.len());- let delta_expr = &rest[..delta_end];- let (num_part, unit_part): (String, String) = delta_expr- .chars()- .partition(|c| c.is_ascii_digit() || *c == '.');- let n: f64 = num_part.parse().ok()?;- let secs = match unit_part.as_str() {- "ms" => n / 1000.0,- "s" => n,- "m" => n * 60.0,- "h" => n * 3600.0,- "d" => n * 86_400.0,- "w" => n * 7.0 * 86_400.0,- "M" => n * 30.0 * 86_400.0,- "y" => n * 365.0 * 86_400.0,- _ => return None,- };- delta_secs = (sign as f64 * secs) as i64;+ QueryError::Parse(ParseError::Invalid(msg))+}++/// Resolve one range bound per ES date semantics (rounding, `format`,+/// date math). `round_up` is true for `lte`/`gt` bounds. Non-date-shaped+/// values under the default format pass through unchanged so keyword and+/// numeric ranges keep their comparator semantics.+fn resolve_range_bound(+ v: Value,+ round_up: bool,+ formats: Option<&[dates::DateFmt]>,+ fmt_raw: Option<&str>,+) -> Result<Value> {+ match &v {+ Value::String(s) => match dates::resolve_date_bound_str(s, round_up, formats) {+ Ok(Some(iso)) => Ok(Value::String(iso)),+ Ok(None) => Ok(v),+ Err(e) => Err(date_resolve_err(e, fmt_raw, s)),+ },+ Value::Number(n) => match formats {+ // ES stringifies numbers under an explicit date format+ // (`gte: 1500, format: "uuuu"` parses "1500" as a year).+ Some(fmts) => match dates::resolve_date_bound_num(n, round_up, fmts) {+ Ok(Some(iso)) => Ok(Value::String(iso)),+ Ok(None) => Ok(v),+ Err(e) => Err(date_resolve_err(e, fmt_raw, &n.to_string())),+ },+ // Default format: numbers are epoch-millis instants — the+ // engine's numeric comparator already handles them exactly.+ None => Ok(v),+ },+ _ => Ok(v),}- let dt = base + chrono::Duration::seconds(delta_secs);- Some(dt.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string())}fn parse_range(params: &Value) -> Result<QueryNode> {@@ -862,64 +881,34 @@ fn parse_range(params: &Value) -> Result<QueryNode> {.and_then(|v| v.as_f64()).map(|b| b as f32);- // ES date math: `gte: "now-24h"` etc. resolve at query-time against- // wall clock. Without this, downstream `doc_matches_query` would- // compare `"now-24h"` as a literal string against doc values and- // fail to match anything for date fields.- let resolve_now = |v: Value| -> Value {- match &v {- Value::String(s) if s.trim_start().starts_with("now") => {- if let Some(iso) = resolve_now_expr(s.trim()) {- Value::String(iso)- } else {- v- }- }- _ => v,+ // ES date bound resolution — date math (`now-24h`, `2026-01-01||+1M/d`),+ // partial-date round-up/round-down fills (`lte: "2026-02"` covers through+ // 2026-02-01T23:59:59.999), and the `format` parameter (`uuuu`,+ // `dd/MM/yyyy`, `epoch_millis`, …). Bounds resolve to canonical ISO+ // instants here so every downstream comparator (JSON-scan, doc-values+ // prefilter, memtable) sees the same concrete value. An invalid+ // `format` or a value that fails an explicit format is a hard 400,+ // matching live ES 8.13.4.+ let fmt_raw = inner.get("format").and_then(|v| v.as_str());+ let formats = match fmt_raw {+ Some(f) => {+ Some(dates::compile_formats(f).map_err(|e| date_resolve_err(e, fmt_raw, ""))?)}+ None => None,};+ // Round-up bounds: `lte` includes its whole covered interval, `gt`+ // excludes its whole covered interval. `gte`/`lt` round down.if let Some(v) = gte.take() {- gte = Some(resolve_now(v));+ gte = Some(resolve_range_bound(v, false, formats.as_deref(), fmt_raw)?);}if let Some(v) = gt.take() {- gt = Some(resolve_now(v));+ gt = Some(resolve_range_bound(v, true, formats.as_deref(), fmt_raw)?);}if let Some(v) = lte.take() {- lte = Some(resolve_now(v));+ lte = Some(resolve_range_bound(v, true, formats.as_deref(), fmt_raw)?);}if let Some(v) = lt.take() {- lt = Some(resolve_now(v));- }-- // `format: uuuu` (year-only): ES resolves the bound as year-first-day- // (YEAR-01-01T00:00:00) and applies range rounding at day granularity —- // gte floors to 00:00:00, lte ceils to 23:59:59.999 of the same first- // day. So `gte:1500 lte:1500` matches only docs on 1500-01-01, not the- // whole year.- if let Some(fmt) = inner.get("format").and_then(|v| v.as_str()) {- let rewrite = |b: Value, upper: bool| -> Value {- let year: Option<i64> = match &b {- Value::Number(n) => n.as_i64(),- Value::String(s) => s.trim().parse::<i64>().ok(),- _ => None,- };- let Some(y) = year else { return b };- if !matches!(fmt, "uuuu" | "yyyy" | "year") {- return b;- }- let iso = if upper {- format!("{:04}-01-01T23:59:59.999Z", y)- } else {- format!("{:04}-01-01T00:00:00.000Z", y)- };- Value::String(iso)- };- if let Some(v) = gte.take() {- gte = Some(rewrite(v, false));- }- if let Some(v) = lte.take() {- lte = Some(rewrite(v, true));- }+ lt = Some(resolve_range_bound(v, false, formats.as_deref(), fmt_raw)?);}if gte.is_none() && gt.is_none() && lte.is_none() && lt.is_none() {@@ -1174,9 +1163,11 @@ enum QsTok {}/// Parse a range bound value from a Lucene query_string range expression.-/// Numbers become JSON numbers (so the Range matcher compares numerically),-/// `now`-style date math resolves to an ISO timestamp, and everything else-/// stays a string. Surrounding quotes are stripped; `*` / empty → open end.+/// Numbers become JSON numbers (so the Range matcher compares numerically)+/// and everything else stays a string — date math / partial-date resolution+/// happens in the callers (`qs_parse_bracket_range` / `qs_parse_cmp_range`),+/// which know whether the bound rounds up or down. Surrounding quotes are+/// stripped; `*` / empty → open end.fn qs_range_bound(raw: &str) -> Option<Value> {let mut t = raw.trim();if t.len() >= 2 && t.starts_with('"') && t.ends_with('"') {@@ -1193,14 +1184,18 @@ fn qs_range_bound(raw: &str) -> Option<Value> {return Some(Value::from(f));}}- if t.starts_with("now") {- if let Some(iso) = resolve_now_expr(t) {- return Some(Value::String(iso));- }- }Some(Value::String(t.to_string()))}+/// Apply ES date semantics (rounding + date math, default format) to one+/// query_string range bound.+fn qs_resolve_bound(v: Option<Value>, round_up: bool) -> Result<Option<Value>> {+ match v {+ Some(b) => Ok(Some(resolve_range_bound(b, round_up, None, None)?)),+ None => Ok(None),+ }+}+/// Parse a bracket range (`[A TO B]` / `{A TO B}` / mixed) starting at byte/// offset `open` in `q` (pointing at the opening bracket). Returns the token/// and the byte offset just past the closing bracket.@@ -1234,15 +1229,17 @@ fn qs_parse_bracket_range(q: &str, field: &str, open: usize) -> Result<(QsTok, u));}let (mut gt, mut gte, mut lt, mut lte) = (None, None, None, None);+ // ES rounding: exclusive lower (`gt`) and inclusive upper (`lte`) round+ // up; the other two round down.if inclusive_lo {- gte = lo;+ gte = qs_resolve_bound(lo, false)?;} else {- gt = lo;+ gt = qs_resolve_bound(lo, true)?;}if inclusive_hi {- lte = hi;+ lte = qs_resolve_bound(hi, true)?;} else {- lt = hi;+ lt = qs_resolve_bound(hi, false)?;}Ok((QsTok::Range {@@ -1287,11 +1284,12 @@ fn qs_parse_cmp_range(q: &str, field: &str, rest: &str, mut i: usize) -> Result<))})?;let (mut gt, mut gte, mut lt, mut lte) = (None, None, None, None);+ // ES rounding: `>` and `<=` round the bound up, `>=` and `<` round down.match op {- ">" => gt = Some(bound),- ">=" => gte = Some(bound),- "<" => lt = Some(bound),- _ => lte = Some(bound),+ ">" => gt = Some(resolve_range_bound(bound, true, None, None)?),+ ">=" => gte = Some(resolve_range_bound(bound, false, None, None)?),+ "<" => lt = Some(resolve_range_bound(bound, false, None, None)?),+ _ => lte = Some(resolve_range_bound(bound, true, None, None)?),}Ok((QsTok::Range {@@ -4478,13 +4476,16 @@ mod tests {#[test]fn test_query_string_range_date() {+ // Date bounds resolve with ES rounding semantics: `>` (exclusive+ // lower) rounds UP to the last ms of the covered day, `>=`/`<=`+ // round down/up respectively — live-verified vs ES 8.13.4.let (field, gt, ..) = expect_range(qs("ts:>2020-01-01"));assert_eq!(field, "ts");- assert_eq!(gt, Some(json!("2020-01-01")));+ assert_eq!(gt, Some(json!("2020-01-01T23:59:59.999Z")));let (field, _, gte, _, lte) = expect_range(qs("ts:[2020-01-01 TO 2020-12-31]"));assert_eq!(field, "ts");- assert_eq!(gte, Some(json!("2020-01-01")));- assert_eq!(lte, Some(json!("2020-12-31")));+ assert_eq!(gte, Some(json!("2020-01-01T00:00:00.000Z")));+ assert_eq!(lte, Some(json!("2020-12-31T23:59:59.999Z")));}#[test]