From ef2492dba6a84fb5430d2a10018b83ce40be6edb Mon Sep 17 00:00:00 2001 From: boraarslan Date: Thu, 2 Jun 2022 15:46:19 +0300 Subject: [PATCH] Broken commit --- src/fastfield/mod.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++ src/schema/flags.rs | 4 ++-- src/schema/schema.rs | 20 ++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/fastfield/mod.rs b/src/fastfield/mod.rs index c1e878c57..c36dce090 100644 --- a/src/fastfield/mod.rs +++ b/src/fastfield/mod.rs @@ -819,6 +819,51 @@ mod tests { } Ok(()) } + + #[test] + pub fn test_fastfield_bool() { + let test_fastfield = DynamicFastFieldReader::::from(vec![true, false, true, false]); + assert_eq!(test_fastfield.get(0), true); + assert_eq!(test_fastfield.get(1), false); + assert_eq!(test_fastfield.get(2), true); + assert_eq!(test_fastfield.get(3), false); + } + + #[test] + pub fn test_fastfield_bool_small() -> crate::Result<()> { + let path = Path::new("test_bool"); + let directory: RamDirectory = RamDirectory::create(); + + let mut schema_builder = Schema::builder(); + schema_builder.add_bool_field("field_bool", FAST); + let schema = schema_builder.build(); + let field = schema.get_field("field_bool").unwrap(); + + { + let write: WritePtr = directory.open_write(path).unwrap(); + let mut serializer = CompositeFastFieldSerializer::from_write(write).unwrap(); + let mut fast_field_writers = FastFieldsWriter::from_schema(&schema); + fast_field_writers.add_document(&doc!(field=>true)); + fast_field_writers.add_document(&doc!(field=>false)); + fast_field_writers.add_document(&doc!(field=>true)); + fast_field_writers.add_document(&doc!(field=>false)); + fast_field_writers + .serialize(&mut serializer, &HashMap::new(), None) + .unwrap(); + serializer.close().unwrap(); + } + let file = directory.open_read(&path).unwrap(); + assert_eq!(file.len(), 5); + let composite_file = CompositeFile::open(&file)?; + let file = composite_file.open_read(field).unwrap(); + let fast_field_reader = DynamicFastFieldReader::::open(file)?; + assert_eq!(fast_field_reader.get(0), true); + assert_eq!(fast_field_reader.get(1), false); + assert_eq!(fast_field_reader.get(2), true); + assert_eq!(fast_field_reader.get(3), false); + + Ok(()) + } } #[cfg(all(test, feature = "unstable"))] diff --git a/src/schema/flags.rs b/src/schema/flags.rs index d82b434c6..925c021cb 100644 --- a/src/schema/flags.rs +++ b/src/schema/flags.rs @@ -22,7 +22,7 @@ pub const STORED: SchemaFlagList = SchemaFlagList { pub struct IndexedFlag; /// Flag to mark the field as indexed. An indexed field is searchable and has a fieldnorm. /// -/// The `INDEXED` flag can only be used when building `NumericOptions` (`u64`, `i64` and `f64` +/// The `INDEXED` flag can only be used when building `NumericOptions` (`u64`, `i64`, `f64` and `bool` /// fields) Of course, text fields can also be indexed... But this is expressed by using either the /// `STRING` (untokenized) or `TEXT` (tokenized with the english tokenizer) flags. pub const INDEXED: SchemaFlagList = SchemaFlagList { @@ -36,7 +36,7 @@ pub struct FastFlag; /// /// Fast fields can be random-accessed rapidly. Fields useful for scoring, filtering /// or collection should be mark as fast fields. -/// The `FAST` flag can only be used when building `NumericOptions` (`u64`, `i64` and `f64` fields) +/// The `FAST` flag can only be used when building `NumericOptions` (`u64`, `i64`, `f64` and `bool` fields) pub const FAST: SchemaFlagList = SchemaFlagList { head: FastFlag, tail: (), diff --git a/src/schema/schema.rs b/src/schema/schema.rs index 235d0412d..a950eecd8 100644 --- a/src/schema/schema.rs +++ b/src/schema/schema.rs @@ -102,6 +102,26 @@ impl SchemaBuilder { self.add_field(field_entry) } + /// Adds a new bool field. + /// Returns the associated field handle + /// + /// # Caution + /// + /// Appending two fields with the same name + /// will result in the shadowing of the first + /// by the second one. + /// The first field will get a field id + /// but only the second one will be indexed + pub fn add_bool_field>( + &mut self, + field_name_str: &str, + field_options: T, + ) -> Field { + let field_name = String::from(field_name_str); + let field_entry = FieldEntry::new_bool(field_name, field_options.into()); + self.add_field(field_entry) + } + /// Adds a new date field. /// Returns the associated field handle /// Internally, Tantivy simply stores dates as i64 UTC timestamps,