Merge pull request #191 from meilisearch/typed-settings

Typed settings
This commit is contained in:
Clément Renault 2019-09-18 14:04:07 +02:00 committed by GitHub
commit 1b18679950
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 118 additions and 11 deletions

View File

@ -38,3 +38,4 @@ branch = "arc-byte-slice"
[dev-dependencies]
tempfile = "3.1.0"
maplit = "1.0.2"
big_s = "1.0.2"

View File

@ -1,4 +1,25 @@
use serde::de::DeserializeOwned;
use serde::{Serialize, Deserialize};
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use super::Error;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RankingOrdering {
Asc,
Dsc
}
pub type StopWords = HashSet<String>;
pub type RankingOrder = Vec<String>;
pub type DistinctField = String;
pub type RankingRules = HashMap<String, RankingOrdering>;
const STOP_WORDS_KEY: &str = "stop-words";
const RANKING_ORDER_KEY: &str = "ranking-order";
const DISTINCT_FIELD_KEY: &str = "distinct-field";
const RANKING_RULES_KEY: &str = "ranking-rules";
#[derive(Clone)]
pub struct CustomSettingsIndex(pub(crate) crate::CfTree);
@ -10,3 +31,59 @@ impl Deref for CustomSettingsIndex {
&self.0
}
}
impl CustomSettingsIndex {
fn get<K, T>(&self, key: K) -> Result<Option<T>, Error>
where K: AsRef<[u8]>,
T: DeserializeOwned,
{
let setting = self.0.get(key)?;
let raw = match setting {
Some(raw) => raw,
None => return Ok(None)
};
Ok(Some(bincode::deserialize(&raw)?))
}
fn set<K, T>(&self, key: K, data: &T) -> Result<(), Error>
where K: AsRef<[u8]>,
T: Serialize,
{
let raw = bincode::serialize(data)?;
self.0.insert(key, &raw)?;
Ok(())
}
pub fn get_stop_words(&self) -> Result<Option<StopWords>, Error> {
self.get(STOP_WORDS_KEY)
}
pub fn get_ranking_order(&self) -> Result<Option<RankingOrder>, Error> {
self.get(RANKING_ORDER_KEY)
}
pub fn get_distinct_field(&self) -> Result<Option<DistinctField>, Error> {
self.get(DISTINCT_FIELD_KEY)
}
pub fn get_ranking_rules(&self) -> Result<Option<RankingRules>, Error> {
self.get(RANKING_RULES_KEY)
}
pub fn set_stop_words(&self, value: &StopWords) -> Result<(), Error> {
self.set(STOP_WORDS_KEY, value)
}
pub fn set_ranking_order(&self, value: &RankingOrder) -> Result<(), Error> {
self.set(RANKING_ORDER_KEY, value)
}
pub fn set_distinct_field(&self, value: &DistinctField) -> Result<(), Error> {
self.set(DISTINCT_FIELD_KEY, value)
}
pub fn set_ranking_rules(&self, value: &RankingRules) -> Result<(), Error> {
self.set(RANKING_RULES_KEY, value)
}
}

View File

@ -17,7 +17,7 @@ use crate::CfTree;
use crate::ranked_map::RankedMap;
use crate::serde::{Deserializer, DeserializerError};
pub use self::custom_settings_index::CustomSettingsIndex;
pub use self::custom_settings_index::{CustomSettingsIndex, RankingOrdering, StopWords, RankingOrder, DistinctField, RankingRules};
pub use self::common_index::CommonIndex;
use self::docs_words_index::DocsWordsIndex;
use self::documents_index::DocumentsIndex;

View File

@ -12,7 +12,7 @@ mod update;
use crate::CfTree;
pub use self::error::Error;
pub use self::index::{Index, CustomSettingsIndex, CommonIndex};
pub use self::index::{Index, CustomSettingsIndex, CommonIndex, RankingOrdering, StopWords, RankingOrder, DistinctField, RankingRules};
pub use self::update::DocumentsAddition;
pub use self::update::DocumentsDeletion;

View File

@ -7,7 +7,7 @@ mod ranked_map;
mod serde;
pub use self::cf_tree::{CfTree, CfIter};
pub use self::database::{Database, Index, CustomSettingsIndex};
pub use self::database::{Database, Index, CustomSettingsIndex, RankingOrdering, StopWords, RankingOrder, DistinctField, RankingRules};
pub use self::number::Number;
pub use self::ranked_map::RankedMap;
pub use self::serde::{compute_document_id, extract_document_id, value_to_string};

View File

@ -3,8 +3,9 @@
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
use std::sync::Arc;
use big_s::S;
use serde_json::json;
use meilidb_data::Database;
use meilidb_data::{Database, RankingOrdering};
use meilidb_schema::{Schema, SchemaBuilder, DISPLAYED, INDEXED};
fn simple_schema() -> Schema {
@ -120,8 +121,8 @@ fn database_stats() {
assert!(status.result.is_ok());
let stats = index.stats().unwrap();
let repartition = hashmap!{
"objectId".to_string() => 1u64,
"title".to_string() => 1u64,
S("objectId") => 1u64,
S("title") => 1u64,
};
assert_eq!(stats.number_of_documents, 1);
assert_eq!(stats.documents_fields_repartition, repartition);
@ -136,8 +137,8 @@ fn database_stats() {
assert!(status.result.is_ok());
let stats = index.stats().unwrap();
let repartition = hashmap!{
"objectId".to_string() => 2u64,
"title".to_string() => 2u64,
S("objectId") => 2u64,
S("title") => 2u64,
};
assert_eq!(stats.number_of_documents, 2);
assert_eq!(stats.documents_fields_repartition, repartition);
@ -153,10 +154,38 @@ fn database_stats() {
assert!(status.result.is_ok());
let stats = index.stats().unwrap();
let repartition = hashmap!{
"objectId".to_string() => 3u64,
"title".to_string() => 2u64,
S("objectId") => 3u64,
S("title") => 2u64,
};
assert_eq!(stats.number_of_documents, 3);
assert_eq!(stats.documents_fields_repartition, repartition);
}
#[test]
fn custom_settings() {
let tmp_dir = tempfile::tempdir().unwrap();
let database = Database::open(&tmp_dir).unwrap();
let schema = simple_schema();
let index = database.create_index("hello", schema).unwrap();
let stop_words = hashset!{ S("le"), S("la"), S("les"), };
let ranking_order = vec![S("SumOfTypos"), S("NumberOfWords"), S("WordsProximity"), S("SumOfWordsAttribute"), S("SumOfWordsPosition"), S("Exact"), S("DocumentId")];
let distinct_field = S("title");
let ranking_rules = hashmap!{ S("objectId") => RankingOrdering::Asc };
index.custom_settings().set_stop_words(&stop_words).unwrap();
index.custom_settings().set_ranking_order(&ranking_order).unwrap();
index.custom_settings().set_distinct_field(&distinct_field).unwrap();
index.custom_settings().set_ranking_rules(&ranking_rules).unwrap();
let ret_stop_words = index.custom_settings().get_stop_words().unwrap().unwrap();
let ret_ranking_orderer = index.custom_settings().get_ranking_order().unwrap().unwrap();
let ret_distinct_field = index.custom_settings().get_distinct_field().unwrap().unwrap();
let ret_ranking_rules = index.custom_settings().get_ranking_rules().unwrap().unwrap();
assert_eq!(ret_stop_words, stop_words);
assert_eq!(ret_ranking_orderer, ranking_order);
assert_eq!(ret_distinct_field, distinct_field);
assert_eq!(ret_ranking_rules, ranking_rules);
}