diff --git a/src/criterion.rs b/src/criterion.rs index fd334f7d9..176630950 100644 --- a/src/criterion.rs +++ b/src/criterion.rs @@ -1,3 +1,8 @@ +use crate::FieldId; + +use serde::{Serialize, Deserialize}; + +#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq)] pub enum Criterion { /// Sorted by increasing number of typos. Typo, @@ -14,9 +19,9 @@ pub enum Criterion { /// Sorted by the similarity of the matched words with the query words. Exactness, /// Sorted by the increasing value of the field specified. - CustomAsc(String), + Asc(FieldId), /// Sorted by the decreasing value of the field specified. - CustomDesc(String), + Desc(FieldId), } pub fn default_criteria() -> Vec { diff --git a/src/index.rs b/src/index.rs index b0c2b1a3f..81b306dce 100644 --- a/src/index.rs +++ b/src/index.rs @@ -9,13 +9,14 @@ use roaring::RoaringBitmap; use crate::facet::FacetType; use crate::fields_ids_map::FieldsIdsMap; -use crate::Search; +use crate::{default_criteria, Criterion, Search}; use crate::{BEU32, DocumentId, FieldId, ExternalDocumentsIds}; use crate::{ RoaringBitmapCodec, BEU32StrCodec, StrStrU8Codec, ObkvCodec, BoRoaringBitmapCodec, CboRoaringBitmapCodec, }; +pub const CRITERIA_KEY: &str = "criteria"; pub const DISPLAYED_FIELDS_KEY: &str = "displayed-fields"; pub const DOCUMENTS_IDS_KEY: &str = "documents-ids"; pub const FACETED_DOCUMENTS_IDS_PREFIX: &str = "faceted-documents-ids"; @@ -246,6 +247,23 @@ impl Index { } } + /* criteria */ + + pub fn put_criteria(&self, wtxn: &mut RwTxn, criteria: &[Criterion]) -> heed::Result<()> { + self.main.put::<_, Str, SerdeJson<&[Criterion]>>(wtxn, CRITERIA_KEY, &criteria) + } + + pub fn delete_criteria(&self, wtxn: &mut RwTxn) -> heed::Result { + self.main.delete::<_, Str>(wtxn, CRITERIA_KEY) + } + + pub fn criteria(&self, rtxn: &RoTxn) -> heed::Result> { + match self.main.get::<_, Str, SerdeJson>>(rtxn, CRITERIA_KEY)? { + Some(criteria) => Ok(criteria), + None => Ok(default_criteria()), + } + } + /* words fst */ /// Writes the FST which is the words dictionnary of the engine.