facet settings

This commit is contained in:
mpostma 2020-05-05 22:27:06 +02:00
parent 59c67f6bc8
commit 270c7b0288
4 changed files with 54 additions and 1 deletions

View File

@ -31,6 +31,8 @@ pub struct Settings {
pub synonyms: Option<Option<BTreeMap<String, Vec<String>>>>, pub synonyms: Option<Option<BTreeMap<String, Vec<String>>>>,
#[serde(default, deserialize_with = "deserialize_some")] #[serde(default, deserialize_with = "deserialize_some")]
pub accept_new_fields: Option<Option<bool>>, pub accept_new_fields: Option<Option<bool>>,
#[serde(default, deserialize_with = "deserialize_some")]
pub attributes_for_faceting: Option<Option<Vec<String>>>,
} }
// Any value that is present is considered Some value, including null. // Any value that is present is considered Some value, including null.
@ -60,6 +62,7 @@ impl Settings {
stop_words: settings.stop_words.into(), stop_words: settings.stop_words.into(),
synonyms: settings.synonyms.into(), synonyms: settings.synonyms.into(),
accept_new_fields: settings.accept_new_fields.into(), accept_new_fields: settings.accept_new_fields.into(),
attributes_for_faceting: settings.attributes_for_faceting.into(),
}) })
} }
} }
@ -166,6 +169,7 @@ pub struct SettingsUpdate {
pub stop_words: UpdateState<BTreeSet<String>>, pub stop_words: UpdateState<BTreeSet<String>>,
pub synonyms: UpdateState<BTreeMap<String, Vec<String>>>, pub synonyms: UpdateState<BTreeMap<String, Vec<String>>>,
pub accept_new_fields: UpdateState<bool>, pub accept_new_fields: UpdateState<bool>,
pub attributes_for_faceting: UpdateState<Vec<String>>,
} }
impl Default for SettingsUpdate { impl Default for SettingsUpdate {
@ -179,6 +183,7 @@ impl Default for SettingsUpdate {
stop_words: UpdateState::Nothing, stop_words: UpdateState::Nothing,
synonyms: UpdateState::Nothing, synonyms: UpdateState::Nothing,
accept_new_fields: UpdateState::Nothing, accept_new_fields: UpdateState::Nothing,
attributes_for_faceting: UpdateState::Nothing,
} }
} }
} }

View File

