Skip to content

Commit

Permalink
feat(es): respect format parameter in range query
Browse files Browse the repository at this point in the history
  • Loading branch information
etolbakov authored Jul 9, 2024
1 parent c84e642 commit a415f30
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions quickwit/quickwit-query/src/elastic_query_dsl/range_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::ops::Bound;
use std::str::FromStr;

use serde::Deserialize;
use quickwit_datetime::{DateTimeInputFormat, parse_date_time_str, StrptimeParser};

use crate::elastic_query_dsl::one_field_map::OneFieldMap;
use crate::elastic_query_dsl::ConvertableToQueryAst;
Expand All @@ -40,7 +42,6 @@ pub struct RangeQueryParams {
lte: Option<JsonLiteral>,
#[serde(default)]
boost: Option<NotNaNf32>,
// Currently NO-OP (see #5109)
#[serde(default)]
format: Option<JsonLiteral>,
}
Expand All @@ -56,8 +57,20 @@ impl ConvertableToQueryAst for RangeQuery {
lt,
lte,
boost,
format: _,
format,
} = self.value;
let (gt, gte, lt, lte) =
if let Some(JsonLiteral::String(fmt)) = format {
(
gt.map(|v| parse_and_convert(v, &fmt)).transpose()?,
gte.map(|v| parse_and_convert(v, &fmt)).transpose()?,
lt.map(|v| parse_and_convert(v, &fmt)).transpose()?,
lte.map(|v| parse_and_convert(v, &fmt)).transpose()?,
)
} else {
(gt, gte, lt, lte)
};

let range_query_ast = crate::query_ast::RangeQuery {
field,
lower_bound: match (gt, gte) {
Expand All @@ -81,3 +94,15 @@ impl ConvertableToQueryAst for RangeQuery {
Ok(ast.boost(boost))
}
}


fn parse_and_convert(value: JsonLiteral, fmt: &str) -> anyhow::Result<JsonLiteral> {
if let JsonLiteral::String(s) = value {
let date_format = DateTimeInputFormat::from_str(fmt).unwrap();
let date_time = parse_date_time_str(&s, &[date_format]).unwrap();
Ok(JsonLiteral::String(date_time.to_string())) // TODO no `to_string` method
} else {
dbg!(value.clone());
Ok(value)
}
}

0 comments on commit a415f30

Please sign in to comment.