Skip to content

Commit 29734d4

Browse files
use correct precision level for fastfield-based termquery on datetime (#6027)
1 parent 29422a9 commit 29734d4

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

quickwit/quickwit-query/src/query_ast/full_text_query.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl FullTextQuery {
301301

302302
#[cfg(test)]
303303
mod tests {
304-
use tantivy::schema::{Schema, TEXT};
304+
use tantivy::schema::{DateOptions, DateTimePrecision, Schema, TEXT};
305305

306306
use crate::BooleanOperand;
307307
use crate::query_ast::tantivy_query_ast::TantivyQueryAst;
@@ -379,6 +379,59 @@ mod tests {
379379
);
380380
}
381381

382+
#[test]
383+
fn test_full_text_datetime() {
384+
let full_text_query = FullTextQuery {
385+
field: "ts".to_string(),
386+
text: "2025-12-13T16:13:12.666777Z".to_string(),
387+
params: super::FullTextParams {
388+
tokenizer: Some("raw".to_string()),
389+
mode: FullTextMode::Phrase { slop: 1 },
390+
zero_terms_query: crate::MatchAllOrNone::MatchAll,
391+
},
392+
lenient: false,
393+
};
394+
{
395+
// indexed, we truncate to the second
396+
let mut schema_builder = Schema::builder();
397+
schema_builder.add_date_field(
398+
"ts",
399+
DateOptions::default()
400+
.set_precision(DateTimePrecision::Milliseconds)
401+
.set_fast()
402+
.set_indexed(),
403+
);
404+
let schema = schema_builder.build();
405+
let ast: TantivyQueryAst = full_text_query
406+
.build_tantivy_ast_call(&BuildTantivyAstContext::for_test(&schema))
407+
.unwrap();
408+
let leaf = ast.as_leaf().unwrap();
409+
assert_eq!(
410+
&format!("{leaf:?}"),
411+
r#"TermQuery(Term(field=0, type=Date, 2025-12-13T16:13:12Z))"#
412+
);
413+
}
414+
{
415+
// not indexed, we truncate to fastfield precision
416+
let mut schema_builder = Schema::builder();
417+
schema_builder.add_date_field(
418+
"ts",
419+
DateOptions::default()
420+
.set_precision(DateTimePrecision::Milliseconds)
421+
.set_fast(),
422+
);
423+
let schema = schema_builder.build();
424+
let ast: TantivyQueryAst = full_text_query
425+
.build_tantivy_ast_call(&BuildTantivyAstContext::for_test(&schema))
426+
.unwrap();
427+
let leaf = ast.as_leaf().unwrap();
428+
assert_eq!(
429+
&format!("{leaf:?}"),
430+
r#"TermQuery(Term(field=0, type=Date, 2025-12-13T16:13:12.666Z))"#
431+
);
432+
}
433+
}
434+
382435
#[test]
383436
fn test_full_text_bool_mode() {
384437
let full_text_query = FullTextQuery {

quickwit/quickwit-query/src/query_ast/utils.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,13 @@ fn compute_query_with_field(
141141
let term = Term::from_field_bool(field, bool_val);
142142
Ok(make_term_query(term))
143143
}
144-
FieldType::Date(_) => {
144+
FieldType::Date(date_options) => {
145145
let dt = parse_value_from_user_text(value, field_entry.name())?;
146-
let term = Term::from_field_date_for_search(field, dt);
146+
let term = if date_options.is_indexed() {
147+
Term::from_field_date_for_search(field, dt)
148+
} else {
149+
Term::from_field_date(field, dt.truncate(date_options.get_precision()))
150+
};
147151
Ok(make_term_query(term))
148152
}
149153
FieldType::Str(text_options) => {

0 commit comments

Comments
 (0)