diff --git a/src/facet/facet_type.rs b/src/facet/facet_type.rs index 13482e8b3..93c98316d 100644 --- a/src/facet/facet_type.rs +++ b/src/facet/facet_type.rs @@ -1,6 +1,8 @@ use std::cmp; +use serde::{Serialize, Deserialize}; #[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)] +#[derive(Serialize, Deserialize)] pub enum FacetType { String, F64, diff --git a/src/index.rs b/src/index.rs index 49f877d81..4946545e4 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::collections::HashMap; use std::path::Path; use anyhow::Context; @@ -6,9 +7,10 @@ use heed::types::*; use heed::{PolyDatabase, Database, RwTxn, RoTxn}; use roaring::RoaringBitmap; +use crate::facet::FacetType; +use crate::fields_ids_map::FieldsIdsMap; use crate::Search; use crate::{BEU32, DocumentId}; -use crate::fields_ids_map::FieldsIdsMap; use crate::{ RoaringBitmapCodec, BEU32StrCodec, StrStrU8Codec, ObkvCodec, BoRoaringBitmapCodec, CboRoaringBitmapCodec, @@ -16,6 +18,7 @@ use crate::{ pub const DISPLAYED_FIELDS_KEY: &str = "displayed-fields"; pub const DOCUMENTS_IDS_KEY: &str = "documents-ids"; +pub const FACETED_FIELDS_KEY: &str = "faceted-fields"; pub const FIELDS_IDS_MAP_KEY: &str = "fields-ids-map"; pub const PRIMARY_KEY_KEY: &str = "primary-key"; pub const SEARCHABLE_FIELDS_KEY: &str = "searchable-fields"; @@ -186,6 +189,24 @@ impl Index { self.main.get::<_, Str, ByteSlice>(rtxn, SEARCHABLE_FIELDS_KEY) } + /* faceted fields */ + + /// Writes the facet fields ids associated with their facet type or `None` if + /// the facet type is currently unknown. + pub fn put_faceted_fields(&self, wtxn: &mut RwTxn, fields_types: &HashMap>) -> heed::Result<()> { + self.main.put::<_, Str, SerdeJson<_>>(wtxn, FACETED_FIELDS_KEY, fields_types) + } + + /// Deletes the facet fields ids associated with their facet type. + pub fn delete_faceted_fields(&self, wtxn: &mut RwTxn) -> heed::Result { + self.main.delete::<_, Str>(wtxn, FACETED_FIELDS_KEY) + } + + /// Returns the facet fields ids associated with their facet type. + pub fn faceted_fields(&self, wtxn: &RoTxn) -> heed::Result>> { + Ok(self.main.get::<_, Str, SerdeJson<_>>(wtxn, FACETED_FIELDS_KEY)?.unwrap_or_default()) + } + /* words fst */ /// Writes the FST which is the words dictionnary of the engine.