From 13f87c83c10a2c83c076068d97cb3a8cd8758cc2 Mon Sep 17 00:00:00 2001 From: Remi Dettai Date: Mon, 9 Dec 2024 16:06:22 +0100 Subject: [PATCH] Fix add query building unit tests --- .../quickwit-doc-mapper/src/query_builder.rs | 59 +++++++++++++++++-- .../src/query_ast/phrase_prefix_query.rs | 2 +- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/quickwit/quickwit-doc-mapper/src/query_builder.rs b/quickwit/quickwit-doc-mapper/src/query_builder.rs index 088c447d61b..9dffeef0ad7 100644 --- a/quickwit/quickwit-doc-mapper/src/query_builder.rs +++ b/quickwit/quickwit-doc-mapper/src/query_builder.rs @@ -287,8 +287,11 @@ mod test { use quickwit_query::query_ast::{ query_ast_from_user_text, FullTextMode, FullTextParams, PhrasePrefixQuery, QueryAstVisitor, + UserInputQuery, + }; + use quickwit_query::{ + create_default_quickwit_tokenizer_manager, BooleanOperand, MatchAllOrNone, }; - use quickwit_query::{create_default_quickwit_tokenizer_manager, MatchAllOrNone}; use tantivy::schema::{DateOptions, DateTimePrecision, Schema, FAST, INDEXED, STORED, TEXT}; use tantivy::Term; @@ -330,7 +333,7 @@ mod test { search_fields: Vec, expected: TestExpectation, ) { - check_build_query(user_query, search_fields, expected, true); + check_build_query(user_query, search_fields, expected, true, false); } #[track_caller] @@ -339,15 +342,31 @@ mod test { search_fields: Vec, expected: TestExpectation, ) { - check_build_query(user_query, search_fields, expected, false); + check_build_query(user_query, search_fields, expected, false, false); + } + + #[track_caller] + fn check_build_query_static_lenient_mode( + user_query: &str, + search_fields: Vec, + expected: TestExpectation, + ) { + check_build_query(user_query, search_fields, expected, false, true); } fn test_build_query( user_query: &str, search_fields: Vec, dynamic_mode: bool, + lenient: bool, ) -> Result { - let query_ast = query_ast_from_user_text(user_query, Some(search_fields)) + let user_input_query = UserInputQuery { + user_text: user_query.to_string(), + default_fields: Some(search_fields), + default_operator: BooleanOperand::And, + lenient, + }; + let query_ast = user_input_query .parse_user_query(&[]) .map_err(|err| err.to_string())?; let schema = make_schema(dynamic_mode); @@ -369,8 +388,9 @@ mod test { search_fields: Vec, expected: TestExpectation, dynamic_mode: bool, + lenient: bool, ) { - let query_result = test_build_query(user_query, search_fields, dynamic_mode); + let query_result = test_build_query(user_query, search_fields, dynamic_mode, lenient); match (query_result, expected) { (Err(query_err_msg), TestExpectation::Err(sub_str)) => { assert!( @@ -432,6 +452,11 @@ mod test { Vec::new(), TestExpectation::Err("invalid query: field does not exist: `foo`"), ); + check_build_query_static_lenient_mode( + "foo:bar", + Vec::new(), + TestExpectation::Ok("EmptyQuery"), + ); check_build_query_static_mode( "title:bar", Vec::new(), @@ -442,6 +467,11 @@ mod test { vec!["fieldnotinschema".to_string()], TestExpectation::Err("invalid query: field does not exist: `fieldnotinschema`"), ); + check_build_query_static_lenient_mode( + "bar", + vec!["fieldnotinschema".to_string()], + TestExpectation::Ok("EmptyQuery"), + ); check_build_query_static_mode( "title:[a TO b]", Vec::new(), @@ -510,6 +540,25 @@ mod test { ); } + #[test] + fn test_wildcard_query() { + check_build_query_static_mode( + "title:hello*", + Vec::new(), + TestExpectation::Ok("PhrasePrefixQuery"), + ); + check_build_query_static_mode( + "foo:bar*", + Vec::new(), + TestExpectation::Err("invalid query: field does not exist: `foo`"), + ); + check_build_query_static_mode( + "title:hello*yo", + Vec::new(), + TestExpectation::Err("Wildcard query contains wildcard in non final position"), + ); + } + #[test] fn test_datetime_range_query() { { diff --git a/quickwit/quickwit-query/src/query_ast/phrase_prefix_query.rs b/quickwit/quickwit-query/src/query_ast/phrase_prefix_query.rs index 9d5a16311c2..1675b22d760 100644 --- a/quickwit/quickwit-query/src/query_ast/phrase_prefix_query.rs +++ b/quickwit/quickwit-query/src/query_ast/phrase_prefix_query.rs @@ -122,7 +122,7 @@ impl BuildTantivyAst for PhrasePrefixQuery { let (_, terms) = match self.get_terms(schema, tokenizer_manager) { Ok(res) => res, Err(InvalidQuery::FieldDoesNotExist { .. }) if self.lenient => { - return Ok(crate::MatchAllOrNone::MatchNone.into()) + return Ok(TantivyQueryAst::match_none()) } Err(e) => return Err(e), };