mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 10:37:41 +08:00
fix some uses cases on settings
This commit is contained in:
parent
5c49f08bb2
commit
50a9825a0f
@ -2,7 +2,7 @@ use std::sync::Mutex;
|
||||
use std::collections::{BTreeMap, BTreeSet, HashSet};
|
||||
use std::str::FromStr;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
static RANKING_RULE_REGEX: Lazy<Mutex<regex::Regex>> = Lazy::new(|| {
|
||||
@ -14,36 +14,30 @@ static RANKING_RULE_REGEX: Lazy<Mutex<regex::Regex>> = Lazy::new(|| {
|
||||
#[derive(Default, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct Settings {
|
||||
pub ranking_rules: Option<Vec<String>>,
|
||||
pub ranking_distinct: Option<String>,
|
||||
pub attribute_identifier: Option<String>,
|
||||
pub attributes_searchable: Option<Vec<String>>,
|
||||
pub attributes_displayed: Option<HashSet<String>>,
|
||||
pub stop_words: Option<BTreeSet<String>>,
|
||||
pub synonyms: Option<BTreeMap<String, Vec<String>>>,
|
||||
pub index_new_fields: Option<bool>,
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub ranking_rules: Option<Option<Vec<String>>>,
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub ranking_distinct: Option<Option<String>>,
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub attribute_identifier: Option<Option<String>>,
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub attributes_searchable: Option<Option<Vec<String>>>,
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub attributes_displayed: Option<Option<HashSet<String>>>,
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub stop_words: Option<Option<BTreeSet<String>>>,
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub synonyms: Option<Option<BTreeMap<String, Vec<String>>>>,
|
||||
#[serde(default, deserialize_with = "deserialize_some")]
|
||||
pub index_new_fields: Option<Option<bool>>,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn into_cleared(self) -> SettingsUpdate {
|
||||
let settings = self.clone();
|
||||
|
||||
let ranking_rules = match settings.ranking_rules {
|
||||
Some(rules) => Some(RankingRule::from_vec(rules)),
|
||||
None => None
|
||||
};
|
||||
|
||||
SettingsUpdate {
|
||||
ranking_rules: UpdateState::convert_with_default(ranking_rules, UpdateState::Clear),
|
||||
ranking_distinct: UpdateState::convert_with_default(settings.ranking_distinct, UpdateState::Clear),
|
||||
attribute_identifier: UpdateState::convert_with_default(settings.attribute_identifier, UpdateState::Clear),
|
||||
attributes_searchable: UpdateState::convert_with_default(settings.attributes_searchable, UpdateState::Clear),
|
||||
attributes_displayed: UpdateState::convert_with_default(settings.attributes_displayed, UpdateState::Clear),
|
||||
stop_words: UpdateState::convert_with_default(settings.stop_words, UpdateState::Clear),
|
||||
synonyms: UpdateState::convert_with_default(settings.synonyms, UpdateState::Clear),
|
||||
index_new_fields: UpdateState::convert_with_default(settings.index_new_fields, UpdateState::Clear),
|
||||
}
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
|
||||
impl Into<SettingsUpdate> for Settings {
|
||||
@ -51,12 +45,13 @@ impl Into<SettingsUpdate> for Settings {
|
||||
let settings = self.clone();
|
||||
|
||||
let ranking_rules = match settings.ranking_rules {
|
||||
Some(rules) => Some(RankingRule::from_vec(rules)),
|
||||
None => None,
|
||||
Some(Some(rules)) => UpdateState::Update(RankingRule::from_vec(rules)),
|
||||
Some(None) => UpdateState::Clear,
|
||||
None => UpdateState::Nothing,
|
||||
};
|
||||
|
||||
SettingsUpdate {
|
||||
ranking_rules: ranking_rules.into(),
|
||||
ranking_rules: ranking_rules,
|
||||
ranking_distinct: settings.ranking_distinct.into(),
|
||||
attribute_identifier: settings.attribute_identifier.into(),
|
||||
attributes_searchable: settings.attributes_searchable.into(),
|
||||
@ -77,10 +72,11 @@ pub enum UpdateState<T> {
|
||||
Nothing,
|
||||
}
|
||||
|
||||
impl <T> From<Option<T>> for UpdateState<T> {
|
||||
fn from(opt: Option<T>) -> UpdateState<T> {
|
||||
impl <T> From<Option<Option<T>>> for UpdateState<T> {
|
||||
fn from(opt: Option<Option<T>>) -> UpdateState<T> {
|
||||
match opt {
|
||||
Some(t) => UpdateState::Update(t),
|
||||
Some(Some(t)) => UpdateState::Update(t),
|
||||
Some(None) => UpdateState::Clear,
|
||||
None => UpdateState::Nothing,
|
||||
}
|
||||
}
|
||||
@ -93,13 +89,6 @@ impl<T> UpdateState<T> {
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_with_default(opt: Option<T>, default: UpdateState<T>) -> UpdateState<T> {
|
||||
match opt {
|
||||
Some(t) => UpdateState::Update(t),
|
||||
None => default,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -249,8 +249,8 @@ mod test {
|
||||
|
||||
const ENDPOINT: &str = "/cors";
|
||||
|
||||
fn app() -> crate::Server<()> {
|
||||
let mut app = crate::Server::new();
|
||||
fn app() -> tide::Server<()> {
|
||||
let mut app = tide::Server::new();
|
||||
app.at(ENDPOINT).get(|_| async move { "Hello World" });
|
||||
|
||||
app
|
||||
|
@ -146,7 +146,7 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
|
||||
},
|
||||
};
|
||||
let settings = Settings {
|
||||
attribute_identifier: Some(id),
|
||||
attribute_identifier: Some(Some(id)),
|
||||
..Settings::default()
|
||||
};
|
||||
index.settings_update(&mut update_writer, settings.into())?;
|
||||
|
@ -1,6 +1,7 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::error;
|
||||
use meilisearch_core::ProcessedUpdateResult;
|
||||
use meilisearch_schema::Schema;
|
||||
use rand::seq::SliceRandom;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
@ -94,6 +95,7 @@ pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
|
||||
struct IndexCreateRequest {
|
||||
name: Option<String>,
|
||||
uid: Option<String>,
|
||||
attribute_identifier: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@ -148,6 +150,10 @@ pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
.updated_at(&writer)?
|
||||
.into_internal_error()?;
|
||||
|
||||
if let Some(id) = body.attribute_identifier {
|
||||
created_index.main.put_schema(&mut writer, &Schema::with_identifier(id))?;
|
||||
}
|
||||
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexCreateResponse {
|
||||
|
@ -60,27 +60,52 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
|
||||
let index_new_fields = schema.map(|s| s.must_index_new_fields());
|
||||
|
||||
let settings = Settings {
|
||||
ranking_rules,
|
||||
ranking_distinct,
|
||||
attribute_identifier,
|
||||
attributes_searchable,
|
||||
attributes_displayed,
|
||||
stop_words,
|
||||
synonyms,
|
||||
index_new_fields,
|
||||
ranking_rules: Some(ranking_rules),
|
||||
ranking_distinct: Some(ranking_distinct),
|
||||
attribute_identifier: Some(attribute_identifier),
|
||||
attributes_searchable: Some(attributes_searchable),
|
||||
attributes_displayed: Some(attributes_displayed),
|
||||
stop_words: Some(stop_words),
|
||||
synonyms: Some(synonyms),
|
||||
index_new_fields: Some(index_new_fields),
|
||||
};
|
||||
|
||||
Ok(tide::Response::new(200).body_json(&settings).unwrap())
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct UpdateSettings {
|
||||
pub ranking_rules: Option<Vec<String>>,
|
||||
pub ranking_distinct: Option<String>,
|
||||
pub attribute_identifier: Option<String>,
|
||||
pub attributes_searchable: Option<Vec<String>>,
|
||||
pub attributes_displayed: Option<HashSet<String>>,
|
||||
pub stop_words: Option<BTreeSet<String>>,
|
||||
pub synonyms: Option<BTreeMap<String, Vec<String>>>,
|
||||
pub index_new_fields: Option<bool>,
|
||||
}
|
||||
|
||||
|
||||
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 settings_update: UpdateSettings = ctx.body_json().await.map_err(ResponseError::bad_request)?;
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
ranking_rules: Some(settings_update.ranking_rules),
|
||||
ranking_distinct: Some(settings_update.ranking_distinct),
|
||||
attribute_identifier: Some(settings_update.attribute_identifier),
|
||||
attributes_searchable: Some(settings_update.attributes_searchable),
|
||||
attributes_displayed: Some(settings_update.attributes_displayed),
|
||||
stop_words: Some(settings_update.stop_words),
|
||||
synonyms: Some(settings_update.synonyms),
|
||||
index_new_fields: Some(settings_update.index_new_fields),
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into_cleared())?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
@ -153,13 +178,13 @@ pub async fn update_ranking(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
ranking_rules: settings.ranking_rules,
|
||||
ranking_distinct: settings.ranking_distinct,
|
||||
ranking_rules: Some(settings.ranking_rules),
|
||||
ranking_distinct: Some(settings.ranking_distinct),
|
||||
..Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into_cleared())?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
@ -208,12 +233,12 @@ pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
ranking_rules,
|
||||
ranking_rules: Some(ranking_rules),
|
||||
..Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into_cleared())?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
@ -272,12 +297,12 @@ pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
ranking_distinct,
|
||||
ranking_distinct: Some(ranking_distinct),
|
||||
..Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into_cleared())?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
@ -348,14 +373,14 @@ pub async fn update_attributes(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
attribute_identifier: settings.attribute_identifier,
|
||||
attributes_searchable: settings.attributes_searchable,
|
||||
attributes_displayed: settings.attributes_displayed,
|
||||
attribute_identifier: Some(settings.attribute_identifier),
|
||||
attributes_searchable: Some(settings.attributes_searchable),
|
||||
attributes_displayed: Some(settings.attributes_displayed),
|
||||
..Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into_cleared())?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
@ -425,12 +450,12 @@ pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
attributes_searchable,
|
||||
attributes_searchable: Some(attributes_searchable),
|
||||
..Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into_cleared())?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
@ -478,12 +503,12 @@ pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> {
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
attributes_displayed,
|
||||
attributes_displayed: Some(attributes_displayed),
|
||||
..Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into_cleared())?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
@ -531,12 +556,12 @@ pub async fn update_index_new_fields(mut ctx: Request<Data>) -> SResult<Response
|
||||
let db = &ctx.state().db;
|
||||
|
||||
let settings = Settings {
|
||||
index_new_fields,
|
||||
index_new_fields: Some(index_new_fields),
|
||||
..Settings::default()
|
||||
};
|
||||
|
||||
let mut writer = db.update_write_txn()?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into_cleared())?;
|
||||
let update_id = index.settings_update(&mut writer, settings.into())?;
|
||||
writer.commit()?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
|
Loading…
Reference in New Issue
Block a user