2021-05-31 16:40:59 +02:00
|
|
|
use std::collections::{BTreeMap, BTreeSet};
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::path::Path;
|
2021-05-31 10:42:31 +02:00
|
|
|
|
|
|
|
use log::{error, info, warn};
|
2021-09-14 18:39:02 +02:00
|
|
|
use milli::update::Setting;
|
2021-08-24 20:55:29 +02:00
|
|
|
use serde::{Deserialize, Deserializer, Serialize};
|
2021-05-31 10:42:31 +02:00
|
|
|
use uuid::Uuid;
|
2021-05-26 20:42:09 +02:00
|
|
|
|
2021-05-31 16:40:59 +02:00
|
|
|
use crate::index_controller::{self, uuid_resolver::HeedUuidStore, IndexMetadata};
|
2021-09-08 11:43:10 +02:00
|
|
|
use crate::index_controller::{asc_ranking_rule, desc_ranking_rule};
|
2021-05-31 10:42:31 +02:00
|
|
|
use crate::{
|
2021-09-14 18:39:02 +02:00
|
|
|
index::Unchecked,
|
2021-09-21 13:23:22 +02:00
|
|
|
options::IndexerOpts,
|
2021-05-31 10:42:31 +02:00
|
|
|
};
|
2021-05-26 20:42:09 +02:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2021-05-31 10:42:31 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-05-26 20:42:09 +02:00
|
|
|
pub struct MetadataV1 {
|
|
|
|
db_version: String,
|
|
|
|
indexes: Vec<IndexMetadata>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MetadataV1 {
|
2021-05-31 10:42:31 +02:00
|
|
|
pub fn load_dump(
|
|
|
|
self,
|
|
|
|
src: impl AsRef<Path>,
|
|
|
|
dst: impl AsRef<Path>,
|
|
|
|
size: usize,
|
2021-05-31 16:40:59 +02:00
|
|
|
indexer_options: &IndexerOpts,
|
2021-05-31 10:42:31 +02:00
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
info!(
|
|
|
|
"Loading dump, dump database version: {}, dump version: V1",
|
|
|
|
self.db_version
|
|
|
|
);
|
|
|
|
|
|
|
|
let uuid_store = HeedUuidStore::new(&dst)?;
|
|
|
|
for index in self.indexes {
|
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
uuid_store.insert(index.uid.clone(), uuid)?;
|
|
|
|
let src = src.as_ref().join(index.uid);
|
2021-05-31 16:40:59 +02:00
|
|
|
load_index(
|
|
|
|
&src,
|
|
|
|
&dst,
|
|
|
|
uuid,
|
|
|
|
index.meta.primary_key.as_deref(),
|
|
|
|
size,
|
|
|
|
indexer_options,
|
|
|
|
)?;
|
2021-05-31 10:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 20:55:29 +02:00
|
|
|
pub fn deserialize_some<'de, T, D>(deserializer: D) -> std::result::Result<Option<T>, D::Error>
|
|
|
|
where
|
|
|
|
T: Deserialize<'de>,
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
Deserialize::deserialize(deserializer).map(Some)
|
|
|
|
}
|
|
|
|
|
2021-05-31 16:40:59 +02:00
|
|
|
// These are the settings used in legacy meilisearch (<v0.21.0).
|
2021-05-31 10:42:31 +02:00
|
|
|
#[derive(Default, Clone, Serialize, Deserialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
|
|
|
struct Settings {
|
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
|
|
|
pub ranking_rules: Option<Option<Vec<String>>>,
|
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
|
|
|
pub distinct_attribute: Option<Option<String>>,
|
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
|
|
|
pub searchable_attributes: Option<Option<Vec<String>>>,
|
|
|
|
#[serde(default, deserialize_with = "deserialize_some")]
|
|
|
|
pub displayed_attributes: Option<Option<BTreeSet<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")]
|
2021-08-04 10:33:30 +02:00
|
|
|
pub attributes_for_faceting: Option<Option<Vec<String>>>,
|
2021-05-31 10:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn load_index(
|
2021-09-14 18:39:02 +02:00
|
|
|
_src: impl AsRef<Path>,
|
|
|
|
_dst: impl AsRef<Path>,
|
|
|
|
_uuid: Uuid,
|
|
|
|
_primary_key: Option<&str>,
|
|
|
|
_size: usize,
|
|
|
|
_indexer_options: &IndexerOpts,
|
2021-05-31 10:42:31 +02:00
|
|
|
) -> anyhow::Result<()> {
|
2021-09-14 18:39:02 +02:00
|
|
|
todo!("fix dump obkv documents")
|
|
|
|
//let index_path = dst.as_ref().join(&format!("indexes/index-{}", uuid));
|
|
|
|
|
|
|
|
//create_dir_all(&index_path)?;
|
|
|
|
//let mut options = EnvOpenOptions::new();
|
|
|
|
//options.map_size(size);
|
|
|
|
//let index = milli::Index::new(options, index_path)?;
|
|
|
|
//let index = Index(Arc::new(index));
|
|
|
|
|
|
|
|
//// extract `settings.json` file and import content
|
|
|
|
//let settings = import_settings(&src)?;
|
|
|
|
//let settings: index_controller::Settings<Unchecked> = settings.into();
|
|
|
|
|
|
|
|
//let mut txn = index.write_txn()?;
|
|
|
|
|
|
|
|
//let handler = UpdateHandler::new(indexer_options)?;
|
|
|
|
|
|
|
|
//index.update_settings_txn(&mut txn, &settings.check(), handler.update_builder(0))?;
|
|
|
|
|
|
|
|
//let file = File::open(&src.as_ref().join("documents.jsonl"))?;
|
|
|
|
//let mut reader = std::io::BufReader::new(file);
|
|
|
|
//reader.fill_buf()?;
|
|
|
|
//if !reader.buffer().is_empty() {
|
|
|
|
//index.update_documents_txn(
|
|
|
|
//&mut txn,
|
|
|
|
//IndexDocumentsMethod::ReplaceDocuments,
|
|
|
|
//Some(reader),
|
|
|
|
//handler.update_builder(0),
|
|
|
|
//primary_key,
|
|
|
|
//)?;
|
|
|
|
//}
|
|
|
|
|
|
|
|
//txn.commit()?;
|
|
|
|
|
|
|
|
//// Finaly, we extract the original milli::Index and close it
|
|
|
|
//Arc::try_unwrap(index.0)
|
|
|
|
//.map_err(|_e| "Couldn't close the index properly")
|
|
|
|
//.unwrap()
|
|
|
|
//.prepare_for_closing()
|
|
|
|
//.wait();
|
|
|
|
|
|
|
|
//// Updates are ignored in dumps V1.
|
|
|
|
|
|
|
|
//Ok(())
|
2021-05-31 10:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// we need to **always** be able to convert the old settings to the settings currently being used
|
|
|
|
impl From<Settings> for index_controller::Settings<Unchecked> {
|
|
|
|
fn from(settings: Settings) -> Self {
|
|
|
|
Self {
|
2021-08-24 20:55:29 +02:00
|
|
|
distinct_attribute: match settings.distinct_attribute {
|
|
|
|
Some(Some(attr)) => Setting::Set(attr),
|
|
|
|
Some(None) => Setting::Reset,
|
|
|
|
None => Setting::NotSet
|
|
|
|
},
|
2021-05-31 10:42:31 +02:00
|
|
|
// we need to convert the old `Vec<String>` into a `BTreeSet<String>`
|
2021-08-24 20:55:29 +02:00
|
|
|
displayed_attributes: match settings.displayed_attributes {
|
|
|
|
Some(Some(attrs)) => Setting::Set(attrs.into_iter().collect()),
|
|
|
|
Some(None) => Setting::Reset,
|
|
|
|
None => Setting::NotSet
|
|
|
|
},
|
|
|
|
searchable_attributes: match settings.searchable_attributes {
|
|
|
|
Some(Some(attrs)) => Setting::Set(attrs),
|
|
|
|
Some(None) => Setting::Reset,
|
|
|
|
None => Setting::NotSet
|
|
|
|
},
|
|
|
|
filterable_attributes: match settings.attributes_for_faceting {
|
|
|
|
Some(Some(attrs)) => Setting::Set(attrs.into_iter().collect()),
|
|
|
|
Some(None) => Setting::Reset,
|
|
|
|
None => Setting::NotSet
|
|
|
|
},
|
2021-08-24 15:46:14 +02:00
|
|
|
sortable_attributes: Setting::NotSet,
|
2021-08-24 20:55:29 +02:00
|
|
|
ranking_rules: match settings.ranking_rules {
|
2021-09-07 16:55:35 +02:00
|
|
|
Some(Some(ranking_rules)) => Setting::Set(ranking_rules.into_iter().filter_map(|criterion| {
|
2021-08-24 20:55:29 +02:00
|
|
|
match criterion.as_str() {
|
2021-09-07 16:55:35 +02:00
|
|
|
"words" | "typo" | "proximity" | "attribute" | "exactness" => Some(criterion),
|
|
|
|
s if s.starts_with("asc") => asc_ranking_rule(s).map(|f| format!("{}:asc", f)),
|
|
|
|
s if s.starts_with("desc") => desc_ranking_rule(s).map(|f| format!("{}:desc", f)),
|
2021-08-24 20:55:29 +02:00
|
|
|
"wordsPosition" => {
|
2021-09-07 16:55:35 +02:00
|
|
|
warn!("The criteria `attribute` and `wordsPosition` have been merged \
|
|
|
|
into a single criterion `attribute` so `wordsPositon` will be \
|
|
|
|
ignored");
|
|
|
|
None
|
2021-08-24 20:55:29 +02:00
|
|
|
}
|
|
|
|
s => {
|
|
|
|
error!("Unknown criterion found in the dump: `{}`, it will be ignored", s);
|
2021-09-07 16:55:35 +02:00
|
|
|
None
|
2021-08-24 20:55:29 +02:00
|
|
|
}
|
2021-05-31 10:42:31 +02:00
|
|
|
}
|
2021-08-24 20:55:29 +02:00
|
|
|
}).collect()),
|
|
|
|
Some(None) => Setting::Reset,
|
|
|
|
None => Setting::NotSet
|
|
|
|
},
|
2021-05-31 10:42:31 +02:00
|
|
|
// we need to convert the old `Vec<String>` into a `BTreeSet<String>`
|
2021-08-24 20:55:29 +02:00
|
|
|
stop_words: match settings.stop_words {
|
|
|
|
Some(Some(stop_words)) => Setting::Set(stop_words.into_iter().collect()),
|
|
|
|
Some(None) => Setting::Reset,
|
|
|
|
None => Setting::NotSet
|
|
|
|
},
|
2021-06-03 14:19:56 +02:00
|
|
|
// we need to convert the old `Vec<String>` into a `BTreeMap<String>`
|
2021-08-24 20:55:29 +02:00
|
|
|
synonyms: match settings.synonyms {
|
|
|
|
Some(Some(synonyms)) => Setting::Set(synonyms.into_iter().collect()),
|
|
|
|
Some(None) => Setting::Reset,
|
|
|
|
None => Setting::NotSet
|
|
|
|
},
|
2021-05-31 10:42:31 +02:00
|
|
|
_kind: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 18:39:02 +02:00
|
|
|
// /// Extract Settings from `settings.json` file present at provided `dir_path`
|
|
|
|
//fn import_settings(dir_path: impl AsRef<Path>) -> anyhow::Result<Settings> {
|
|
|
|
//let path = dir_path.as_ref().join("settings.json");
|
|
|
|
//let file = File::open(path)?;
|
|
|
|
//let reader = std::io::BufReader::new(file);
|
|
|
|
//let metadata = serde_json::from_reader(reader)?;
|
2021-05-31 10:42:31 +02:00
|
|
|
|
2021-09-14 18:39:02 +02:00
|
|
|
//Ok(metadata)
|
|
|
|
//}
|
2021-08-04 11:02:17 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn settings_format_regression() {
|
|
|
|
let settings = Settings::default();
|
|
|
|
assert_eq!(
|
|
|
|
r##"{"rankingRules":null,"distinctAttribute":null,"searchableAttributes":null,"displayedAttributes":null,"stopWords":null,"synonyms":null,"attributesForFaceting":null}"##,
|
|
|
|
serde_json::to_string(&settings).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|