From 3c74e71d4f5abfc60c556fdf874336f7abcd44c7 Mon Sep 17 00:00:00 2001 From: qdequele Date: Wed, 26 Feb 2020 18:24:19 +0100 Subject: [PATCH] show default ranking rules if user reset them; fix #476 --- meilisearch-core/src/settings.rs | 24 ++++++++++++++---------- meilisearch-http/src/routes/setting.rs | 23 +++++++++++++---------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/meilisearch-core/src/settings.rs b/meilisearch-core/src/settings.rs index b97a14874..1bebdce53 100644 --- a/meilisearch-core/src/settings.rs +++ b/meilisearch-core/src/settings.rs @@ -5,6 +5,10 @@ use std::iter::IntoIterator; use serde::{Deserialize, Deserializer, Serialize}; use once_cell::sync::Lazy; +use self::RankingRule::*; + +pub const DEFAULT_RANKING_RULES: [RankingRule; 6] = [Typo, Words, Proximity, Attribute, WordsPosition, Exactness]; + static RANKING_RULE_REGEX: Lazy = Lazy::new(|| { let regex = regex::Regex::new(r"(asc|dsc)\(([a-zA-Z0-9-_]*)\)").unwrap(); regex @@ -98,17 +102,17 @@ pub enum RankingRule { Dsc(String), } -impl ToString for RankingRule { - fn to_string(&self) -> String { +impl std::fmt::Display for RankingRule { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - RankingRule::Typo => "_typo".to_string(), - RankingRule::Words => "_words".to_string(), - RankingRule::Proximity => "_proximity".to_string(), - RankingRule::Attribute => "_attribute".to_string(), - RankingRule::WordsPosition => "_words_position".to_string(), - RankingRule::Exactness => "_exactness".to_string(), - RankingRule::Asc(field) => format!("asc({})", field), - RankingRule::Dsc(field) => format!("dsc({})", field), + RankingRule::Typo => write!(f, "_typo"), + RankingRule::Words => write!(f, "_words"), + RankingRule::Proximity => write!(f, "_proximity"), + RankingRule::Attribute => write!(f, "_attribute"), + RankingRule::WordsPosition => write!(f, "_words_position"), + RankingRule::Exactness => write!(f, "_exactness"), + RankingRule::Asc(field) => write!(f, "asc({})", field), + RankingRule::Dsc(field) => write!(f, "dsc({})", field), } } } diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index f106a69ea..9b8c41a2f 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -1,4 +1,4 @@ -use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState}; +use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES}; use serde::Deserialize; use std::collections::{BTreeMap, BTreeSet, HashSet}; use tide::{Request, Response}; @@ -46,10 +46,12 @@ pub async fn get_all(ctx: Request) -> SResult { None }; - let ranking_rules = match index.main.ranking_rules(&reader)? { - Some(rules) => Some(rules.iter().map(|r| r.to_string()).collect()), - None => None, - }; + let ranking_rules = index.main.ranking_rules(&reader)? + .unwrap_or(DEFAULT_RANKING_RULES.to_vec()) + .into_iter() + .map(|r| r.to_string()) + .collect(); + let distinct_attribute = index.main.distinct_attribute(&reader)?; let schema = index.main.schema(&reader)?; @@ -80,7 +82,7 @@ pub async fn get_all(ctx: Request) -> SResult { let accept_new_fields = schema.map(|s| s.accept_new_fields()); let settings = Settings { - ranking_rules: Some(ranking_rules), + ranking_rules: Some(Some(ranking_rules)), distinct_attribute: Some(distinct_attribute), searchable_attributes, displayed_attributes, @@ -161,10 +163,11 @@ pub async fn get_rules(ctx: Request) -> SResult { let db = &ctx.state().db; let reader = db.main_read_txn()?; - let ranking_rules: Option> = match index.main.ranking_rules(&reader)? { - Some(rules) => Some(rules.iter().map(|r| r.to_string()).collect()), - None => None, - }; + let ranking_rules = index.main.ranking_rules(&reader)? + .unwrap_or(DEFAULT_RANKING_RULES.to_vec()) + .into_iter() + .map(|r| r.to_string()) + .collect::>(); Ok(tide::Response::new(200).body_json(&ranking_rules).unwrap()) }