mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-22 12:54:26 +01:00
show default ranking rules if user reset them; fix #476
This commit is contained in:
parent
79e07fa852
commit
3c74e71d4f
@ -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<regex::Regex> = 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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Data>) -> SResult<Response> {
|
||||
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<Data>) -> SResult<Response> {
|
||||
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<Data>) -> SResult<Response> {
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let ranking_rules: Option<Vec<String>> = 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::<Vec<String>>();
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&ranking_rules).unwrap())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user