mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-12-26 06:30:05 +01:00
expose api for new settings
This commit is contained in:
parent
7c14769226
commit
38d57d213f
@ -1,5 +1,6 @@
|
||||
use std::sync::Mutex;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::collections::{BTreeMap, BTreeSet, HashSet};
|
||||
use std::str::FromStr;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use once_cell::sync::Lazy;
|
||||
@ -16,8 +17,8 @@ pub struct Settings {
|
||||
pub ranking_distinct: Option<String>,
|
||||
pub attribute_identifier: Option<String>,
|
||||
pub attributes_searchable: Option<Vec<String>>,
|
||||
pub attributes_displayed: Option<Vec<String>>,
|
||||
pub attributes_ranked: Option<Vec<String>>,
|
||||
pub attributes_displayed: Option<HashSet<String>>,
|
||||
pub attributes_ranked: Option<HashSet<String>>,
|
||||
pub stop_words: Option<BTreeSet<String>>,
|
||||
pub synonyms: Option<BTreeMap<String, Vec<String>>>,
|
||||
}
|
||||
@ -93,6 +94,15 @@ impl<T> UpdateState<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RankingRuleConversionError;
|
||||
|
||||
impl std::fmt::Display for RankingRuleConversionError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "impossible to convert into RankingRule")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum RankingRule {
|
||||
Typo,
|
||||
@ -105,14 +115,53 @@ pub enum RankingRule {
|
||||
Dsc(String),
|
||||
}
|
||||
|
||||
impl ToString for RankingRule {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
RankingRule::Typo => "_typo".to_string(),
|
||||
RankingRule::Words => "_words".to_string(),
|
||||
RankingRule::Proximity => "_proximity".to_string(),
|
||||
RankingRule::Attribute => "_attribute".to_string(),
|
||||
RankingRule::WordsPosition => "_word_position".to_string(),
|
||||
RankingRule::Exact => "_exact".to_string(),
|
||||
RankingRule::Asc(field) => format!("asc({})", field),
|
||||
RankingRule::Dsc(field) => format!("dsc({})", field),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for RankingRule {
|
||||
type Err = RankingRuleConversionError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let rule = match s {
|
||||
"_typo" => RankingRule::Typo,
|
||||
"_words" => RankingRule::Words,
|
||||
"_proximity" => RankingRule::Proximity,
|
||||
"_attribute" => RankingRule::Attribute,
|
||||
"_words_position" => RankingRule::WordsPosition,
|
||||
"_exact" => RankingRule::Exact,
|
||||
_ => {
|
||||
let captures = RANKING_RULE_REGEX.lock().unwrap().captures(s).unwrap();
|
||||
match captures[0].as_ref() {
|
||||
"asc" => RankingRule::Asc(captures[1].to_string()),
|
||||
"dsc" => RankingRule::Dsc(captures[1].to_string()),
|
||||
_ => return Err(RankingRuleConversionError)
|
||||
}
|
||||
}
|
||||
};
|
||||
Ok(rule)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SettingsUpdate {
|
||||
pub ranking_rules: UpdateState<Vec<RankingRule>>,
|
||||
pub ranking_distinct: UpdateState<String>,
|
||||
pub attribute_identifier: UpdateState<String>,
|
||||
pub attributes_searchable: UpdateState<Vec<String>>,
|
||||
pub attributes_displayed: UpdateState<Vec<String>>,
|
||||
pub attributes_ranked: UpdateState<Vec<String>>,
|
||||
pub attributes_displayed: UpdateState<HashSet<String>>,
|
||||
pub attributes_ranked: UpdateState<HashSet<String>>,
|
||||
pub stop_words: UpdateState<BTreeSet<String>>,
|
||||
pub synonyms: UpdateState<BTreeMap<String, Vec<String>>>,
|
||||
}
|
||||
|
@ -76,6 +76,50 @@ pub fn load_routes(app: &mut tide::Server<Data>) {
|
||||
});
|
||||
|
||||
router.at("/settings").nest(|router| {
|
||||
|
||||
router
|
||||
.get(|ctx| into_response(setting::get_all(ctx)))
|
||||
.post(|ctx| into_response(setting::update_all(ctx)))
|
||||
.delete(|ctx| into_response(setting::delete_all(ctx)));
|
||||
|
||||
router.at("/ranking").nest(|router| {
|
||||
|
||||
router
|
||||
.get(|ctx| into_response(setting::get_ranking(ctx)))
|
||||
.post(|ctx| into_response(setting::update_ranking(ctx)))
|
||||
.delete(|ctx| into_response(setting::delete_ranking(ctx)));
|
||||
|
||||
router.at("/rules")
|
||||
.get(|ctx| into_response(setting::get_rules(ctx)))
|
||||
.post(|ctx| into_response(setting::update_rules(ctx)))
|
||||
.delete(|ctx| into_response(setting::delete_rules(ctx)));
|
||||
|
||||
router.at("/distinct")
|
||||
.get(|ctx| into_response(setting::get_distinct(ctx)))
|
||||
.post(|ctx| into_response(setting::update_distinct(ctx)))
|
||||
.delete(|ctx| into_response(setting::delete_distinct(ctx)));
|
||||
});
|
||||
|
||||
router.at("/attributes").nest(|router| {
|
||||
router
|
||||
.get(|ctx| into_response(setting::get_attributes(ctx)))
|
||||
.post(|ctx| into_response(setting::update_attributes(ctx)))
|
||||
.delete(|ctx| into_response(setting::delete_attributes(ctx)));
|
||||
|
||||
router.at("/identifier")
|
||||
.get(|ctx| into_response(setting::get_identifier(ctx)));
|
||||
|
||||
router.at("/searchable")
|
||||
.get(|ctx| into_response(setting::get_searchable(ctx)))
|
||||
.post(|ctx| into_response(setting::update_searchable(ctx)))
|
||||
.delete(|ctx| into_response(setting::delete_searchable(ctx)));
|
||||
|
||||
router.at("/displayed")
|
||||
.get(|ctx| into_response(setting::get_displayed(ctx)))
|
||||
.post(|ctx| into_response(setting::update_displayed(ctx)))
|
||||
.delete(|ctx| into_response(setting::delete_displayed(ctx)));
|
||||
});
|
||||
|
||||
router.at("/synonyms")
|
||||
.get(|ctx| into_response(synonym::get(ctx)))
|
||||
.post(|ctx| into_response(synonym::update(ctx)))
|
||||
@ -85,9 +129,7 @@ pub fn load_routes(app: &mut tide::Server<Data>) {
|
||||
.get(|ctx| into_response(stop_words::get(ctx)))
|
||||
.post(|ctx| into_response(stop_words::update(ctx)))
|
||||
.delete(|ctx| into_response(stop_words::delete(ctx)));
|
||||
})
|
||||
.get(|ctx| into_response(setting::get(ctx)))
|
||||
.post(|ctx| into_response(setting::update(ctx)));
|
||||
});
|
||||
|
||||
router.at("/stats").get(|ctx| into_response(stats::index_stat(ctx)));
|
||||
});
|
||||
|
@ -1,5 +1,7 @@
|
||||
use serde::{Deserialize, Serialize, Deserializer};
|
||||
use std::collections::{BTreeMap, BTreeSet, HashSet};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tide::{Request, Response};
|
||||
use meilisearch_core::settings::{SettingsUpdate, UpdateState, Settings};
|
||||
|
||||
use crate::error::{ResponseError, SResult};
|
||||
use crate::helpers::tide::RequestExt;
|
||||
@ -7,85 +9,513 @@ use crate::models::token::ACL::*;
|
||||
use crate::routes::document::IndexUpdateResponse;
|
||||
use crate::Data;
|
||||
|
||||
#[derive(Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct Setting {
|
||||
pub distinct_field: Option<DistinctField>,
|
||||
pub ranking_rules: Option<RankingRules>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum RankingOrdering {
|
||||
Asc,
|
||||
Dsc,
|
||||
}
|
||||
|
||||
pub type DistinctField = String;
|
||||
pub type RankingRules = Vec<String>;
|
||||
|
||||
pub async fn get(ctx: Request<Data>) -> SResult<Response> {
|
||||
pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsRead)?;
|
||||
let index = ctx.index()?;
|
||||
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let settings = match index.main.customs(&reader).unwrap() {
|
||||
Some(bytes) => bincode::deserialize(bytes).unwrap(),
|
||||
None => Setting::default(),
|
||||
let stop_words_fst = index.main.stop_words_fst(&reader)?;
|
||||
let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?;
|
||||
let stop_words: BTreeSet<String> = stop_words.into_iter().collect();
|
||||
let stop_words = if stop_words.len() > 0 {
|
||||
Some(stop_words)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let synonyms_fst = index.main.synonyms_fst(&reader)?.unwrap_or_default();
|
||||
let synonyms_list = synonyms_fst.stream().into_strs()?;
|
||||
|
||||
let mut synonyms = BTreeMap::new();
|
||||
|
||||
let index_synonyms = &index.synonyms;
|
||||
|
||||
for synonym in synonyms_list {
|
||||
let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?;
|
||||
|
||||
if let Some(list) = alternative_list {
|
||||
let list = list.stream().into_strs()?;
|
||||
synonyms.insert(synonym, list);
|
||||
}
|
||||
}
|
||||
|
||||
let synonyms = if synonyms.len() > 0 {
|
||||
Some(synonyms)
|
||||
} else {
|
||||
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_distinct = index.main.ranking_distinct(&reader)?;
|
||||
|
||||
let schema = index.main.schema(&reader)?;
|
||||
|
||||
let attribute_identifier = schema.clone().map(|s| s.identifier());
|
||||
let attributes_searchable = schema.clone().map(|s| s.get_indexed_name());
|
||||
let attributes_displayed = schema.clone().map(|s| s.get_displayed_name());
|
||||
let attributes_ranked = schema.map(|s| s.get_ranked_name());
|
||||
|
||||
let settings = Settings {
|
||||
ranking_rules,
|
||||
ranking_distinct,
|
||||
attribute_identifier,
|
||||
attributes_searchable,
|
||||
attributes_displayed,
|
||||
attributes_ranked,
|
||||
stop_words,
|
||||
synonyms,
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct SettingBody {
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub distinct_field: Option<Option<DistinctField>>,
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub ranking_rules: Option<Option<RankingRules>>,
|
||||
}
|
||||
|
||||
// Any value that is present is considered Some value, including null.
|
||||
fn deserialize_some<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
|
||||
where T: Deserialize<'de>,
|
||||
D: Deserializer<'de>
|
||||
{
|
||||
Deserialize::deserialize(deserializer).map(Some)
|
||||
}
|
||||
|
||||
pub async fn update(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let settings: Settings = ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
|
||||
let settings: SettingBody = ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
|
||||
let index = ctx.index()?;
|
||||
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
let mut writer = db.update_write_txn()?;
|
||||
|
||||
let mut current_settings = match index.main.customs(&reader).unwrap() {
|
||||
Some(bytes) => bincode::deserialize(bytes).unwrap(),
|
||||
None => Setting::default(),
|
||||
let settings = SettingsUpdate {
|
||||
ranking_rules: UpdateState::Clear,
|
||||
ranking_distinct: UpdateState::Clear,
|
||||
attribute_identifier: UpdateState::Clear,
|
||||
attributes_searchable: UpdateState::Clear,
|
||||
attributes_displayed: UpdateState::Clear,
|
||||
attributes_ranked: UpdateState::Clear,
|
||||
stop_words: UpdateState::Clear,
|
||||
synonyms: UpdateState::Clear,
|
||||
};
|
||||
|
||||
if let Some(distinct_field) = settings.distinct_field {
|
||||
current_settings.distinct_field = distinct_field;
|
||||
}
|
||||
|
||||
if let Some(ranking_rules) = settings.ranking_rules {
|
||||
current_settings.ranking_rules = ranking_rules;
|
||||
}
|
||||
|
||||
let bytes = bincode::serialize(¤t_settings).unwrap();
|
||||
|
||||
let update_id = index.customs_update(&mut writer, bytes)?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
pub struct RankingSettings {
|
||||
pub ranking_rules: Option<Vec<String>>,
|
||||
pub ranking_distinct: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn get_ranking(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsRead)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let ranking_rules = match index.main.ranking_rules(&reader)? {
|
||||
Some(rules) => {
|
||||
Some(rules.iter().map(|r| r.to_string()).collect())
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
|
||||
let ranking_distinct = index.main.ranking_distinct(&reader)?;
|
||||
let settings = RankingSettings {
|
||||
ranking_rules,
|
||||
ranking_distinct,
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
}
|
||||
|
||||
pub async fn update_ranking(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let settings: RankingSettings = ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
ranking_rules: settings.ranking_rules,
|
||||
ranking_distinct: settings.ranking_distinct,
|
||||
.. Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
pub async fn delete_ranking(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let mut writer = db.update_write_txn()?;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
ranking_rules: UpdateState::Clear,
|
||||
ranking_distinct: UpdateState::Clear,
|
||||
.. SettingsUpdate::default()
|
||||
};
|
||||
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
pub struct RankingRulesSettings {
|
||||
pub ranking_rules: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsRead)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let ranking_rules = match index.main.ranking_rules(&reader)? {
|
||||
Some(rules) => {
|
||||
Some(rules.iter().map(|r| r.to_string()).collect())
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
|
||||
let settings = RankingRulesSettings {
|
||||
ranking_rules,
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
}
|
||||
|
||||
pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let settings: RankingRulesSettings = ctx.body_json().await
|
||||
.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
ranking_rules: settings.ranking_rules,
|
||||
.. Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
pub async fn delete_rules(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let mut writer = db.update_write_txn()?;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
ranking_rules: UpdateState::Clear,
|
||||
.. SettingsUpdate::default()
|
||||
};
|
||||
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
pub struct RankingDistinctSettings {
|
||||
pub ranking_distinct: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn get_distinct(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsRead)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let ranking_distinct = index.main.ranking_distinct(&reader)?;
|
||||
let settings = RankingDistinctSettings {
|
||||
ranking_distinct,
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
}
|
||||
|
||||
pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let settings: RankingDistinctSettings = ctx.body_json().await
|
||||
.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
ranking_distinct: settings.ranking_distinct,
|
||||
.. Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
pub async fn delete_distinct(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let mut writer = db.update_write_txn()?;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
ranking_distinct: UpdateState::Clear,
|
||||
.. SettingsUpdate::default()
|
||||
};
|
||||
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
pub struct AttributesSettings {
|
||||
pub attribute_identifier: Option<String>,
|
||||
pub attributes_searchable: Option<Vec<String>>,
|
||||
pub attributes_displayed: Option<HashSet<String>>,
|
||||
pub attributes_ranked: Option<HashSet<String>>,
|
||||
}
|
||||
|
||||
pub async fn get_attributes(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsRead)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let schema = index.main.schema(&reader)?;
|
||||
|
||||
let attribute_identifier = schema.clone().map(|s| s.identifier());
|
||||
let attributes_searchable = schema.clone().map(|s| s.get_indexed_name());
|
||||
let attributes_displayed = schema.clone().map(|s| s.get_displayed_name());
|
||||
let attributes_ranked = schema.map(|s| s.get_ranked_name());
|
||||
|
||||
let settings = AttributesSettings {
|
||||
attribute_identifier,
|
||||
attributes_searchable,
|
||||
attributes_displayed,
|
||||
attributes_ranked,
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
}
|
||||
|
||||
pub async fn update_attributes(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let settings: AttributesSettings = ctx.body_json().await
|
||||
.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
attribute_identifier: settings.attribute_identifier,
|
||||
attributes_searchable: settings.attributes_searchable,
|
||||
attributes_displayed: settings.attributes_displayed,
|
||||
attributes_ranked: settings.attributes_ranked,
|
||||
.. Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
pub async fn delete_attributes(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
attributes_searchable: UpdateState::Clear,
|
||||
attributes_displayed: UpdateState::Clear,
|
||||
.. SettingsUpdate::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
pub struct AttributesIdentifierSettings {
|
||||
pub attribute_identifier: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn get_identifier(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsRead)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let schema = index.main.schema(&reader)?;
|
||||
|
||||
let attribute_identifier = schema.map(|s| s.identifier());
|
||||
|
||||
let settings = AttributesIdentifierSettings {
|
||||
attribute_identifier,
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
pub struct AttributesSearchableSettings {
|
||||
pub attributes_searchable: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsRead)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let schema = index.main.schema(&reader)?;
|
||||
|
||||
let attributes_searchable = schema.map(|s| s.get_indexed_name());
|
||||
|
||||
let settings = AttributesSearchableSettings {
|
||||
attributes_searchable,
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
}
|
||||
|
||||
pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let settings: AttributesSearchableSettings = ctx.body_json().await
|
||||
.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
attributes_searchable: settings.attributes_searchable,
|
||||
.. Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
pub async fn delete_searchable(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
attributes_searchable: UpdateState::Clear,
|
||||
.. SettingsUpdate::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
pub struct AttributesDisplayedSettings {
|
||||
pub attributes_displayed: Option<HashSet<String>>,
|
||||
}
|
||||
|
||||
pub async fn get_displayed(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsRead)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let schema = index.main.schema(&reader)?;
|
||||
|
||||
let attributes_displayed = schema.map(|s| s.get_displayed_name());
|
||||
|
||||
let settings = AttributesDisplayedSettings {
|
||||
attributes_displayed,
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
}
|
||||
|
||||
pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let settings: AttributesDisplayedSettings = ctx.body_json().await
|
||||
.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
attributes_displayed: settings.attributes_displayed,
|
||||
.. Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
||||
pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(SettingsWrite)?;
|
||||
let index = ctx.index()?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = SettingsUpdate {
|
||||
attributes_displayed: UpdateState::Clear,
|
||||
.. SettingsUpdate::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings)?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
||||
}
|
||||
|
@ -17,14 +17,10 @@ pub async fn get(ctx: Request<Data>) -> SResult<Response> {
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn()?;
|
||||
|
||||
let synonyms_fst = index
|
||||
.main
|
||||
.synonyms_fst(&reader)?;
|
||||
|
||||
let synonyms_fst = synonyms_fst.unwrap_or_default();
|
||||
let synonyms_fst = index.main.synonyms_fst(&reader)?.unwrap_or_default();
|
||||
let synonyms_list = synonyms_fst.stream().into_strs()?;
|
||||
|
||||
let mut response = IndexMap::new();
|
||||
let mut synonyms = IndexMap::new();
|
||||
|
||||
let index_synonyms = &index.synonyms;
|
||||
|
||||
@ -33,11 +29,11 @@ pub async fn get(ctx: Request<Data>) -> SResult<Response> {
|
||||
|
||||
if let Some(list) = alternative_list {
|
||||
let list = list.stream().into_strs()?;
|
||||
response.insert(synonym, list);
|
||||
synonyms.insert(synonym, list);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&response).unwrap())
|
||||
Ok(tide::Response::new(200).body_json(&synonyms).unwrap())
|
||||
}
|
||||
|
||||
pub async fn update(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
|
@ -49,10 +49,7 @@ impl Schema {
|
||||
}
|
||||
|
||||
pub fn contains<S: Into<String>>(&self, name: S) -> bool {
|
||||
match self.fields_map.get_id(name.into()) {
|
||||
Some(_) => true,
|
||||
None => false,
|
||||
}
|
||||
self.fields_map.get_id(name.into()).is_some()
|
||||
}
|
||||
|
||||
pub fn get_or_create_empty<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user