show default ranking rules if user reset them; fix #476

This commit is contained in:
qdequele 2020-02-26 18:24:19 +01:00
parent 79e07fa852
commit 3c74e71d4f
No known key found for this signature in database
GPG Key ID: B3F0A000EBF11745
2 changed files with 27 additions and 20 deletions

View File

@ -5,6 +5,10 @@ use std::iter::IntoIterator;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use once_cell::sync::Lazy; 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<regex::Regex> = Lazy::new(|| { static RANKING_RULE_REGEX: Lazy<regex::Regex> = Lazy::new(|| {
let regex = regex::Regex::new(r"(asc|dsc)\(([a-zA-Z0-9-_]*)\)").unwrap(); let regex = regex::Regex::new(r"(asc|dsc)\(([a-zA-Z0-9-_]*)\)").unwrap();
regex regex
@ -98,17 +102,17 @@ pub enum RankingRule {
Dsc(String), Dsc(String),
} }
impl ToString for RankingRule { impl std::fmt::Display for RankingRule {
fn to_string(&self) -> String { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
RankingRule::Typo => "_typo".to_string(), RankingRule::Typo => write!(f, "_typo"),
RankingRule::Words => "_words".to_string(), RankingRule::Words => write!(f, "_words"),
RankingRule::Proximity => "_proximity".to_string(), RankingRule::Proximity => write!(f, "_proximity"),
RankingRule::Attribute => "_attribute".to_string(), RankingRule::Attribute => write!(f, "_attribute"),
RankingRule::WordsPosition => "_words_position".to_string(), RankingRule::WordsPosition => write!(f, "_words_position"),
RankingRule::Exactness => "_exactness".to_string(), RankingRule::Exactness => write!(f, "_exactness"),
RankingRule::Asc(field) => format!("asc({})", field), RankingRule::Asc(field) => write!(f, "asc({})", field),
RankingRule::Dsc(field) => format!("dsc({})", field), RankingRule::Dsc(field) => write!(f, "dsc({})", field),
} }
} }
} }

View File

@ -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 serde::Deserialize;
use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashSet};
use tide::{Request, Response}; use tide::{Request, Response};
@ -46,10 +46,12 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
None None
}; };
let ranking_rules = match index.main.ranking_rules(&reader)? { let ranking_rules = index.main.ranking_rules(&reader)?
Some(rules) => Some(rules.iter().map(|r| r.to_string()).collect()), .unwrap_or(DEFAULT_RANKING_RULES.to_vec())
None => None, .into_iter()
}; .map(|r| r.to_string())
.collect();
let distinct_attribute = index.main.distinct_attribute(&reader)?; let distinct_attribute = index.main.distinct_attribute(&reader)?;
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
@ -80,7 +82,7 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
let accept_new_fields = schema.map(|s| s.accept_new_fields()); let accept_new_fields = schema.map(|s| s.accept_new_fields());
let settings = Settings { let settings = Settings {
ranking_rules: Some(ranking_rules), ranking_rules: Some(Some(ranking_rules)),
distinct_attribute: Some(distinct_attribute), distinct_attribute: Some(distinct_attribute),
searchable_attributes, searchable_attributes,
displayed_attributes, displayed_attributes,
@ -161,10 +163,11 @@ pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let reader = db.main_read_txn()?; let reader = db.main_read_txn()?;
let ranking_rules: Option<Vec<String>> = match index.main.ranking_rules(&reader)? { let ranking_rules = index.main.ranking_rules(&reader)?
Some(rules) => Some(rules.iter().map(|r| r.to_string()).collect()), .unwrap_or(DEFAULT_RANKING_RULES.to_vec())
None => None, .into_iter()
}; .map(|r| r.to_string())
.collect::<Vec<String>>();
Ok(tide::Response::new(200).body_json(&ranking_rules).unwrap()) Ok(tide::Response::new(200).body_json(&ranking_rules).unwrap())
} }