2021-10-04 18:31:05 +02:00
|
|
|
use std::collections::{BTreeSet, HashSet};
|
|
|
|
use std::fs::create_dir_all;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-03-22 18:17:33 +01:00
|
|
|
use fst::IntoStreamer;
|
2022-03-16 13:45:58 +01:00
|
|
|
use milli::heed::{EnvOpenOptions, RoTxn};
|
2022-01-19 11:21:19 +01:00
|
|
|
use milli::update::{IndexerConfig, Setting};
|
2021-10-04 18:31:05 +02:00
|
|
|
use milli::{obkv_to_json, FieldDistribution, FieldId};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::{Map, Value};
|
2022-02-14 15:32:41 +01:00
|
|
|
use time::OffsetDateTime;
|
2021-10-04 18:31:05 +02:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::EnvSizer;
|
|
|
|
|
|
|
|
use super::error::IndexError;
|
|
|
|
use super::error::Result;
|
2022-03-22 16:28:00 +01:00
|
|
|
use super::updates::{MinWordLengthTypoSetting, TypoSettings};
|
2021-10-06 13:01:02 +02:00
|
|
|
use super::{Checked, Settings};
|
2021-10-04 18:31:05 +02:00
|
|
|
|
|
|
|
pub type Document = Map<String, Value>;
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct IndexMeta {
|
2022-02-14 15:32:41 +01:00
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
|
|
pub created_at: OffsetDateTime,
|
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
|
|
pub updated_at: OffsetDateTime,
|
2021-10-04 18:31:05 +02:00
|
|
|
pub primary_key: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexMeta {
|
|
|
|
pub fn new(index: &Index) -> Result<Self> {
|
|
|
|
let txn = index.read_txn()?;
|
|
|
|
Self::new_txn(index, &txn)
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:45:58 +01:00
|
|
|
pub fn new_txn(index: &Index, txn: &milli::heed::RoTxn) -> Result<Self> {
|
2021-10-04 18:31:05 +02:00
|
|
|
let created_at = index.created_at(txn)?;
|
|
|
|
let updated_at = index.updated_at(txn)?;
|
|
|
|
let primary_key = index.primary_key(txn)?.map(String::from);
|
|
|
|
Ok(Self {
|
|
|
|
created_at,
|
|
|
|
updated_at,
|
|
|
|
primary_key,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct IndexStats {
|
|
|
|
#[serde(skip)]
|
|
|
|
pub size: u64,
|
|
|
|
pub number_of_documents: u64,
|
|
|
|
/// Whether the current index is performing an update. It is initially `None` when the
|
|
|
|
/// index returns it, since it is the `UpdateStore` that knows what index is currently indexing. It is
|
|
|
|
/// later set to either true or false, we we retrieve the information from the `UpdateStore`
|
|
|
|
pub is_indexing: Option<bool>,
|
|
|
|
pub field_distribution: FieldDistribution,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, derivative::Derivative)]
|
|
|
|
#[derivative(Debug)]
|
|
|
|
pub struct Index {
|
|
|
|
pub uuid: Uuid,
|
|
|
|
#[derivative(Debug = "ignore")]
|
|
|
|
pub inner: Arc<milli::Index>,
|
|
|
|
#[derivative(Debug = "ignore")]
|
2022-01-19 11:21:19 +01:00
|
|
|
pub indexer_config: Arc<IndexerConfig>,
|
2021-10-04 18:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Index {
|
|
|
|
type Target = milli::Index;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
self.inner.as_ref()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index {
|
|
|
|
pub fn open(
|
|
|
|
path: impl AsRef<Path>,
|
|
|
|
size: usize,
|
|
|
|
uuid: Uuid,
|
2022-01-19 11:21:19 +01:00
|
|
|
update_handler: Arc<IndexerConfig>,
|
2021-10-04 18:31:05 +02:00
|
|
|
) -> Result<Self> {
|
2021-12-02 16:03:26 +01:00
|
|
|
log::debug!("opening index in {}", path.as_ref().display());
|
2021-10-04 18:31:05 +02:00
|
|
|
create_dir_all(&path)?;
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(size);
|
|
|
|
let inner = Arc::new(milli::Index::new(options, &path)?);
|
|
|
|
Ok(Index {
|
|
|
|
inner,
|
|
|
|
uuid,
|
2022-01-19 11:21:19 +01:00
|
|
|
indexer_config: update_handler,
|
2021-10-04 18:31:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-02 16:03:26 +01:00
|
|
|
/// Asynchronously close the underlying index
|
|
|
|
pub fn close(self) {
|
|
|
|
self.inner.as_ref().clone().prepare_for_closing();
|
2021-10-04 18:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stats(&self) -> Result<IndexStats> {
|
|
|
|
let rtxn = self.read_txn()?;
|
|
|
|
|
|
|
|
Ok(IndexStats {
|
|
|
|
size: self.size(),
|
|
|
|
number_of_documents: self.number_of_documents(&rtxn)?,
|
|
|
|
is_indexing: None,
|
|
|
|
field_distribution: self.field_distribution(&rtxn)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn meta(&self) -> Result<IndexMeta> {
|
|
|
|
IndexMeta::new(self)
|
|
|
|
}
|
|
|
|
pub fn settings(&self) -> Result<Settings<Checked>> {
|
|
|
|
let txn = self.read_txn()?;
|
|
|
|
self.settings_txn(&txn)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn uuid(&self) -> Uuid {
|
|
|
|
self.uuid
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn settings_txn(&self, txn: &RoTxn) -> Result<Settings<Checked>> {
|
|
|
|
let displayed_attributes = self
|
|
|
|
.displayed_fields(txn)?
|
|
|
|
.map(|fields| fields.into_iter().map(String::from).collect());
|
|
|
|
|
|
|
|
let searchable_attributes = self
|
|
|
|
.searchable_fields(txn)?
|
|
|
|
.map(|fields| fields.into_iter().map(String::from).collect());
|
|
|
|
|
|
|
|
let filterable_attributes = self.filterable_fields(txn)?.into_iter().collect();
|
|
|
|
|
|
|
|
let sortable_attributes = self.sortable_fields(txn)?.into_iter().collect();
|
|
|
|
|
|
|
|
let criteria = self
|
|
|
|
.criteria(txn)?
|
|
|
|
.into_iter()
|
|
|
|
.map(|c| c.to_string())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let stop_words = self
|
|
|
|
.stop_words(txn)?
|
|
|
|
.map(|stop_words| -> Result<BTreeSet<_>> {
|
|
|
|
Ok(stop_words.stream().into_strs()?.into_iter().collect())
|
|
|
|
})
|
|
|
|
.transpose()?
|
2022-02-28 14:43:22 +01:00
|
|
|
.unwrap_or_default();
|
2021-10-04 18:31:05 +02:00
|
|
|
let distinct_field = self.distinct_field(txn)?.map(String::from);
|
|
|
|
|
|
|
|
// in milli each word in the synonyms map were split on their separator. Since we lost
|
|
|
|
// this information we are going to put space between words.
|
|
|
|
let synonyms = self
|
|
|
|
.synonyms(txn)?
|
|
|
|
.iter()
|
|
|
|
.map(|(key, values)| {
|
|
|
|
(
|
|
|
|
key.join(" "),
|
|
|
|
values.iter().map(|value| value.join(" ")).collect(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2022-03-22 16:28:00 +01:00
|
|
|
let min_typo_word_len = MinWordLengthTypoSetting {
|
|
|
|
one_typo: Setting::Set(self.min_word_len_one_typo(txn)?),
|
|
|
|
two_typos: Setting::Set(self.min_word_len_two_typos(txn)?),
|
|
|
|
};
|
|
|
|
|
2022-03-22 18:17:33 +01:00
|
|
|
let disabled_words = self
|
|
|
|
.exact_words(txn)?
|
|
|
|
.into_stream()
|
|
|
|
.into_strs()?
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
|
|
|
|
2022-04-06 19:25:12 +02:00
|
|
|
let disabled_attributes = self
|
|
|
|
.exact_attributes(txn)?
|
|
|
|
.into_iter()
|
|
|
|
.map(String::from)
|
|
|
|
.collect();
|
|
|
|
|
2022-03-17 11:59:35 +01:00
|
|
|
let typo_tolerance = TypoSettings {
|
|
|
|
enabled: Setting::Set(self.authorize_typos(txn)?),
|
2022-03-22 16:28:00 +01:00
|
|
|
min_word_length_for_typo: Setting::Set(min_typo_word_len),
|
2022-03-22 18:17:33 +01:00
|
|
|
disable_on_words: Setting::Set(disabled_words),
|
2022-04-06 19:25:12 +02:00
|
|
|
disable_on_attributes: Setting::Set(disabled_attributes),
|
2022-03-17 11:59:35 +01:00
|
|
|
};
|
|
|
|
|
2021-10-04 18:31:05 +02:00
|
|
|
Ok(Settings {
|
|
|
|
displayed_attributes: match displayed_attributes {
|
|
|
|
Some(attrs) => Setting::Set(attrs),
|
|
|
|
None => Setting::Reset,
|
|
|
|
},
|
|
|
|
searchable_attributes: match searchable_attributes {
|
|
|
|
Some(attrs) => Setting::Set(attrs),
|
|
|
|
None => Setting::Reset,
|
|
|
|
},
|
|
|
|
filterable_attributes: Setting::Set(filterable_attributes),
|
|
|
|
sortable_attributes: Setting::Set(sortable_attributes),
|
|
|
|
ranking_rules: Setting::Set(criteria),
|
|
|
|
stop_words: Setting::Set(stop_words),
|
|
|
|
distinct_attribute: match distinct_field {
|
|
|
|
Some(field) => Setting::Set(field),
|
|
|
|
None => Setting::Reset,
|
|
|
|
},
|
|
|
|
synonyms: Setting::Set(synonyms),
|
2022-03-17 11:59:35 +01:00
|
|
|
typo: Setting::Set(typo_tolerance),
|
2021-10-04 18:31:05 +02:00
|
|
|
_kind: PhantomData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn retrieve_documents<S: AsRef<str>>(
|
|
|
|
&self,
|
|
|
|
offset: usize,
|
|
|
|
limit: usize,
|
|
|
|
attributes_to_retrieve: Option<Vec<S>>,
|
|
|
|
) -> Result<Vec<Map<String, Value>>> {
|
|
|
|
let txn = self.read_txn()?;
|
|
|
|
|
|
|
|
let fields_ids_map = self.fields_ids_map(&txn)?;
|
|
|
|
let fields_to_display =
|
|
|
|
self.fields_to_display(&txn, &attributes_to_retrieve, &fields_ids_map)?;
|
|
|
|
|
|
|
|
let iter = self.documents.range(&txn, &(..))?.skip(offset).take(limit);
|
|
|
|
|
|
|
|
let mut documents = Vec::new();
|
|
|
|
|
|
|
|
for entry in iter {
|
|
|
|
let (_id, obkv) = entry?;
|
|
|
|
let object = obkv_to_json(&fields_to_display, &fields_ids_map, obkv)?;
|
|
|
|
documents.push(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(documents)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn retrieve_document<S: AsRef<str>>(
|
|
|
|
&self,
|
|
|
|
doc_id: String,
|
|
|
|
attributes_to_retrieve: Option<Vec<S>>,
|
|
|
|
) -> Result<Map<String, Value>> {
|
|
|
|
let txn = self.read_txn()?;
|
|
|
|
|
|
|
|
let fields_ids_map = self.fields_ids_map(&txn)?;
|
|
|
|
|
|
|
|
let fields_to_display =
|
|
|
|
self.fields_to_display(&txn, &attributes_to_retrieve, &fields_ids_map)?;
|
|
|
|
|
|
|
|
let internal_id = self
|
|
|
|
.external_documents_ids(&txn)?
|
|
|
|
.get(doc_id.as_bytes())
|
|
|
|
.ok_or_else(|| IndexError::DocumentNotFound(doc_id.clone()))?;
|
|
|
|
|
|
|
|
let document = self
|
|
|
|
.documents(&txn, std::iter::once(internal_id))?
|
|
|
|
.into_iter()
|
|
|
|
.next()
|
|
|
|
.map(|(_, d)| d)
|
|
|
|
.ok_or(IndexError::DocumentNotFound(doc_id))?;
|
|
|
|
|
|
|
|
let document = obkv_to_json(&fields_to_display, &fields_ids_map, document)?;
|
|
|
|
|
|
|
|
Ok(document)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn size(&self) -> u64 {
|
|
|
|
self.env.size()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fields_to_display<S: AsRef<str>>(
|
|
|
|
&self,
|
2022-03-16 13:45:58 +01:00
|
|
|
txn: &milli::heed::RoTxn,
|
2021-10-04 18:31:05 +02:00
|
|
|
attributes_to_retrieve: &Option<Vec<S>>,
|
|
|
|
fields_ids_map: &milli::FieldsIdsMap,
|
|
|
|
) -> Result<Vec<FieldId>> {
|
|
|
|
let mut displayed_fields_ids = match self.displayed_fields_ids(txn)? {
|
|
|
|
Some(ids) => ids.into_iter().collect::<Vec<_>>(),
|
|
|
|
None => fields_ids_map.iter().map(|(id, _)| id).collect(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let attributes_to_retrieve_ids = match attributes_to_retrieve {
|
|
|
|
Some(attrs) => attrs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|f| fields_ids_map.id(f.as_ref()))
|
|
|
|
.collect::<HashSet<_>>(),
|
|
|
|
None => fields_ids_map.iter().map(|(id, _)| id).collect(),
|
|
|
|
};
|
|
|
|
|
|
|
|
displayed_fields_ids.retain(|fid| attributes_to_retrieve_ids.contains(fid));
|
|
|
|
Ok(displayed_fields_ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn snapshot(&self, path: impl AsRef<Path>) -> Result<()> {
|
|
|
|
let mut dst = path.as_ref().join(format!("indexes/{}/", self.uuid));
|
|
|
|
create_dir_all(&dst)?;
|
|
|
|
dst.push("data.mdb");
|
|
|
|
let _txn = self.write_txn()?;
|
|
|
|
self.inner
|
|
|
|
.env
|
2022-03-16 13:45:58 +01:00
|
|
|
.copy_to_path(dst, milli::heed::CompactionOption::Enabled)?;
|
2021-10-04 18:31:05 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
/// When running tests, when a server instance is dropped, the environment is not actually closed,
|
|
|
|
/// leaving a lot of open file descriptors.
|
|
|
|
impl Drop for Index {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// When dropping the last instance of an index, we want to close the index
|
|
|
|
// Note that the close is actually performed only if all the instances a effectively
|
|
|
|
// dropped
|
|
|
|
|
|
|
|
if Arc::strong_count(&self.inner) == 1 {
|
|
|
|
self.inner.as_ref().clone().prepare_for_closing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|