@ -102,6 +102,18 @@ pub fn apply_settings_update(
UpdateState::Nothing => (), UpdateState::Nothing => (),
} }
match settings.attributes_for_faceting {
UpdateState::Update(attrs) => {
apply_attributes_for_faceting_update(writer, index, &mut schema, &attrs)?;
must_reindex = true;
},
UpdateState::Clear => {
index.main.delete_attributes_for_faceting(writer)?;
index.facets.clear(writer)?;
},
UpdateState::Nothing => (),
}
index.main.put_schema(writer, &schema)?; index.main.put_schema(writer, &schema)?;
match settings.stop_words { match settings.stop_words {
@ -131,6 +143,21 @@ pub fn apply_settings_update(
Ok(()) Ok(())
} }
fn apply_attributes_for_faceting_update(
writer: &mut heed::RwTxn<MainT>,
index: &store::Index,
schema: &mut Schema,
attributes: &[String]
) -> MResult<()> {
let mut attribute_ids = Vec::new();
for name in attributes {
attribute_ids.push(schema.insert(name)?);
}
let attributes_for_faceting = SetBuf::from_dirty(attribute_ids);
index.main.put_attributes_for_faceting(writer, &attributes_for_faceting)?;
Ok(())
}
pub fn apply_stop_words_update( pub fn apply_stop_words_update(
writer: &mut heed::RwTxn<MainT>, writer: &mut heed::RwTxn<MainT>,
index: &store::Index, index: &store::Index,

View File

@ -91,6 +91,17 @@ async fn get_all(
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let attributes_for_faceting = match (&schema, &index.main.attributes_for_faceting(&reader)?) {
(Some(schema), Some(attrs)) => {
Some(attrs
.iter()
.filter_map(|&id| schema .name(id))
.map(str::to_string)
.collect())
}
_ => None,
};
let searchable_attributes = schema.clone().map(|s| { let searchable_attributes = schema.clone().map(|s| {
s.indexed_name() s.indexed_name()
.iter() .iter()
@ -115,6 +126,7 @@ async fn get_all(
stop_words: Some(Some(stop_words)), stop_words: Some(Some(stop_words)),
synonyms: Some(Some(synonyms)), synonyms: Some(Some(synonyms)),
accept_new_fields: Some(accept_new_fields), accept_new_fields: Some(accept_new_fields),
attributes_for_faceting: Some(attributes_for_faceting),
}; };
Ok(HttpResponse::Ok().json(settings)) Ok(HttpResponse::Ok().json(settings))
@ -140,6 +152,7 @@ async fn delete_all(
stop_words: UpdateState::Clear, stop_words: UpdateState::Clear,
synonyms: UpdateState::Clear, synonyms: UpdateState::Clear,
accept_new_fields: UpdateState::Clear, accept_new_fields: UpdateState::Clear,
attributes_for_faceting: UpdateState::Clear,
}; };
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;

View File

@ -8,7 +8,6 @@ mod common;
async fn write_all_and_delete() { async fn write_all_and_delete() {
let mut server = common::Server::with_uid("movies"); let mut server = common::Server::with_uid("movies");
server.populate_movies().await; server.populate_movies().await;
// 2 - Send the settings // 2 - Send the settings
let body = json!({ let body = json!({
@ -48,9 +47,11 @@ async fn write_all_and_delete() {
"wolverine": ["xmen", "logan"], "wolverine": ["xmen", "logan"],
"logan": ["wolverine"], "logan": ["wolverine"],
}, },
"attributesForFaceting": ["title"],
"acceptNewFields": false, "acceptNewFields": false,
}); });
server.update_all_settings(body.clone()).await; server.update_all_settings(body.clone()).await;
// 3 - Get all settings and compare to the previous one // 3 - Get all settings and compare to the previous one
@ -119,6 +120,7 @@ async fn write_all_and_delete() {
], ],
"stopWords": [], "stopWords": [],
"synonyms": {}, "synonyms": {},
"attributesForFaceting": null,
"acceptNewFields": true, "acceptNewFields": true,
}); });
@ -169,6 +171,7 @@ async fn write_all_and_update() {
"wolverine": ["xmen", "logan"], "wolverine": ["xmen", "logan"],
"logan": ["wolverine"], "logan": ["wolverine"],
}, },
"attributesForFaceting": ["title"],
"acceptNewFields": false, "acceptNewFields": false,
}); });
@ -210,6 +213,7 @@ async fn write_all_and_update() {
"wolverine": ["xmen", "logan"], "wolverine": ["xmen", "logan"],
"logan": ["wolverine", "xmen"], "logan": ["wolverine", "xmen"],
}, },
"attributesForFaceting": ["title"],
"acceptNewFields": false, "acceptNewFields": false,
}); });
@ -247,6 +251,7 @@ async fn write_all_and_update() {
"wolverine": ["xmen", "logan"], "wolverine": ["xmen", "logan"],
"logan": ["wolverine", "xmen"], "logan": ["wolverine", "xmen"],
}, },
"attributesForFaceting": ["title"],
"acceptNewFields": false "acceptNewFields": false
}); });
@ -277,6 +282,7 @@ async fn test_default_settings() {
"displayedAttributes": [], "displayedAttributes": [],
"stopWords": [], "stopWords": [],
"synonyms": {}, "synonyms": {},
"attributesForFaceting": null,
"acceptNewFields": true, "acceptNewFields": true,
}); });
@ -314,6 +320,7 @@ async fn test_default_settings_2() {
], ],
"stopWords": [], "stopWords": [],
"synonyms": {}, "synonyms": {},
"attributesForFaceting": null,
"acceptNewFields": true, "acceptNewFields": true,
}); });
@ -421,6 +428,7 @@ async fn write_setting_and_update_partial() {
"wolverine": ["xmen", "logan"], "wolverine": ["xmen", "logan"],
"logan": ["wolverine"], "logan": ["wolverine"],
}, },
"attributesForFaceting": null,
"acceptNewFields": false, "acceptNewFields": false,
}); });