1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Date parameter handler for Elasticsearch.
use serde_json::{Value, json};
use crate::types::SearchPrefix;
/// Builds an ES query clause for a date search parameter.
pub fn build_clause(name: &str, value: &str, prefix: SearchPrefix) -> Option<Value> {
let range_condition = match prefix {
SearchPrefix::Eq => {
// Equality accounting for precision:
// "2024-01-15" matches the full day
let (lower, upper) = date_precision_range(value);
json!({
"range": {
"search_params.date.value": {
"gte": lower,
"lt": upper
}
}
})
}
SearchPrefix::Ne => {
let (lower, upper) = date_precision_range(value);
return Some(json!({
"nested": {
"path": "search_params.date",
"query": {
"bool": {
"must": [
{ "term": { "search_params.date.name": name } }
],
"must_not": [
{
"range": {
"search_params.date.value": {
"gte": lower,
"lt": upper
}
}
}
]
}
}
}
}));
}
SearchPrefix::Gt | SearchPrefix::Sa => {
let (_, upper) = date_precision_range(value);
json!({
"range": {
"search_params.date.value": {
"gte": upper
}
}
})
}
SearchPrefix::Lt | SearchPrefix::Eb => {
let (lower, _) = date_precision_range(value);
json!({
"range": {
"search_params.date.value": {
"lt": lower
}
}
})
}
SearchPrefix::Ge => {
let (lower, _) = date_precision_range(value);
json!({
"range": {
"search_params.date.value": {
"gte": lower
}
}
})
}
SearchPrefix::Le => {
let (_, upper) = date_precision_range(value);
json!({
"range": {
"search_params.date.value": {
"lt": upper
}
}
})
}
SearchPrefix::Ap => {
// Approximately: ±10% of the precision range
let (lower, upper) = date_precision_range(value);
// For approximate, we use the range itself (ES handles fuzzy matching)
json!({
"range": {
"search_params.date.value": {
"gte": lower,
"lt": upper
}
}
})
}
};
Some(json!({
"nested": {
"path": "search_params.date",
"query": {
"bool": {
"must": [
{ "term": { "search_params.date.name": name } },
range_condition
]
}
}
}
}))
}
/// Computes the precision-based range for a date value.
///
/// Returns (lower_bound_inclusive, upper_bound_exclusive).
fn date_precision_range(value: &str) -> (String, String) {
// Count characters to determine precision
let clean = value.trim();
if clean.len() == 4 {
// Year precision: "2024" -> ["2024-01-01", "2025-01-01")
let year: i32 = clean.parse().unwrap_or(2000);
(
format!("{:04}-01-01", year),
format!("{:04}-01-01", year + 1),
)
} else if clean.len() == 7 {
// Month precision: "2024-01" -> ["2024-01-01", "2024-02-01")
let parts: Vec<&str> = clean.split('-').collect();
let year: i32 = parts.first().and_then(|p| p.parse().ok()).unwrap_or(2000);
let month: u32 = parts.get(1).and_then(|p| p.parse().ok()).unwrap_or(1);
let (next_year, next_month) = if month >= 12 {
(year + 1, 1)
} else {
(year, month + 1)
};
(
format!("{:04}-{:02}-01", year, month),
format!("{:04}-{:02}-01", next_year, next_month),
)
} else if clean.len() == 10 {
// Day precision: "2024-01-15" -> ["2024-01-15", "2024-01-16")
// Simple: parse and add one day
let lower = clean.to_string();
let parts: Vec<&str> = clean.split('-').collect();
let year: i32 = parts.first().and_then(|p| p.parse().ok()).unwrap_or(2000);
let month: u32 = parts.get(1).and_then(|p| p.parse().ok()).unwrap_or(1);
let day: u32 = parts.get(2).and_then(|p| p.parse().ok()).unwrap_or(1);
// Use chrono for correct date arithmetic
if let Some(date) = chrono::NaiveDate::from_ymd_opt(year, month, day) {
let next = date + chrono::Duration::days(1);
(lower, next.format("%Y-%m-%d").to_string())
} else {
(lower.clone(), lower)
}
} else {
// Full date-time precision: use the value directly
(clean.to_string(), clean.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_year_precision() {
let (lower, upper) = date_precision_range("2024");
assert_eq!(lower, "2024-01-01");
assert_eq!(upper, "2025-01-01");
}
#[test]
fn test_month_precision() {
let (lower, upper) = date_precision_range("2024-01");
assert_eq!(lower, "2024-01-01");
assert_eq!(upper, "2024-02-01");
}
#[test]
fn test_day_precision() {
let (lower, upper) = date_precision_range("2024-01-15");
assert_eq!(lower, "2024-01-15");
assert_eq!(upper, "2024-01-16");
}
#[test]
fn test_eq_range() {
let clause = build_clause("birthdate", "2024-01-15", SearchPrefix::Eq).unwrap();
let s = serde_json::to_string(&clause).unwrap();
assert!(s.contains("gte"));
assert!(s.contains("2024-01-15"));
assert!(s.contains("2024-01-16"));
}
#[test]
fn test_gt_range() {
let clause = build_clause("birthdate", "2024-01-15", SearchPrefix::Gt).unwrap();
let s = serde_json::to_string(&clause).unwrap();
assert!(s.contains("gte"));
assert!(s.contains("2024-01-16")); // starts after precision range
}
}