2021-06-15 17:39:07 +02:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashSet};
|
2021-03-04 11:56:32 +01:00
|
|
|
use std::io;
|
2021-05-10 17:30:09 +02:00
|
|
|
use std::marker::PhantomData;
|
2021-05-12 17:04:24 +02:00
|
|
|
use std::num::NonZeroUsize;
|
2021-03-04 11:56:32 +01:00
|
|
|
|
|
|
|
use flate2::read::GzDecoder;
|
|
|
|
use log::info;
|
2021-05-10 20:24:14 +02:00
|
|
|
use milli::update::{IndexDocumentsMethod, UpdateBuilder, UpdateFormat};
|
2021-05-12 17:04:24 +02:00
|
|
|
use serde::{Deserialize, Serialize, Serializer};
|
2021-03-04 11:56:32 +01:00
|
|
|
|
2021-05-10 20:24:14 +02:00
|
|
|
use crate::index_controller::UpdateResult;
|
2021-05-10 20:22:18 +02:00
|
|
|
|
2021-06-14 21:26:35 +02:00
|
|
|
use super::error::Result;
|
2021-05-10 20:48:06 +02:00
|
|
|
use super::{deserialize_some, Index};
|
|
|
|
|
2021-06-14 21:26:35 +02:00
|
|
|
fn serialize_with_wildcard<S>(
|
|
|
|
field: &Option<Option<Vec<String>>>,
|
|
|
|
s: S,
|
|
|
|
) -> std::result::Result<S::Ok, S::Error>
|
2021-05-12 17:04:24 +02:00
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let wildcard = vec!["*".to_string()];
|
|
|
|
s.serialize_some(&field.as_ref().map(|o| o.as_ref().unwrap_or(&wildcard)))
|
|
|
|
}
|
2021-03-04 11:56:32 +01:00
|
|
|
|
2021-06-01 20:15:51 +02:00
|
|
|
#[derive(Clone, Default, Debug, Serialize)]
|
2021-05-10 17:30:09 +02:00
|
|
|
pub struct Checked;
|
2021-06-01 20:15:51 +02:00
|
|
|
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
|
2021-05-10 17:30:09 +02:00
|
|
|
pub struct Unchecked;
|
|
|
|
|
2021-03-04 11:56:32 +01:00
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-06-01 20:15:51 +02:00
|
|
|
#[serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'static>"))]
|
2021-05-10 17:30:09 +02:00
|
|
|
pub struct Settings<T> {
|
2021-03-04 11:56:32 +01:00
|
|
|
#[serde(
|
|
|
|
default,
|
2021-05-10 20:20:36 +02:00
|
|
|
deserialize_with = "deserialize_some",
|
2021-05-12 17:04:24 +02:00
|
|
|
serialize_with = "serialize_with_wildcard",
|
2021-03-15 18:11:10 +01:00
|
|
|
skip_serializing_if = "Option::is_none"
|
2021-03-04 11:56:32 +01:00
|
|
|
)]
|
|
|
|
pub displayed_attributes: Option<Option<Vec<String>>>,
|
|
|
|
|
|
|
|
#[serde(
|
|
|
|
default,
|
2021-05-10 20:20:36 +02:00
|
|
|
deserialize_with = "deserialize_some",
|
2021-05-12 17:04:24 +02:00
|
|
|
serialize_with = "serialize_with_wildcard",
|
2021-03-15 18:11:10 +01:00
|
|
|
skip_serializing_if = "Option::is_none"
|
2021-03-04 11:56:32 +01:00
|
|
|
)]
|
|
|
|
pub searchable_attributes: Option<Option<Vec<String>>>,
|
|
|
|
|
2021-05-10 20:48:06 +02:00
|
|
|
#[serde(
|
|
|
|
default,
|
|
|
|
deserialize_with = "deserialize_some",
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
)]
|
2021-06-03 23:47:16 +02:00
|
|
|
pub filterable_attributes: Option<Option<HashSet<String>>>,
|
2021-03-04 11:56:32 +01:00
|
|
|
|
|
|
|
#[serde(
|
|
|
|
default,
|
|
|
|
deserialize_with = "deserialize_some",
|
2021-03-15 18:11:10 +01:00
|
|
|
skip_serializing_if = "Option::is_none"
|
2021-03-04 11:56:32 +01:00
|
|
|
)]
|
2021-03-11 22:39:16 +01:00
|
|
|
pub ranking_rules: Option<Option<Vec<String>>>,
|
2021-04-06 15:41:03 +02:00
|
|
|
#[serde(
|
|
|
|
default,
|
|
|
|
deserialize_with = "deserialize_some",
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
)]
|
|
|
|
pub stop_words: Option<Option<BTreeSet<String>>>,
|
2021-03-29 09:22:36 +02:00
|
|
|
#[serde(
|
|
|
|
default,
|
|
|
|
deserialize_with = "deserialize_some",
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
)]
|
2021-06-03 14:19:56 +02:00
|
|
|
pub synonyms: Option<Option<BTreeMap<String, Vec<String>>>>,
|
|
|
|
#[serde(
|
|
|
|
default,
|
|
|
|
deserialize_with = "deserialize_some",
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
)]
|
2021-03-29 09:22:36 +02:00
|
|
|
pub distinct_attribute: Option<Option<String>>,
|
2021-05-10 17:30:09 +02:00
|
|
|
|
|
|
|
#[serde(skip)]
|
|
|
|
pub _kind: PhantomData<T>,
|
2021-03-04 11:56:32 +01:00
|
|
|
}
|
|
|
|
|
2021-05-10 17:30:09 +02:00
|
|
|
impl Settings<Checked> {
|
|
|
|
pub fn cleared() -> Settings<Checked> {
|
|
|
|
Settings {
|
2021-03-04 11:56:32 +01:00
|
|
|
displayed_attributes: Some(None),
|
|
|
|
searchable_attributes: Some(None),
|
2021-06-03 23:47:16 +02:00
|
|
|
filterable_attributes: Some(None),
|
2021-03-11 22:39:16 +01:00
|
|
|
ranking_rules: Some(None),
|
2021-04-06 15:41:03 +02:00
|
|
|
stop_words: Some(None),
|
2021-06-03 14:19:56 +02:00
|
|
|
synonyms: Some(None),
|
2021-03-29 09:22:36 +02:00
|
|
|
distinct_attribute: Some(None),
|
2021-05-10 17:30:09 +02:00
|
|
|
_kind: PhantomData,
|
2021-03-04 11:56:32 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-27 14:30:20 +02:00
|
|
|
|
|
|
|
pub fn into_unchecked(self) -> Settings<Unchecked> {
|
|
|
|
let Self {
|
|
|
|
displayed_attributes,
|
|
|
|
searchable_attributes,
|
2021-06-03 23:47:16 +02:00
|
|
|
filterable_attributes,
|
2021-05-27 14:30:20 +02:00
|
|
|
ranking_rules,
|
|
|
|
stop_words,
|
2021-06-03 14:19:56 +02:00
|
|
|
synonyms,
|
2021-05-27 14:30:20 +02:00
|
|
|
distinct_attribute,
|
|
|
|
..
|
|
|
|
} = self;
|
|
|
|
|
|
|
|
Settings {
|
|
|
|
displayed_attributes,
|
|
|
|
searchable_attributes,
|
2021-06-03 23:47:16 +02:00
|
|
|
filterable_attributes,
|
2021-05-27 14:30:20 +02:00
|
|
|
ranking_rules,
|
|
|
|
stop_words,
|
2021-06-03 14:19:56 +02:00
|
|
|
synonyms,
|
2021-05-27 14:30:20 +02:00
|
|
|
distinct_attribute,
|
|
|
|
_kind: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 11:56:32 +01:00
|
|
|
}
|
|
|
|
|
2021-05-10 17:30:09 +02:00
|
|
|
impl Settings<Unchecked> {
|
2021-05-10 18:22:41 +02:00
|
|
|
pub fn check(mut self) -> Settings<Checked> {
|
|
|
|
let displayed_attributes = match self.displayed_attributes.take() {
|
|
|
|
Some(Some(fields)) => {
|
|
|
|
if fields.iter().any(|f| f == "*") {
|
|
|
|
Some(None)
|
|
|
|
} else {
|
|
|
|
Some(Some(fields))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
otherwise => otherwise,
|
|
|
|
};
|
|
|
|
|
|
|
|
let searchable_attributes = match self.searchable_attributes.take() {
|
|
|
|
Some(Some(fields)) => {
|
|
|
|
if fields.iter().any(|f| f == "*") {
|
|
|
|
Some(None)
|
|
|
|
} else {
|
|
|
|
Some(Some(fields))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
otherwise => otherwise,
|
|
|
|
};
|
|
|
|
|
|
|
|
Settings {
|
|
|
|
displayed_attributes,
|
|
|
|
searchable_attributes,
|
2021-06-03 23:47:16 +02:00
|
|
|
filterable_attributes: self.filterable_attributes,
|
2021-05-10 18:22:41 +02:00
|
|
|
ranking_rules: self.ranking_rules,
|
|
|
|
stop_words: self.stop_words,
|
2021-06-03 14:19:56 +02:00
|
|
|
synonyms: self.synonyms,
|
2021-05-10 18:22:41 +02:00
|
|
|
distinct_attribute: self.distinct_attribute,
|
|
|
|
_kind: PhantomData,
|
|
|
|
}
|
2021-05-10 17:30:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:56:32 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Facets {
|
|
|
|
pub level_group_size: Option<NonZeroUsize>,
|
|
|
|
pub min_level_size: Option<NonZeroUsize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index {
|
|
|
|
pub fn update_documents(
|
|
|
|
&self,
|
|
|
|
format: UpdateFormat,
|
|
|
|
method: IndexDocumentsMethod,
|
2021-04-22 10:14:29 +02:00
|
|
|
content: Option<impl io::Read>,
|
2021-03-04 11:56:32 +01:00
|
|
|
update_builder: UpdateBuilder,
|
|
|
|
primary_key: Option<&str>,
|
2021-06-14 21:26:35 +02:00
|
|
|
) -> Result<UpdateResult> {
|
2021-05-12 16:21:37 +02:00
|
|
|
let mut txn = self.write_txn()?;
|
2021-05-12 17:04:24 +02:00
|
|
|
let result = self.update_documents_txn(
|
|
|
|
&mut txn,
|
|
|
|
format,
|
|
|
|
method,
|
|
|
|
content,
|
|
|
|
update_builder,
|
|
|
|
primary_key,
|
|
|
|
)?;
|
2021-05-12 16:21:37 +02:00
|
|
|
txn.commit()?;
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_documents_txn<'a, 'b>(
|
|
|
|
&'a self,
|
|
|
|
txn: &mut heed::RwTxn<'a, 'b>,
|
|
|
|
format: UpdateFormat,
|
|
|
|
method: IndexDocumentsMethod,
|
|
|
|
content: Option<impl io::Read>,
|
|
|
|
update_builder: UpdateBuilder,
|
|
|
|
primary_key: Option<&str>,
|
2021-06-14 21:26:35 +02:00
|
|
|
) -> Result<UpdateResult> {
|
2021-03-04 11:56:32 +01:00
|
|
|
info!("performing document addition");
|
|
|
|
|
|
|
|
// Set the primary key if not set already, ignore if already set.
|
2021-06-16 17:15:56 +02:00
|
|
|
if let (None, Some(primary_key)) = (self.primary_key(txn)?, primary_key) {
|
2021-06-17 14:36:32 +02:00
|
|
|
let mut builder = UpdateBuilder::new(0).settings(txn, &self);
|
2021-06-16 17:15:56 +02:00
|
|
|
builder.set_primary_key(primary_key.to_string());
|
2021-06-17 14:36:32 +02:00
|
|
|
builder.execute(|_, _| ())?;
|
2021-03-04 11:56:32 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 16:21:37 +02:00
|
|
|
let mut builder = update_builder.index_documents(txn, self);
|
2021-03-04 11:56:32 +01:00
|
|
|
builder.update_format(format);
|
|
|
|
builder.index_documents_method(method);
|
|
|
|
|
2021-05-31 16:40:59 +02:00
|
|
|
let indexing_callback =
|
|
|
|
|indexing_step, update_id| info!("update {}: {:?}", update_id, indexing_step);
|
2021-05-25 16:33:09 +02:00
|
|
|
|
2021-03-04 11:56:32 +01:00
|
|
|
let gzipped = false;
|
2021-05-12 16:21:37 +02:00
|
|
|
let addition = match content {
|
2021-06-17 14:38:52 +02:00
|
|
|
Some(content) if gzipped => {
|
|
|
|
builder.execute(GzDecoder::new(content), indexing_callback)?
|
|
|
|
}
|
2021-06-17 14:36:32 +02:00
|
|
|
Some(content) => builder.execute(content, indexing_callback)?,
|
|
|
|
None => builder.execute(std::io::empty(), indexing_callback)?,
|
2021-03-04 11:56:32 +01:00
|
|
|
};
|
|
|
|
|
2021-05-12 16:21:37 +02:00
|
|
|
info!("document addition done: {:?}", addition);
|
2021-03-04 11:56:32 +01:00
|
|
|
|
2021-05-12 16:21:37 +02:00
|
|
|
Ok(UpdateResult::DocumentsAddition(addition))
|
2021-03-04 11:56:32 +01:00
|
|
|
}
|
|
|
|
|
2021-06-14 21:26:35 +02:00
|
|
|
pub fn clear_documents(&self, update_builder: UpdateBuilder) -> Result<UpdateResult> {
|
2021-03-04 11:56:32 +01:00
|
|
|
// We must use the write transaction of the update here.
|
|
|
|
let mut wtxn = self.write_txn()?;
|
|
|
|
let builder = update_builder.clear_documents(&mut wtxn, self);
|
|
|
|
|
2021-06-17 14:36:32 +02:00
|
|
|
let _count = builder.execute()?;
|
|
|
|
|
|
|
|
wtxn.commit()
|
|
|
|
.and(Ok(UpdateResult::Other))
|
|
|
|
.map_err(Into::into)
|
2021-03-04 11:56:32 +01:00
|
|
|
}
|
|
|
|
|
2021-05-12 16:21:37 +02:00
|
|
|
pub fn update_settings_txn<'a, 'b>(
|
|
|
|
&'a self,
|
|
|
|
txn: &mut heed::RwTxn<'a, 'b>,
|
2021-05-10 17:30:09 +02:00
|
|
|
settings: &Settings<Checked>,
|
2021-03-04 11:56:32 +01:00
|
|
|
update_builder: UpdateBuilder,
|
2021-06-14 21:26:35 +02:00
|
|
|
) -> Result<UpdateResult> {
|
2021-03-04 11:56:32 +01:00
|
|
|
// We must use the write transaction of the update here.
|
2021-05-12 16:21:37 +02:00
|
|
|
let mut builder = update_builder.settings(txn, self);
|
2021-03-04 11:56:32 +01:00
|
|
|
|
|
|
|
if let Some(ref names) = settings.searchable_attributes {
|
|
|
|
match names {
|
|
|
|
Some(names) => builder.set_searchable_fields(names.clone()),
|
|
|
|
None => builder.reset_searchable_fields(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref names) = settings.displayed_attributes {
|
|
|
|
match names {
|
|
|
|
Some(names) => builder.set_displayed_fields(names.clone()),
|
|
|
|
None => builder.reset_displayed_fields(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 23:47:16 +02:00
|
|
|
if let Some(ref facet_types) = settings.filterable_attributes {
|
2021-06-03 19:36:25 +02:00
|
|
|
let facet_types = facet_types.clone().unwrap_or_else(HashSet::new);
|
|
|
|
builder.set_filterable_fields(facet_types);
|
2021-03-04 11:56:32 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 22:39:16 +01:00
|
|
|
if let Some(ref criteria) = settings.ranking_rules {
|
2021-03-04 11:56:32 +01:00
|
|
|
match criteria {
|
|
|
|
Some(criteria) => builder.set_criteria(criteria.clone()),
|
|
|
|
None => builder.reset_criteria(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 15:41:03 +02:00
|
|
|
if let Some(ref stop_words) = settings.stop_words {
|
|
|
|
match stop_words {
|
|
|
|
Some(stop_words) => builder.set_stop_words(stop_words.clone()),
|
2021-06-14 10:38:56 +02:00
|
|
|
None => builder.reset_stop_words(),
|
2021-04-06 15:41:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 14:19:56 +02:00
|
|
|
if let Some(ref synonyms) = settings.synonyms {
|
|
|
|
match synonyms {
|
|
|
|
Some(synonyms) => builder.set_synonyms(synonyms.clone().into_iter().collect()),
|
2021-06-14 10:38:56 +02:00
|
|
|
None => builder.reset_synonyms(),
|
2021-06-03 14:19:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 09:22:36 +02:00
|
|
|
if let Some(ref distinct_attribute) = settings.distinct_attribute {
|
|
|
|
match distinct_attribute {
|
2021-06-03 19:36:25 +02:00
|
|
|
Some(attr) => builder.set_distinct_field(attr.clone()),
|
|
|
|
None => builder.reset_distinct_field(),
|
2021-03-29 09:22:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 14:38:52 +02:00
|
|
|
builder.execute(|indexing_step, update_id| {
|
|
|
|
info!("update {}: {:?}", update_id, indexing_step)
|
|
|
|
})?;
|
2021-03-04 11:56:32 +01:00
|
|
|
|
2021-05-12 16:21:37 +02:00
|
|
|
Ok(UpdateResult::Other)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_settings(
|
|
|
|
&self,
|
|
|
|
settings: &Settings<Checked>,
|
|
|
|
update_builder: UpdateBuilder,
|
2021-06-14 21:26:35 +02:00
|
|
|
) -> Result<UpdateResult> {
|
2021-05-12 16:21:37 +02:00
|
|
|
let mut txn = self.write_txn()?;
|
|
|
|
let result = self.update_settings_txn(&mut txn, settings, update_builder)?;
|
|
|
|
txn.commit()?;
|
|
|
|
Ok(result)
|
2021-03-04 11:56:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_documents(
|
|
|
|
&self,
|
2021-06-10 15:55:44 +02:00
|
|
|
document_ids: &[String],
|
2021-03-04 11:56:32 +01:00
|
|
|
update_builder: UpdateBuilder,
|
2021-06-14 21:26:35 +02:00
|
|
|
) -> Result<UpdateResult> {
|
2021-03-04 11:56:32 +01:00
|
|
|
let mut txn = self.write_txn()?;
|
2021-06-17 14:36:32 +02:00
|
|
|
let mut builder = update_builder.delete_documents(&mut txn, self)?;
|
2021-03-04 11:56:32 +01:00
|
|
|
|
|
|
|
// We ignore unexisting document ids
|
2021-06-09 17:10:10 +02:00
|
|
|
document_ids.iter().for_each(|id| {
|
2021-03-15 18:11:10 +01:00
|
|
|
builder.delete_external_id(id);
|
|
|
|
});
|
2021-03-04 11:56:32 +01:00
|
|
|
|
2021-06-17 14:36:32 +02:00
|
|
|
let deleted = builder.execute()?;
|
|
|
|
txn.commit()
|
|
|
|
.and(Ok(UpdateResult::DocumentDeletion { deleted }))
|
|
|
|
.map_err(Into::into)
|
2021-03-04 11:56:32 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-10 18:34:25 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_setting_check() {
|
|
|
|
// test no changes
|
|
|
|
let settings = Settings {
|
|
|
|
displayed_attributes: Some(Some(vec![String::from("hello")])),
|
|
|
|
searchable_attributes: Some(Some(vec![String::from("hello")])),
|
2021-06-03 23:47:16 +02:00
|
|
|
filterable_attributes: None,
|
2021-05-10 18:34:25 +02:00
|
|
|
ranking_rules: None,
|
|
|
|
stop_words: None,
|
2021-06-03 14:19:56 +02:00
|
|
|
synonyms: None,
|
2021-05-10 18:34:25 +02:00
|
|
|
distinct_attribute: None,
|
|
|
|
_kind: PhantomData::<Unchecked>,
|
|
|
|
};
|
|
|
|
|
|
|
|
let checked = settings.clone().check();
|
|
|
|
assert_eq!(settings.displayed_attributes, checked.displayed_attributes);
|
2021-05-12 17:04:24 +02:00
|
|
|
assert_eq!(
|
|
|
|
settings.searchable_attributes,
|
|
|
|
checked.searchable_attributes
|
|
|
|
);
|
2021-05-10 18:34:25 +02:00
|
|
|
|
|
|
|
// test wildcard
|
|
|
|
// test no changes
|
|
|
|
let settings = Settings {
|
|
|
|
displayed_attributes: Some(Some(vec![String::from("*")])),
|
|
|
|
searchable_attributes: Some(Some(vec![String::from("hello"), String::from("*")])),
|
2021-06-03 23:47:16 +02:00
|
|
|
filterable_attributes: None,
|
2021-05-10 18:34:25 +02:00
|
|
|
ranking_rules: None,
|
|
|
|
stop_words: None,
|
2021-06-03 14:19:56 +02:00
|
|
|
synonyms: None,
|
2021-05-10 18:34:25 +02:00
|
|
|
distinct_attribute: None,
|
|
|
|
_kind: PhantomData::<Unchecked>,
|
|
|
|
};
|
|
|
|
|
|
|
|
let checked = settings.check();
|
|
|
|
assert_eq!(checked.displayed_attributes, Some(None));
|
|
|
|
assert_eq!(checked.searchable_attributes, Some(None));
|
|
|
|
}
|
|
|
|
}
|