2020-01-16 19:19:44 +01:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashSet};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-01-15 17:10:33 +01:00
|
|
|
use tide::{Request, Response};
|
2020-01-20 09:52:24 +01:00
|
|
|
use meilisearch_core::settings::{SettingsUpdate, UpdateState, Settings, SettingsComplete};
|
2019-10-31 15:00:36 +01:00
|
|
|
|
|
|
|
use crate::error::{ResponseError, SResult};
|
2020-01-15 17:10:33 +01:00
|
|
|
use crate::helpers::tide::RequestExt;
|
2019-10-31 15:00:36 +01:00
|
|
|
use crate::models::token::ACL::*;
|
|
|
|
use crate::routes::document::IndexUpdateResponse;
|
|
|
|
use crate::Data;
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
|
|
|
|
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 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 settings = Settings {
|
|
|
|
ranking_rules,
|
|
|
|
ranking_distinct,
|
|
|
|
attribute_identifier,
|
|
|
|
attributes_searchable,
|
|
|
|
attributes_displayed,
|
|
|
|
stop_words,
|
|
|
|
synonyms,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> {
|
|
|
|
ctx.is_allowed(SettingsWrite)?;
|
|
|
|
let index = ctx.index()?;
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings: SettingsComplete = ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
2020-01-16 19:19:44 +01:00
|
|
|
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())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
pub async fn delete_all(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,
|
|
|
|
attribute_identifier: UpdateState::Clear,
|
|
|
|
attributes_searchable: UpdateState::Clear,
|
|
|
|
attributes_displayed: UpdateState::Clear,
|
|
|
|
stop_words: UpdateState::Clear,
|
|
|
|
synonyms: UpdateState::Clear,
|
|
|
|
};
|
|
|
|
|
|
|
|
let update_id = index.settings_update(&mut writer, settings)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
writer.commit()?;
|
|
|
|
|
|
|
|
let response_body = IndexUpdateResponse { update_id };
|
|
|
|
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Clone, Serialize, Deserialize)]
|
2020-01-20 09:52:24 +01:00
|
|
|
pub struct GetRankingSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
pub ranking_rules: Option<Vec<String>>,
|
|
|
|
pub ranking_distinct: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_ranking(ctx: Request<Data>) -> SResult<Response> {
|
2019-10-31 15:00:36 +01:00
|
|
|
ctx.is_allowed(SettingsRead)?;
|
|
|
|
let index = ctx.index()?;
|
2020-01-16 19:19:44 +01:00
|
|
|
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,
|
|
|
|
};
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
let ranking_distinct = index.main.ranking_distinct(&reader)?;
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = GetRankingSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
ranking_rules,
|
|
|
|
ranking_distinct,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
|
|
|
}
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
#[derive(Default, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct SetRankingSettings {
|
|
|
|
pub ranking_rules: Option<Option<Vec<String>>>,
|
|
|
|
pub ranking_distinct: Option<Option<String>>,
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
pub async fn update_ranking(mut ctx: Request<Data>) -> SResult<Response> {
|
|
|
|
ctx.is_allowed(SettingsWrite)?;
|
|
|
|
let index = ctx.index()?;
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings: SetRankingSettings = ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
2020-01-16 19:19:44 +01:00
|
|
|
let db = &ctx.state().db;
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = SettingsComplete {
|
2020-01-16 19:19:44 +01:00
|
|
|
ranking_rules: settings.ranking_rules,
|
|
|
|
ranking_distinct: settings.ranking_distinct,
|
2020-01-20 09:52:24 +01:00
|
|
|
.. SettingsComplete::default()
|
2020-01-16 19:19:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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)]
|
2020-01-20 09:52:24 +01:00
|
|
|
pub struct GetRankingRulesSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = GetRankingRulesSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
ranking_rules,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
|
|
|
}
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
#[derive(Default, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct SetRankingRulesSettings {
|
|
|
|
pub ranking_rules: Option<Option<Vec<String>>>,
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> {
|
|
|
|
ctx.is_allowed(SettingsWrite)?;
|
|
|
|
let index = ctx.index()?;
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings: SetRankingRulesSettings = ctx.body_json().await
|
2020-01-16 19:19:44 +01:00
|
|
|
.map_err(ResponseError::bad_request)?;
|
|
|
|
let db = &ctx.state().db;
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = SettingsComplete {
|
2020-01-16 19:19:44 +01:00
|
|
|
ranking_rules: settings.ranking_rules,
|
2020-01-20 09:52:24 +01:00
|
|
|
.. SettingsComplete::default()
|
2020-01-16 19:19:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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)]
|
2020-01-20 09:52:24 +01:00
|
|
|
pub struct GetRankingDistinctSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
pub ranking_distinct: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_distinct(ctx: Request<Data>) -> SResult<Response> {
|
|
|
|
ctx.is_allowed(SettingsRead)?;
|
|
|
|
let index = ctx.index()?;
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
let reader = db.main_read_txn()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
let ranking_distinct = index.main.ranking_distinct(&reader)?;
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = GetRankingDistinctSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
ranking_distinct,
|
2019-10-31 15:00:36 +01:00
|
|
|
};
|
|
|
|
|
2020-01-15 17:10:33 +01:00
|
|
|
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
#[derive(Default, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct SetRankingDistinctSettings {
|
|
|
|
pub ranking_distinct: Option<Option<String>>,
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
|
|
|
|
ctx.is_allowed(SettingsWrite)?;
|
|
|
|
let index = ctx.index()?;
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings: SetRankingDistinctSettings = ctx.body_json().await
|
2020-01-16 19:19:44 +01:00
|
|
|
.map_err(ResponseError::bad_request)?;
|
|
|
|
let db = &ctx.state().db;
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = SettingsComplete {
|
2020-01-16 19:19:44 +01:00
|
|
|
ranking_distinct: settings.ranking_distinct,
|
2020-01-20 09:52:24 +01:00
|
|
|
.. SettingsComplete::default()
|
2020-01-16 19:19:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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)]
|
2020-01-20 09:52:24 +01:00
|
|
|
pub struct GetAttributesSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
pub attribute_identifier: Option<String>,
|
|
|
|
pub attributes_searchable: Option<Vec<String>>,
|
|
|
|
pub attributes_displayed: Option<HashSet<String>>,
|
2019-12-12 19:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
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());
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = GetAttributesSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
attribute_identifier,
|
|
|
|
attributes_searchable,
|
|
|
|
attributes_displayed,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
2019-12-12 19:10:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
#[derive(Default, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct SetAttributesSettings {
|
|
|
|
pub attribute_identifier: Option<Option<String>>,
|
|
|
|
pub attributes_searchable: Option<Option<Vec<String>>>,
|
|
|
|
pub attributes_displayed: Option<Option<HashSet<String>>>,
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
pub async fn update_attributes(mut ctx: Request<Data>) -> SResult<Response> {
|
2019-10-31 15:00:36 +01:00
|
|
|
ctx.is_allowed(SettingsWrite)?;
|
2020-01-16 19:19:44 +01:00
|
|
|
let index = ctx.index()?;
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings: SetAttributesSettings = ctx.body_json().await
|
2020-01-16 19:19:44 +01:00
|
|
|
.map_err(ResponseError::bad_request)?;
|
|
|
|
let db = &ctx.state().db;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = SettingsComplete {
|
2020-01-16 19:19:44 +01:00
|
|
|
attribute_identifier: settings.attribute_identifier,
|
|
|
|
attributes_searchable: settings.attributes_searchable,
|
|
|
|
attributes_displayed: settings.attributes_displayed,
|
2020-01-20 09:52:24 +01:00
|
|
|
.. SettingsComplete::default()
|
2020-01-16 19:19:44 +01:00
|
|
|
};
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
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)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
let index = ctx.index()?;
|
2020-01-16 19:19:44 +01:00
|
|
|
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()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
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()?;
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &ctx.state().db;
|
2020-01-16 16:58:57 +01:00
|
|
|
let reader = db.main_read_txn()?;
|
2020-01-16 19:19:44 +01:00
|
|
|
|
|
|
|
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)]
|
2020-01-20 09:52:24 +01:00
|
|
|
pub struct GetAttributesSearchableSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
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());
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = GetAttributesSearchableSettings {
|
2020-01-16 19:19:44 +01:00
|
|
|
attributes_searchable,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
|
|
|
}
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
#[derive(Default, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct SetAttributesSearchableSettings {
|
|
|
|
pub attributes_searchable: Option<Option<Vec<String>>>,
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> {
|
|
|
|
ctx.is_allowed(SettingsWrite)?;
|
|
|
|
let index = ctx.index()?;
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings: SetAttributesSearchableSettings = ctx.body_json().await
|
2020-01-16 19:19:44 +01:00
|
|
|
.map_err(ResponseError::bad_request)?;
|
|
|
|
let db = &ctx.state().db;
|
|
|
|
|
2020-01-20 09:52:24 +01:00
|
|
|
let settings = SettingsComplete {
|
2020-01-16 19:19:44 +01:00
|
|
|
attributes_searchable: settings.attributes_searchable,
|
2020-01-20 09:52:24 +01:00
|
|
|
.. SettingsComplete::default()
|
2020-01-16 19:19:44 +01:00
|
|
|
};
|
|
|
|
|
2020-01-16 16:58:57 +01:00
|
|
|
let mut writer = db.update_write_txn()?;
|
2020-01-16 19:19:44 +01:00
|
|
|
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;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
let settings = SettingsUpdate {
|
|
|
|
attributes_searchable: UpdateState::Clear,
|
|
|
|
.. SettingsUpdate::default()
|
2019-10-31 15:00:36 +01:00
|
|
|
};
|
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
let mut writer = db.update_write_txn()?;
|
|
|
|
let update_id = index.settings_update(&mut writer, settings)?;
|
|
|
|
writer.commit()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
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)?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
let attributes_displayed = schema.map(|s| s.get_displayed_name());
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
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;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
2020-01-16 19:19:44 +01:00
|
|
|
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)?;
|
2020-01-16 16:58:57 +01:00
|
|
|
writer.commit()?;
|
2019-10-31 15:00:36 +01:00
|
|
|
|
|
|
|
let response_body = IndexUpdateResponse { update_id };
|
2020-01-15 17:10:33 +01:00
|
|
|
Ok(tide::Response::new(202).body_json(&response_body).unwrap())
|
2019-10-31 15:00:36 +01:00
|
|
|
}
|