diff --git a/src/query/query_parser/query_parser.rs b/src/query/query_parser/query_parser.rs index ef7d0f392c..597dca07c4 100644 --- a/src/query/query_parser/query_parser.rs +++ b/src/query/query_parser/query_parser.rs @@ -810,6 +810,8 @@ mod test { schema_builder.add_bytes_field("bytes_not_indexed", STORED); schema_builder.add_json_field("json", TEXT); schema_builder.add_json_field("json_not_indexed", STORED); + schema_builder.add_bool_field("bool", INDEXED); + schema_builder.add_bool_field("notindexed_bool", STORED); schema_builder.build() } @@ -925,6 +927,10 @@ mod test { is_not_indexed_err("notindexed_i64:-234324"), Some(String::from("notindexed_i64")) ); + assert_eq!( + is_not_indexed_err("notindexed_bool:true"), + Some(String::from("notindexed_bool")) + ); } #[test] @@ -1006,6 +1012,18 @@ mod test { ); } + #[test] + fn test_parse_bool() { + test_parse_query_to_logical_ast_helper( + "bool:true", + &format!( + "{:?}", + Term::from_field_bool(Field::from_field_id(16u32), true), + ), + false, + ); + } + #[test] fn test_parse_bytes_not_indexed() { let error = parse_query_to_logical_ast("bytes_not_indexed:aaa", false).unwrap_err(); @@ -1050,6 +1068,15 @@ mod test { ); } + #[test] + fn test_json_field_possibly_a_bool() { + test_parse_query_to_logical_ast_helper( + "json.titi:true", + r#"(Term(type=Json, field=14, path=titi, vtype=Bool, true) Term(type=Json, field=14, path=titi, vtype=Str, "true"))"#, + true, + ); + } + #[test] fn test_json_field_not_indexed() { let error = parse_query_to_logical_ast("json_not_indexed.titi:hello", false).unwrap_err(); @@ -1299,6 +1326,17 @@ mod test { ); } + #[test] + pub fn test_query_parser_expected_bool() { + let query_parser = make_query_parser(); + assert_matches!( + query_parser.parse_query("bool:brie"), + Err(QueryParserError::ExpectedBool(_)) + ); + assert!(query_parser.parse_query("bool:\"true\"").is_ok()); + assert!(query_parser.parse_query("bool:\"false\"").is_ok()); + } + #[test] pub fn test_query_parser_expected_date() { let query_parser = make_query_parser(); diff --git a/src/schema/schema.rs b/src/schema/schema.rs index a950eecd87..03e0f879d2 100644 --- a/src/schema/schema.rs +++ b/src/schema/schema.rs @@ -466,6 +466,9 @@ mod tests { .set_indexed() .set_fieldnorm() .set_fast(Cardinality::SingleValue); + let is_read_options = NumericOptions::default() + .set_stored() + .set_fast(Cardinality::SingleValue); schema_builder.add_text_field("title", TEXT); schema_builder.add_text_field( "author", @@ -478,6 +481,7 @@ mod tests { schema_builder.add_u64_field("count", count_options); schema_builder.add_i64_field("popularity", popularity_options); schema_builder.add_f64_field("score", score_options); + schema_builder.add_bool_field("is_read", is_read_options); let schema = schema_builder.build(); let schema_json = serde_json::to_string_pretty(&schema).unwrap(); let expected = r#"[ @@ -536,6 +540,16 @@ mod tests { "fast": "single", "stored": false } + }, + { + "name": "is_read", + "type": "bool", + "options": { + "indexed": false, + "fieldnorms": false, + "fast": "single", + "stored": true + } } ]"#; assert_eq!(schema_json, expected); @@ -568,6 +582,11 @@ mod tests { assert_eq!("score", field_entry.name()); assert_eq!(4, field.field_id()); } + { + let (field, field_entry) = fields.next().unwrap(); + assert_eq!("is_read", field_entry.name()); + assert_eq!(5, field.field_id()); + } assert!(fields.next().is_none()); } @@ -577,14 +596,19 @@ mod tests { let count_options = NumericOptions::default() .set_stored() .set_fast(Cardinality::SingleValue); + let is_read_options = NumericOptions::default() + .set_stored() + .set_fast(Cardinality::SingleValue); schema_builder.add_text_field("title", TEXT); schema_builder.add_text_field("author", STRING); schema_builder.add_u64_field("count", count_options); + schema_builder.add_bool_field("is_read", is_read_options); let schema = schema_builder.build(); let doc_json = r#"{ "title": "my title", "author": "fulmicoton", - "count": 4 + "count": 4, + "is_read": true }"#; let doc = schema.parse_document(doc_json).unwrap(); diff --git a/src/schema/term.rs b/src/schema/term.rs index 5837bb1e5d..a485ef8960 100644 --- a/src/schema/term.rs +++ b/src/schema/term.rs @@ -458,4 +458,15 @@ mod tests { assert_eq!(term.as_slice().len(), super::FAST_VALUE_TERM_LEN); assert_eq!(term.as_u64(), Some(983u64)) } + + #[test] + pub fn test_term_bool() { + let mut schema_builder = Schema::builder(); + let bool_field = schema_builder.add_bool_field("bool", INDEXED); + let term = Term::from_field_bool(bool_field, true); + assert_eq!(term.field(), bool_field); + assert_eq!(term.typ(), Type::Bool); + assert_eq!(term.as_slice().len(), super::FAST_VALUE_TERM_LEN); + assert_eq!(term.as_bool(), Some(true)) + } }