2021-08-16 13:36:30 +02:00
|
|
|
mod extract;
|
|
|
|
mod helpers;
|
|
|
|
mod transform;
|
|
|
|
mod typed_chunk;
|
|
|
|
|
2020-11-03 13:42:29 +01:00
|
|
|
use std::collections::HashSet;
|
2021-08-31 11:44:15 +02:00
|
|
|
use std::io::{Read, Seek};
|
2021-08-16 13:36:30 +02:00
|
|
|
use std::iter::FromIterator;
|
2021-03-24 15:06:54 +01:00
|
|
|
use std::num::{NonZeroU32, NonZeroUsize};
|
2020-10-26 20:18:10 +01:00
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
use crossbeam_channel::{Receiver, Sender};
|
2021-12-08 14:12:07 +01:00
|
|
|
use log::debug;
|
2021-08-16 13:36:30 +02:00
|
|
|
use roaring::RoaringBitmap;
|
2021-06-16 18:33:33 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-16 13:36:30 +02:00
|
|
|
use typed_chunk::{write_typed_chunk_into_index, TypedChunk};
|
2020-11-03 13:20:11 +01:00
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
pub use self::helpers::{
|
2022-01-19 14:30:03 +01:00
|
|
|
create_sorter, create_writer, fst_stream_into_hashset, merge_cbo_roaring_bitmaps,
|
|
|
|
merge_roaring_bitmaps, sorter_into_lmdb_database, write_into_lmdb_database, writer_into_reader,
|
|
|
|
ClonableMmap, MergeFn,
|
2020-10-26 20:18:10 +01:00
|
|
|
};
|
2021-08-16 13:36:30 +02:00
|
|
|
use self::helpers::{grenad_obkv_into_chunks, GrenadParameters};
|
2020-10-26 20:18:10 +01:00
|
|
|
pub use self::transform::{Transform, TransformOutput};
|
2021-08-31 11:44:15 +02:00
|
|
|
use crate::documents::DocumentBatchReader;
|
2022-01-18 14:59:51 +01:00
|
|
|
pub use crate::update::index_documents::helpers::CursorClonableMmap;
|
2021-06-16 18:33:33 +02:00
|
|
|
use crate::update::{
|
2021-12-08 14:12:07 +01:00
|
|
|
self, Facets, IndexerConfig, UpdateIndexingStep, WordPrefixDocids,
|
|
|
|
WordPrefixPairProximityDocids, WordPrefixPositionDocids, WordsPrefixesFst,
|
2021-06-16 18:33:33 +02:00
|
|
|
};
|
2021-08-16 13:36:30 +02:00
|
|
|
use crate::{Index, Result};
|
2020-10-26 20:18:10 +01:00
|
|
|
|
2021-08-17 10:56:06 +02:00
|
|
|
static MERGED_DATABASE_COUNT: usize = 7;
|
|
|
|
static PREFIX_DATABASE_COUNT: usize = 5;
|
|
|
|
static TOTAL_POSTING_DATABASE_COUNT: usize = MERGED_DATABASE_COUNT + PREFIX_DATABASE_COUNT;
|
|
|
|
|
2021-11-10 14:08:36 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
2020-12-30 18:43:50 +01:00
|
|
|
pub struct DocumentAdditionResult {
|
2021-11-10 14:08:36 +01:00
|
|
|
/// The number of documents that were indexed during the update
|
|
|
|
pub indexed_documents: u64,
|
|
|
|
/// The total number of documents in the index after the update
|
|
|
|
pub number_of_documents: u64,
|
2020-12-30 18:43:50 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:17:35 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
2020-10-31 16:10:15 +01:00
|
|
|
#[non_exhaustive]
|
2020-10-26 11:02:44 +01:00
|
|
|
pub enum IndexDocumentsMethod {
|
|
|
|
/// Replace the previous document with the new one,
|
|
|
|
/// removing all the already known attributes.
|
|
|
|
ReplaceDocuments,
|
|
|
|
|
|
|
|
/// Merge the previous version of the document with the new version,
|
|
|
|
/// replacing old attributes values with the new ones and add the new attributes.
|
|
|
|
UpdateDocuments,
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
impl Default for IndexDocumentsMethod {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::ReplaceDocuments
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum WriteMethod {
|
|
|
|
Append,
|
|
|
|
GetMergePut,
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
pub struct IndexDocuments<'t, 'u, 'i, 'a, F> {
|
2020-10-30 11:42:00 +01:00
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
2020-10-26 11:02:44 +01:00
|
|
|
index: &'i Index,
|
2021-12-08 14:12:07 +01:00
|
|
|
config: IndexDocumentsConfig,
|
|
|
|
indexer_config: &'a IndexerConfig,
|
|
|
|
transform: Option<Transform<'a, 'i>>,
|
|
|
|
progress: F,
|
|
|
|
added_documents: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug, Clone)]
|
|
|
|
pub struct IndexDocumentsConfig {
|
|
|
|
pub facet_level_group_size: Option<NonZeroUsize>,
|
|
|
|
pub facet_min_level_size: Option<NonZeroUsize>,
|
|
|
|
pub words_prefix_threshold: Option<u32>,
|
|
|
|
pub max_prefix_length: Option<usize>,
|
|
|
|
pub words_positions_level_group_size: Option<NonZeroU32>,
|
|
|
|
pub words_positions_min_level_size: Option<NonZeroU32>,
|
|
|
|
pub update_method: IndexDocumentsMethod,
|
|
|
|
pub autogenerate_docids: bool,
|
2020-10-26 11:02:44 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
impl<'t, 'u, 'i, 'a, F> IndexDocuments<'t, 'u, 'i, 'a, F>
|
|
|
|
where
|
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
|
|
|
{
|
2020-12-22 16:21:07 +01:00
|
|
|
pub fn new(
|
|
|
|
wtxn: &'t mut heed::RwTxn<'i, 'u>,
|
|
|
|
index: &'i Index,
|
2021-12-08 14:12:07 +01:00
|
|
|
indexer_config: &'a IndexerConfig,
|
|
|
|
config: IndexDocumentsConfig,
|
|
|
|
progress: F,
|
|
|
|
) -> IndexDocuments<'t, 'u, 'i, 'a, F> {
|
|
|
|
let transform = Some(Transform::new(
|
|
|
|
&index,
|
|
|
|
indexer_config,
|
|
|
|
config.update_method,
|
|
|
|
config.autogenerate_docids,
|
|
|
|
));
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
IndexDocuments {
|
2021-12-08 14:12:07 +01:00
|
|
|
transform,
|
|
|
|
config,
|
|
|
|
indexer_config,
|
|
|
|
progress,
|
2020-10-26 20:18:10 +01:00
|
|
|
wtxn,
|
|
|
|
index,
|
2021-12-08 14:12:07 +01:00
|
|
|
added_documents: 0,
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
/// Adds a batch of documents to the current builder.
|
|
|
|
///
|
|
|
|
/// Since the documents are progressively added to the writer, a failure will cause a stale
|
|
|
|
/// builder, and the builder must be discarded.
|
|
|
|
///
|
|
|
|
/// Returns the number of documents added to the builder.
|
|
|
|
pub fn add_documents<R>(&mut self, reader: DocumentBatchReader<R>) -> Result<u64>
|
2020-10-26 20:18:10 +01:00
|
|
|
where
|
2021-08-31 11:44:15 +02:00
|
|
|
R: Read + Seek,
|
2020-10-26 20:18:10 +01:00
|
|
|
{
|
2021-05-06 21:16:40 +02:00
|
|
|
// Early return when there is no document to add
|
2021-08-31 11:44:15 +02:00
|
|
|
if reader.is_empty() {
|
2021-12-08 14:12:07 +01:00
|
|
|
return Ok(0);
|
2021-05-06 18:14:16 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
let indexed_documents = self
|
|
|
|
.transform
|
|
|
|
.as_mut()
|
|
|
|
.expect("Invalid document addition state")
|
|
|
|
.read_documents(reader, self.wtxn, &self.progress)?
|
|
|
|
as u64;
|
2020-10-26 20:18:10 +01:00
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
self.added_documents += indexed_documents;
|
2020-12-30 18:43:50 +01:00
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
Ok(indexed_documents)
|
|
|
|
}
|
2020-11-03 13:20:11 +01:00
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
#[logging_timer::time("IndexDocuments::{}")]
|
|
|
|
pub fn execute(mut self) -> Result<DocumentAdditionResult> {
|
|
|
|
if self.added_documents == 0 {
|
|
|
|
let number_of_documents = self.index.number_of_documents(self.wtxn)?;
|
|
|
|
return Ok(DocumentAdditionResult { indexed_documents: 0, number_of_documents });
|
|
|
|
}
|
|
|
|
let output = self
|
|
|
|
.transform
|
|
|
|
.take()
|
|
|
|
.expect("Invalid document addition state")
|
|
|
|
.output_from_sorter(self.wtxn, &self.progress)?;
|
|
|
|
let indexed_documents = output.documents_count as u64;
|
|
|
|
let number_of_documents = self.execute_raw(output)?;
|
2020-11-03 13:20:11 +01:00
|
|
|
|
2021-11-10 14:08:36 +01:00
|
|
|
Ok(DocumentAdditionResult { indexed_documents, number_of_documents })
|
|
|
|
}
|
2021-12-08 14:12:07 +01:00
|
|
|
|
2021-11-10 14:08:36 +01:00
|
|
|
/// Returns the total number of documents in the index after the update.
|
2021-08-24 13:55:53 +02:00
|
|
|
#[logging_timer::time("IndexDocuments::{}")]
|
2021-12-08 14:12:07 +01:00
|
|
|
pub fn execute_raw(self, output: TransformOutput) -> Result<u64>
|
2020-11-03 13:20:11 +01:00
|
|
|
where
|
2021-06-16 18:33:33 +02:00
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
2020-11-03 13:20:11 +01:00
|
|
|
{
|
2020-10-26 20:18:10 +01:00
|
|
|
let TransformOutput {
|
2020-10-31 12:54:43 +01:00
|
|
|
primary_key,
|
2020-10-26 20:18:10 +01:00
|
|
|
fields_ids_map,
|
2021-06-17 15:16:20 +02:00
|
|
|
field_distribution,
|
2020-11-22 11:54:04 +01:00
|
|
|
external_documents_ids,
|
2020-10-26 20:18:10 +01:00
|
|
|
new_documents_ids,
|
|
|
|
replaced_documents_ids,
|
|
|
|
documents_count,
|
|
|
|
documents_file,
|
2020-10-31 16:10:15 +01:00
|
|
|
} = output;
|
2020-10-26 20:18:10 +01:00
|
|
|
|
2021-07-27 16:24:21 +02:00
|
|
|
// The fields_ids_map is put back to the store now so the rest of the transaction sees an
|
|
|
|
// up to date field map.
|
|
|
|
self.index.put_fields_ids_map(self.wtxn, &fields_ids_map)?;
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
let backup_pool;
|
2021-12-08 14:12:07 +01:00
|
|
|
let pool = match self.indexer_config.thread_pool {
|
|
|
|
Some(ref pool) => pool,
|
2021-08-16 13:36:30 +02:00
|
|
|
#[cfg(not(test))]
|
|
|
|
None => {
|
|
|
|
// We initialize a bakcup pool with the default
|
|
|
|
// settings if none have already been set.
|
|
|
|
backup_pool = rayon::ThreadPoolBuilder::new().build()?;
|
|
|
|
&backup_pool
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
None => {
|
|
|
|
// We initialize a bakcup pool with the default
|
|
|
|
// settings if none have already been set.
|
|
|
|
backup_pool = rayon::ThreadPoolBuilder::new().num_threads(1).build()?;
|
|
|
|
&backup_pool
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let documents_file = grenad::Reader::new(documents_file)?;
|
|
|
|
|
|
|
|
// create LMDB writer channel
|
2021-08-24 13:01:31 +02:00
|
|
|
let (lmdb_writer_sx, lmdb_writer_rx): (
|
|
|
|
Sender<Result<TypedChunk>>,
|
|
|
|
Receiver<Result<TypedChunk>>,
|
|
|
|
) = crossbeam_channel::unbounded();
|
2021-08-16 13:36:30 +02:00
|
|
|
|
2021-09-02 15:57:40 +02:00
|
|
|
// get the primary key field id
|
2021-09-08 18:12:10 +02:00
|
|
|
let primary_key_id = fields_ids_map.id(&primary_key).unwrap();
|
2021-09-02 15:57:40 +02:00
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
// get searchable fields for word databases
|
|
|
|
let searchable_fields =
|
|
|
|
self.index.searchable_fields_ids(self.wtxn)?.map(HashSet::from_iter);
|
|
|
|
// get filterable fields for facet databases
|
|
|
|
let faceted_fields = self.index.faceted_fields_ids(self.wtxn)?;
|
2021-08-23 18:41:48 +02:00
|
|
|
// get the fid of the `_geo` field.
|
2021-09-09 12:20:08 +02:00
|
|
|
let geo_field_id = match self.index.fields_ids_map(self.wtxn)?.id("_geo") {
|
|
|
|
Some(gfid) => {
|
|
|
|
let is_sortable = self.index.sortable_fields_ids(self.wtxn)?.contains(&gfid);
|
|
|
|
let is_filterable = self.index.filterable_fields_ids(self.wtxn)?.contains(&gfid);
|
|
|
|
if is_sortable || is_filterable {
|
|
|
|
Some(gfid)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => None,
|
2021-08-30 15:47:33 +02:00
|
|
|
};
|
2021-08-16 13:36:30 +02:00
|
|
|
|
2021-08-17 12:25:07 +02:00
|
|
|
let stop_words = self.index.stop_words(self.wtxn)?;
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
// Run extraction pipeline in parallel.
|
|
|
|
pool.install(|| {
|
|
|
|
let params = GrenadParameters {
|
2021-12-08 14:12:07 +01:00
|
|
|
chunk_compression_type: self.indexer_config.chunk_compression_type,
|
|
|
|
chunk_compression_level: self.indexer_config.chunk_compression_level,
|
|
|
|
max_memory: self.indexer_config.max_memory,
|
|
|
|
max_nb_chunks: self.indexer_config.max_nb_chunks, // default value, may be chosen.
|
2021-08-16 13:36:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// split obkv file into several chuncks
|
2021-08-24 13:01:31 +02:00
|
|
|
let chunk_iter = grenad_obkv_into_chunks(
|
2021-08-16 13:36:30 +02:00
|
|
|
documents_file,
|
|
|
|
params.clone(),
|
2021-12-08 14:12:07 +01:00
|
|
|
self.indexer_config.documents_chunk_size.unwrap_or(1024 * 1024 * 128), // 128MiB
|
2021-08-24 13:01:31 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let result = chunk_iter.map(|chunk_iter| {
|
|
|
|
// extract all databases from the chunked obkv douments
|
|
|
|
extract::data_from_obkv_documents(
|
|
|
|
chunk_iter,
|
|
|
|
params,
|
|
|
|
lmdb_writer_sx.clone(),
|
|
|
|
searchable_fields,
|
|
|
|
faceted_fields,
|
2021-09-02 15:57:40 +02:00
|
|
|
primary_key_id,
|
2021-08-23 18:41:48 +02:00
|
|
|
geo_field_id,
|
2021-08-24 13:01:31 +02:00
|
|
|
stop_words,
|
2021-12-08 14:12:07 +01:00
|
|
|
self.indexer_config.max_positions_per_attributes,
|
2021-08-24 13:01:31 +02:00
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Err(e) = result {
|
2021-08-26 11:01:30 +02:00
|
|
|
let _ = lmdb_writer_sx.send(Err(e));
|
2021-08-24 13:01:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// needs to be droped to avoid channel waiting lock.
|
|
|
|
drop(lmdb_writer_sx)
|
2021-08-16 13:36:30 +02:00
|
|
|
});
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
// We delete the documents that this document addition replaces. This way we are
|
|
|
|
// able to simply insert all the documents even if they already exist in the database.
|
|
|
|
if !replaced_documents_ids.is_empty() {
|
2021-12-08 14:12:07 +01:00
|
|
|
let mut deletion_builder = update::DeleteDocuments::new(self.wtxn, self.index)?;
|
2020-11-19 11:18:52 +01:00
|
|
|
debug!("documents to delete {:?}", replaced_documents_ids);
|
2020-10-26 20:18:10 +01:00
|
|
|
deletion_builder.delete_documents(&replaced_documents_ids);
|
2020-11-19 11:18:52 +01:00
|
|
|
let deleted_documents_count = deletion_builder.execute()?;
|
2021-11-10 14:08:36 +01:00
|
|
|
debug!("{} documents actually deleted", deleted_documents_count.deleted_documents);
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
|
2021-08-16 13:36:30 +02:00
|
|
|
let index_documents_ids = self.index.documents_ids(self.wtxn)?;
|
|
|
|
let index_is_empty = index_documents_ids.len() == 0;
|
|
|
|
let mut final_documents_ids = RoaringBitmap::new();
|
2022-01-18 14:59:51 +01:00
|
|
|
let mut word_pair_proximity_docids = Vec::new();
|
2021-08-16 13:36:30 +02:00
|
|
|
|
2021-08-17 10:56:06 +02:00
|
|
|
let mut databases_seen = 0;
|
2021-12-08 14:12:07 +01:00
|
|
|
(self.progress)(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
2021-08-17 10:56:06 +02:00
|
|
|
databases_seen,
|
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2022-01-18 14:59:51 +01:00
|
|
|
for result in lmdb_writer_rx {
|
|
|
|
let typed_chunk = match result? {
|
|
|
|
TypedChunk::WordPairProximityDocids(chunk) => {
|
|
|
|
// We extract and mmap our chunk file to be able to get it for next processes.
|
|
|
|
let mut file = chunk.into_inner();
|
|
|
|
let mmap = unsafe { memmap2::Mmap::map(&file)? };
|
|
|
|
let cursor_mmap = CursorClonableMmap::new(ClonableMmap::from(mmap));
|
|
|
|
let chunk = grenad::Reader::new(cursor_mmap)?;
|
|
|
|
word_pair_proximity_docids.push(chunk);
|
|
|
|
|
|
|
|
// We reconstruct our typed-chunk back.
|
|
|
|
file.rewind()?;
|
|
|
|
let chunk = grenad::Reader::new(file)?;
|
|
|
|
TypedChunk::WordPairProximityDocids(chunk)
|
|
|
|
}
|
|
|
|
otherwise => otherwise,
|
|
|
|
};
|
|
|
|
|
2021-08-17 10:56:06 +02:00
|
|
|
let (docids, is_merged_database) =
|
2022-01-18 14:59:51 +01:00
|
|
|
write_typed_chunk_into_index(typed_chunk, &self.index, self.wtxn, index_is_empty)?;
|
2021-08-17 10:56:06 +02:00
|
|
|
if !docids.is_empty() {
|
|
|
|
final_documents_ids |= docids;
|
|
|
|
let documents_seen_count = final_documents_ids.len();
|
2021-12-08 14:12:07 +01:00
|
|
|
(self.progress)(UpdateIndexingStep::IndexDocuments {
|
2021-08-17 10:56:06 +02:00
|
|
|
documents_seen: documents_seen_count as usize,
|
|
|
|
total_documents: documents_count,
|
|
|
|
});
|
|
|
|
debug!(
|
|
|
|
"We have seen {} documents on {} total document so far",
|
|
|
|
documents_seen_count, documents_count
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if is_merged_database {
|
|
|
|
databases_seen += 1;
|
2021-12-08 14:12:07 +01:00
|
|
|
(self.progress)(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
2021-11-10 14:08:36 +01:00
|
|
|
databases_seen,
|
2021-08-17 10:56:06 +02:00
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
}
|
2020-10-26 20:18:10 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 15:16:20 +02:00
|
|
|
// We write the field distribution into the main database
|
|
|
|
self.index.put_field_distribution(self.wtxn, &field_distribution)?;
|
2021-03-31 17:14:23 +02:00
|
|
|
|
2020-10-31 12:54:43 +01:00
|
|
|
// We write the primary key field id into the main database
|
2021-01-20 17:27:43 +01:00
|
|
|
self.index.put_primary_key(self.wtxn, &primary_key)?;
|
2020-10-31 12:54:43 +01:00
|
|
|
|
2020-11-22 11:54:04 +01:00
|
|
|
// We write the external documents ids into the main database.
|
|
|
|
self.index.put_external_documents_ids(self.wtxn, &external_documents_ids)?;
|
2020-10-26 20:18:10 +01:00
|
|
|
|
2021-08-17 10:56:06 +02:00
|
|
|
let all_documents_ids = index_documents_ids | new_documents_ids | replaced_documents_ids;
|
2021-08-16 13:36:30 +02:00
|
|
|
self.index.put_documents_ids(self.wtxn, &all_documents_ids)?;
|
2020-11-11 12:39:09 +01:00
|
|
|
|
2022-01-18 14:59:51 +01:00
|
|
|
self.execute_prefix_databases(word_pair_proximity_docids)?;
|
2021-11-10 14:08:36 +01:00
|
|
|
|
|
|
|
Ok(all_documents_ids.len())
|
2021-08-16 13:36:30 +02:00
|
|
|
}
|
2020-10-26 20:18:10 +01:00
|
|
|
|
2021-08-24 13:55:53 +02:00
|
|
|
#[logging_timer::time("IndexDocuments::{}")]
|
2022-01-18 14:59:51 +01:00
|
|
|
pub fn execute_prefix_databases(
|
|
|
|
self,
|
|
|
|
word_pair_proximity_docids: Vec<grenad::Reader<CursorClonableMmap>>,
|
|
|
|
) -> Result<()>
|
|
|
|
where
|
|
|
|
F: Fn(UpdateIndexingStep) + Sync,
|
|
|
|
{
|
2021-08-17 10:56:06 +02:00
|
|
|
// Merged databases are already been indexed, we start from this count;
|
|
|
|
let mut databases_seen = MERGED_DATABASE_COUNT;
|
|
|
|
|
2021-02-10 11:53:13 +01:00
|
|
|
// Run the facets update operation.
|
2021-11-03 13:12:01 +01:00
|
|
|
let mut builder = Facets::new(self.wtxn, self.index);
|
2021-12-08 14:12:07 +01:00
|
|
|
builder.chunk_compression_type = self.indexer_config.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.indexer_config.chunk_compression_level;
|
|
|
|
if let Some(value) = self.config.facet_level_group_size {
|
2020-11-28 12:43:43 +01:00
|
|
|
builder.level_group_size(value);
|
2020-11-17 21:19:25 +01:00
|
|
|
}
|
2021-12-08 14:12:07 +01:00
|
|
|
if let Some(value) = self.config.facet_min_level_size {
|
2020-11-28 12:43:43 +01:00
|
|
|
builder.min_level_size(value);
|
2020-11-18 16:29:07 +01:00
|
|
|
}
|
2020-11-17 21:19:25 +01:00
|
|
|
builder.execute()?;
|
2021-03-11 17:23:46 +01:00
|
|
|
|
2021-08-17 10:56:06 +02:00
|
|
|
databases_seen += 1;
|
2021-12-08 14:12:07 +01:00
|
|
|
(self.progress)(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen,
|
2021-08-17 10:56:06 +02:00
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2022-01-18 14:02:24 +01:00
|
|
|
let previous_words_prefixes_fst =
|
|
|
|
self.index.words_prefixes_fst(self.wtxn)?.map_data(|cow| cow.into_owned())?;
|
|
|
|
|
2021-02-10 11:53:13 +01:00
|
|
|
// Run the words prefixes update operation.
|
2021-11-03 13:12:01 +01:00
|
|
|
let mut builder = WordsPrefixesFst::new(self.wtxn, self.index);
|
2021-12-08 14:12:07 +01:00
|
|
|
if let Some(value) = self.config.words_prefix_threshold {
|
2021-02-10 11:53:13 +01:00
|
|
|
builder.threshold(value);
|
|
|
|
}
|
2021-12-08 14:12:07 +01:00
|
|
|
if let Some(value) = self.config.max_prefix_length {
|
2021-02-10 11:53:13 +01:00
|
|
|
builder.max_prefix_length(value);
|
|
|
|
}
|
|
|
|
builder.execute()?;
|
|
|
|
|
2021-08-17 10:56:06 +02:00
|
|
|
databases_seen += 1;
|
2021-12-08 14:12:07 +01:00
|
|
|
(self.progress)(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen,
|
2021-08-17 10:56:06 +02:00
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2021-03-25 11:10:12 +01:00
|
|
|
// Run the word prefix docids update operation.
|
|
|
|
let mut builder = WordPrefixDocids::new(self.wtxn, self.index);
|
2021-12-08 14:12:07 +01:00
|
|
|
builder.chunk_compression_type = self.indexer_config.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.indexer_config.chunk_compression_level;
|
|
|
|
builder.max_nb_chunks = self.indexer_config.max_nb_chunks;
|
|
|
|
builder.max_memory = self.indexer_config.max_memory;
|
2021-03-25 11:10:12 +01:00
|
|
|
builder.execute()?;
|
|
|
|
|
2021-08-17 10:56:06 +02:00
|
|
|
databases_seen += 1;
|
2021-12-08 14:12:07 +01:00
|
|
|
(self.progress)(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen,
|
2021-08-17 10:56:06 +02:00
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2021-03-25 11:10:12 +01:00
|
|
|
// Run the word prefix pair proximity docids update operation.
|
|
|
|
let mut builder = WordPrefixPairProximityDocids::new(self.wtxn, self.index);
|
2021-12-08 14:12:07 +01:00
|
|
|
builder.chunk_compression_type = self.indexer_config.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.indexer_config.chunk_compression_level;
|
|
|
|
builder.max_nb_chunks = self.indexer_config.max_nb_chunks;
|
|
|
|
builder.max_memory = self.indexer_config.max_memory;
|
2022-01-18 14:59:51 +01:00
|
|
|
builder.execute(word_pair_proximity_docids, &previous_words_prefixes_fst)?;
|
2021-03-25 11:10:12 +01:00
|
|
|
|
2021-08-17 10:56:06 +02:00
|
|
|
databases_seen += 1;
|
2021-12-08 14:12:07 +01:00
|
|
|
(self.progress)(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen,
|
2021-08-17 10:56:06 +02:00
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2021-10-05 11:18:42 +02:00
|
|
|
// Run the words prefix position docids update operation.
|
|
|
|
let mut builder = WordPrefixPositionDocids::new(self.wtxn, self.index);
|
2021-12-08 14:12:07 +01:00
|
|
|
builder.chunk_compression_type = self.indexer_config.chunk_compression_type;
|
|
|
|
builder.chunk_compression_level = self.indexer_config.chunk_compression_level;
|
|
|
|
builder.max_nb_chunks = self.indexer_config.max_nb_chunks;
|
|
|
|
builder.max_memory = self.indexer_config.max_memory;
|
|
|
|
if let Some(value) = self.config.words_positions_level_group_size {
|
2021-03-17 13:55:24 +01:00
|
|
|
builder.level_group_size(value);
|
|
|
|
}
|
2021-12-08 14:12:07 +01:00
|
|
|
if let Some(value) = self.config.words_positions_min_level_size {
|
2021-03-17 13:55:24 +01:00
|
|
|
builder.min_level_size(value);
|
|
|
|
}
|
|
|
|
builder.execute()?;
|
|
|
|
|
2021-08-17 10:56:06 +02:00
|
|
|
databases_seen += 1;
|
2021-12-08 14:12:07 +01:00
|
|
|
(self.progress)(UpdateIndexingStep::MergeDataIntoFinalDatabase {
|
|
|
|
databases_seen,
|
2021-08-17 10:56:06 +02:00
|
|
|
total_databases: TOTAL_POSTING_DATABASE_COUNT,
|
|
|
|
});
|
|
|
|
|
2020-10-26 20:18:10 +01:00
|
|
|
Ok(())
|
2020-10-26 11:02:44 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-30 12:14:25 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-06-23 18:35:44 +02:00
|
|
|
use std::io::Cursor;
|
|
|
|
|
2021-07-06 11:40:45 +02:00
|
|
|
use big_s::S;
|
2020-10-30 12:14:25 +01:00
|
|
|
use heed::EnvOpenOptions;
|
|
|
|
|
2021-06-16 18:33:33 +02:00
|
|
|
use super::*;
|
2021-08-31 11:44:15 +02:00
|
|
|
use crate::documents::DocumentBatchBuilder;
|
2021-06-30 11:23:29 +02:00
|
|
|
use crate::update::DeleteDocuments;
|
2021-07-06 11:40:45 +02:00
|
|
|
use crate::HashMap;
|
2021-06-16 18:33:33 +02:00
|
|
|
|
2020-10-30 12:14:25 +01:00
|
|
|
#[test]
|
2020-10-30 13:46:56 +01:00
|
|
|
fn simple_document_replacement() {
|
2020-10-30 12:14:25 +01:00
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with ids from 1 to 3.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
|
|
|
{ "id": 1, "name": "kevin" },
|
|
|
|
{ "id": 2, "name": "kevina" },
|
|
|
|
{ "id": 3, "name": "benoit" }
|
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
|
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-10-30 12:14:25 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 3 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Second we send 1 document with id 1, to erase the previous ones.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([ { "id": 1, "name": "updated kevin" } ]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-10-30 12:14:25 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
2020-10-30 13:46:56 +01:00
|
|
|
// Check that there is **always** 3 documents.
|
2020-10-30 12:14:25 +01:00
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Third we send 3 documents again to replace the existing ones.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
|
|
|
{ "id": 1, "name": "updated second kevin" },
|
|
|
|
{ "id": 2, "name": "updated kevina" },
|
|
|
|
{ "id": 3, "name": "updated benoit" }
|
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
2020-10-30 12:14:25 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
2020-10-30 13:46:56 +01:00
|
|
|
// Check that there is **always** 3 documents.
|
2020-10-30 12:14:25 +01:00
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-10-30 13:46:56 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_document_merge() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with duplicate ids and
|
|
|
|
// change the index method to merge documents.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
|
|
|
{ "id": 1, "name": "kevin" },
|
|
|
|
{ "id": 1, "name": "kevina" },
|
|
|
|
{ "id": 1, "name": "benoit" }
|
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig {
|
|
|
|
update_method: IndexDocumentsMethod::UpdateDocuments,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-10-30 13:46:56 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is only 1 document now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 1);
|
|
|
|
|
|
|
|
// Check that we get only one document from the database.
|
|
|
|
let docs = index.documents(&rtxn, Some(0)).unwrap();
|
|
|
|
assert_eq!(docs.len(), 1);
|
|
|
|
let (id, doc) = docs[0];
|
|
|
|
assert_eq!(id, 0);
|
|
|
|
|
|
|
|
// Check that this document is equal to the last one sent.
|
|
|
|
let mut doc_iter = doc.iter();
|
2021-09-28 18:35:54 +02:00
|
|
|
assert_eq!(doc_iter.next(), Some((0, &b"1"[..])));
|
2020-10-30 13:46:56 +01:00
|
|
|
assert_eq!(doc_iter.next(), Some((1, &br#""benoit""#[..])));
|
|
|
|
assert_eq!(doc_iter.next(), None);
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Second we send 1 document with id 1, to force it to be merged with the previous one.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([ { "id": 1, "age": 25 } ]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-10-30 13:46:56 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is **always** 1 document.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 1);
|
|
|
|
|
|
|
|
// Check that we get only one document from the database.
|
|
|
|
let docs = index.documents(&rtxn, Some(0)).unwrap();
|
|
|
|
assert_eq!(docs.len(), 1);
|
|
|
|
let (id, doc) = docs[0];
|
|
|
|
assert_eq!(id, 0);
|
|
|
|
|
|
|
|
// Check that this document is equal to the last one sent.
|
|
|
|
let mut doc_iter = doc.iter();
|
2021-09-28 18:35:54 +02:00
|
|
|
assert_eq!(doc_iter.next(), Some((0, &b"1"[..])));
|
2020-10-30 13:46:56 +01:00
|
|
|
assert_eq!(doc_iter.next(), Some((1, &br#""benoit""#[..])));
|
2021-09-28 18:35:54 +02:00
|
|
|
assert_eq!(doc_iter.next(), Some((2, &b"25"[..])));
|
2020-10-30 13:46:56 +01:00
|
|
|
assert_eq!(doc_iter.next(), None);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-10-31 12:54:43 +01:00
|
|
|
|
2020-10-31 21:46:55 +01:00
|
|
|
#[test]
|
2021-08-31 11:44:15 +02:00
|
|
|
fn not_auto_generated_documents_ids() {
|
2020-10-31 21:46:55 +01:00
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with ids from 1 to 3.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
2020-10-31 21:46:55 +01:00
|
|
|
{ "name": "kevin" },
|
2021-08-31 11:44:15 +02:00
|
|
|
{ "name": "kevina" },
|
2020-10-31 21:46:55 +01:00
|
|
|
{ "name": "benoit" }
|
2021-08-31 11:44:15 +02:00
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
assert!(builder.add_documents(content).is_err());
|
2020-10-31 21:46:55 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is no document.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 0);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
|
|
|
|
2020-10-31 12:54:43 +01:00
|
|
|
#[test]
|
|
|
|
fn simple_auto_generated_documents_ids() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with ids from 1 to 3.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
|
|
|
{ "name": "kevin" },
|
|
|
|
{ "name": "kevina" },
|
|
|
|
{ "name": "benoit" }
|
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config =
|
|
|
|
IndexDocumentsConfig { autogenerate_docids: true, ..Default::default() };
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-10-31 12:54:43 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 3 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
|
|
|
|
let docs = index.documents(&rtxn, vec![0, 1, 2]).unwrap();
|
|
|
|
let (_id, obkv) = docs.iter().find(|(_id, kv)| kv.get(0) == Some(br#""kevin""#)).unwrap();
|
|
|
|
let kevin_uuid: String = serde_json::from_slice(&obkv.get(1).unwrap()).unwrap();
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Second we send 1 document with the generated uuid, to erase the previous ones.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([ { "name": "updated kevin", "id": kevin_uuid } ]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-10-31 12:54:43 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is **always** 3 documents.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
2020-11-01 12:14:44 +01:00
|
|
|
|
|
|
|
let docs = index.documents(&rtxn, vec![0, 1, 2]).unwrap();
|
2021-06-16 18:33:33 +02:00
|
|
|
let (kevin_id, _) =
|
|
|
|
docs.iter().find(|(_, d)| d.get(0).unwrap() == br#""updated kevin""#).unwrap();
|
2020-11-01 12:14:44 +01:00
|
|
|
let (id, doc) = docs[*kevin_id as usize];
|
|
|
|
assert_eq!(id, *kevin_id);
|
|
|
|
|
|
|
|
// Check that this document is equal to the last
|
|
|
|
// one sent and that an UUID has been generated.
|
2020-11-01 16:43:12 +01:00
|
|
|
assert_eq!(doc.get(0), Some(&br#""updated kevin""#[..]));
|
2020-11-01 12:14:44 +01:00
|
|
|
// This is an UUID, it must be 36 bytes long plus the 2 surrounding string quotes (").
|
2021-03-31 17:14:23 +02:00
|
|
|
assert_eq!(doc.get(1).unwrap().len(), 36 + 2);
|
2020-10-31 12:54:43 +01:00
|
|
|
drop(rtxn);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reordered_auto_generated_documents_ids() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with ids from 1 to 3.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
|
|
|
{ "id": 1, "name": "kevin" },
|
|
|
|
{ "id": 2, "name": "kevina" },
|
|
|
|
{ "id": 3, "name": "benoit" }
|
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-10-31 12:54:43 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 3 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 3);
|
|
|
|
drop(rtxn);
|
|
|
|
|
|
|
|
// Second we send 1 document without specifying the id.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([ { "name": "new kevin" } ]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let indexing_config =
|
|
|
|
IndexDocumentsConfig { autogenerate_docids: true, ..Default::default() };
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-10-31 12:54:43 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 4 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 4);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-10-31 16:10:15 +01:00
|
|
|
|
|
|
|
#[test]
|
2021-08-31 11:44:15 +02:00
|
|
|
fn empty_update() {
|
2020-10-31 16:10:15 +01:00
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 0 documents and only headers.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-10-31 16:10:15 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is no documents.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 0);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-11-01 11:50:10 +01:00
|
|
|
|
2020-11-01 16:43:12 +01:00
|
|
|
#[test]
|
|
|
|
fn invalid_documents_ids() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 1 document with an invalid id.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
// There is a space in the document id.
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([ { "id": "brume bleue", "name": "kevin" } ]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
assert!(builder.add_documents(content).is_err());
|
2020-11-01 16:43:12 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// First we send 1 document with a valid id.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
// There is a space in the document id.
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([ { "id": 32, "name": "kevin" } ]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-11-01 16:43:12 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 1 document now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 1);
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2020-11-06 16:15:07 +01:00
|
|
|
|
|
|
|
#[test]
|
2021-08-31 11:44:15 +02:00
|
|
|
fn complex_documents() {
|
2020-11-06 16:15:07 +01:00
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with an id for only one of them.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
2020-11-06 16:15:07 +01:00
|
|
|
{ "id": 0, "name": "kevin", "object": { "key1": "value1", "key2": "value2" } },
|
|
|
|
{ "id": 1, "name": "kevina", "array": ["I", "am", "fine"] },
|
|
|
|
{ "id": 2, "name": "benoit", "array_of_object": [{ "wow": "amazing" }] }
|
2021-08-31 11:44:15 +02:00
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2020-11-06 16:15:07 +01:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 1 documents now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
// Search for a sub object value
|
|
|
|
let result = index.search(&rtxn).query(r#""value2""#).execute().unwrap();
|
|
|
|
assert_eq!(result.documents_ids, vec![0]);
|
|
|
|
|
|
|
|
// Search for a sub array value
|
|
|
|
let result = index.search(&rtxn).query(r#""fine""#).execute().unwrap();
|
|
|
|
assert_eq!(result.documents_ids, vec![1]);
|
|
|
|
|
|
|
|
// Search for a sub array sub object key
|
|
|
|
let result = index.search(&rtxn).query(r#""wow""#).execute().unwrap();
|
|
|
|
assert_eq!(result.documents_ids, vec![2]);
|
|
|
|
|
|
|
|
drop(rtxn);
|
|
|
|
}
|
2021-06-23 18:35:44 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_documents_replace() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
// First we send 3 documents with an id for only one of them.
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let documents = documents!([
|
2021-08-26 13:27:32 +02:00
|
|
|
{ "id": 2, "title": "Pride and Prejudice", "author": "Jane Austin", "genre": "romance", "price": 3.5, "_geo": { "lat": 12, "lng": 42 } },
|
2021-06-23 18:35:44 +02:00
|
|
|
{ "id": 456, "title": "Le Petit Prince", "author": "Antoine de Saint-Exupéry", "genre": "adventure" , "price": 10.0 },
|
|
|
|
{ "id": 1, "title": "Alice In Wonderland", "author": "Lewis Carroll", "genre": "fantasy", "price": 25.99 },
|
|
|
|
{ "id": 1344, "title": "The Hobbit", "author": "J. R. R. Tolkien", "genre": "fantasy" },
|
|
|
|
{ "id": 4, "title": "Harry Potter and the Half-Blood Prince", "author": "J. K. Rowling", "genre": "fantasy" },
|
2021-08-26 13:27:32 +02:00
|
|
|
{ "id": 42, "title": "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "_geo": { "lat": 35, "lng": 23 } }
|
2021-08-31 11:44:15 +02:00
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig {
|
|
|
|
update_method: IndexDocumentsMethod::ReplaceDocuments,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
|
|
|
builder.add_documents(documents).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-06-23 18:35:44 +02:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-12-08 14:12:07 +01:00
|
|
|
let indexing_config = IndexDocumentsConfig {
|
|
|
|
update_method: IndexDocumentsMethod::UpdateDocuments,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let mut builder = IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| ());
|
2021-08-31 11:44:15 +02:00
|
|
|
let documents = documents!([
|
2021-06-23 18:35:44 +02:00
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"author": "J. Austen",
|
|
|
|
"date": "1813"
|
|
|
|
}
|
2021-08-31 11:44:15 +02:00
|
|
|
]);
|
2021-06-23 18:35:44 +02:00
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
builder.add_documents(documents).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-06-23 18:35:44 +02:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
}
|
2021-06-30 11:23:29 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete_documents_then_insert() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
2021-06-30 11:23:29 +02:00
|
|
|
{ "objectId": 123, "title": "Pride and Prejudice", "comment": "A great book" },
|
|
|
|
{ "objectId": 456, "title": "Le Petit Prince", "comment": "A french book" },
|
|
|
|
{ "objectId": 1, "title": "Alice In Wonderland", "comment": "A weird book" },
|
2021-08-26 13:27:32 +02:00
|
|
|
{ "objectId": 30, "title": "Hamlet", "_geo": { "lat": 12, "lng": 89 } }
|
2021-08-31 11:44:15 +02:00
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-06-30 11:23:29 +02:00
|
|
|
|
|
|
|
assert_eq!(index.primary_key(&wtxn).unwrap(), Some("objectId"));
|
|
|
|
|
|
|
|
// Delete not all of the documents but some of them.
|
2021-11-03 13:12:01 +01:00
|
|
|
let mut builder = DeleteDocuments::new(&mut wtxn, &index).unwrap();
|
2021-06-30 11:23:29 +02:00
|
|
|
builder.delete_external_id("30");
|
|
|
|
builder.execute().unwrap();
|
|
|
|
|
|
|
|
let external_documents_ids = index.external_documents_ids(&wtxn).unwrap();
|
|
|
|
assert!(external_documents_ids.get("30").is_none());
|
|
|
|
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
2021-08-26 13:27:32 +02:00
|
|
|
{ "objectId": 30, "title": "Hamlet", "_geo": { "lat": 12, "lng": 89 } }
|
2021-08-31 11:44:15 +02:00
|
|
|
]);
|
2021-06-30 11:23:29 +02:00
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-06-30 11:23:29 +02:00
|
|
|
let external_documents_ids = index.external_documents_ids(&wtxn).unwrap();
|
|
|
|
assert!(external_documents_ids.get("30").is_some());
|
|
|
|
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
2021-08-26 13:27:32 +02:00
|
|
|
{ "objectId": 30, "title": "Hamlet", "_geo": { "lat": 12, "lng": 89 } }
|
2021-08-31 11:44:15 +02:00
|
|
|
]);
|
2021-12-08 14:12:07 +01:00
|
|
|
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-06-30 11:23:29 +02:00
|
|
|
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
}
|
2021-07-06 11:40:45 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn index_more_than_256_fields() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
|
|
|
|
let mut big_object = HashMap::new();
|
|
|
|
big_object.insert(S("id"), "wow");
|
|
|
|
for i in 0..1000 {
|
|
|
|
let key = i.to_string();
|
|
|
|
big_object.insert(key, "I am a text!");
|
|
|
|
}
|
|
|
|
|
2021-08-31 11:44:15 +02:00
|
|
|
let mut cursor = Cursor::new(Vec::new());
|
2021-07-06 11:40:45 +02:00
|
|
|
|
2021-08-31 11:44:15 +02:00
|
|
|
let mut builder = DocumentBatchBuilder::new(&mut cursor).unwrap();
|
2021-10-24 14:41:36 +02:00
|
|
|
let big_object = Cursor::new(serde_json::to_vec(&big_object).unwrap());
|
|
|
|
builder.extend_from_json(big_object).unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
builder.finish().unwrap();
|
|
|
|
cursor.set_position(0);
|
|
|
|
let content = DocumentBatchReader::from_reader(cursor).unwrap();
|
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-07-06 11:40:45 +02:00
|
|
|
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
}
|
2021-07-22 17:14:44 +02:00
|
|
|
|
2021-09-22 17:48:24 +02:00
|
|
|
#[test]
|
|
|
|
fn index_more_than_1000_positions_in_a_field() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(50 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
|
|
|
|
let mut big_object = HashMap::new();
|
|
|
|
big_object.insert(S("id"), "wow");
|
|
|
|
let content: String =
|
|
|
|
(0..=u16::MAX).into_iter().map(|p| p.to_string()).reduce(|a, b| a + " " + &b).unwrap();
|
|
|
|
big_object.insert("content".to_string(), &content);
|
|
|
|
|
|
|
|
let mut cursor = Cursor::new(Vec::new());
|
|
|
|
|
2021-10-25 17:38:32 +02:00
|
|
|
let big_object = serde_json::to_string(&big_object).unwrap();
|
2021-09-22 17:48:24 +02:00
|
|
|
let mut builder = DocumentBatchBuilder::new(&mut cursor).unwrap();
|
2021-10-25 17:38:32 +02:00
|
|
|
builder.extend_from_json(&mut big_object.as_bytes()).unwrap();
|
2021-09-22 17:48:24 +02:00
|
|
|
builder.finish().unwrap();
|
|
|
|
cursor.set_position(0);
|
|
|
|
let content = DocumentBatchReader::from_reader(cursor).unwrap();
|
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-09-22 17:48:24 +02:00
|
|
|
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
let mut rtxn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
assert!(index.word_docids.get(&mut rtxn, "0").unwrap().is_some());
|
|
|
|
assert!(index.word_docids.get(&mut rtxn, "64").unwrap().is_some());
|
|
|
|
assert!(index.word_docids.get(&mut rtxn, "256").unwrap().is_some());
|
|
|
|
assert!(index.word_docids.get(&mut rtxn, "1024").unwrap().is_some());
|
|
|
|
assert!(index.word_docids.get(&mut rtxn, "32768").unwrap().is_some());
|
|
|
|
assert!(index.word_docids.get(&mut rtxn, "65535").unwrap().is_some());
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:14:44 +02:00
|
|
|
#[test]
|
|
|
|
fn index_documents_with_zeroes() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-08-31 11:44:15 +02:00
|
|
|
let content = documents!([
|
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"title": "Prideand Prejudice",
|
|
|
|
"au{hor": "Jane Austin",
|
|
|
|
"genre": "romance",
|
|
|
|
"price$": "3.5$",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 456,
|
|
|
|
"title": "Le Petit Prince",
|
|
|
|
"au{hor": "Antoine de Saint-Exupéry",
|
|
|
|
"genre": "adventure",
|
|
|
|
"price$": "10.0$",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"title": "Wonderland",
|
|
|
|
"au{hor": "Lewis Carroll",
|
|
|
|
"genre": "fantasy",
|
|
|
|
"price$": "25.99$",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 4,
|
|
|
|
"title": "Harry Potter ing fantasy\0lood Prince",
|
|
|
|
"au{hor": "J. K. Rowling",
|
|
|
|
"genre": "fantasy\0",
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-07-22 17:14:44 +02:00
|
|
|
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
}
|
2021-09-23 15:55:39 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn index_2_times_documents_split_by_zero_document_indexation() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let content = documents!([
|
|
|
|
{"id": 0, "name": "Kerollmops", "score": 78},
|
|
|
|
{"id": 1, "name": "ManyTheFish", "score": 75},
|
|
|
|
{"id": 2, "name": "Ferdi", "score": 39},
|
|
|
|
{"id": 3, "name": "Tommy", "score": 33}
|
|
|
|
]);
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-09-23 15:55:39 +02:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 4 document now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 4);
|
|
|
|
|
|
|
|
let content = documents!([]);
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-09-23 15:55:39 +02:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 4 document now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 4);
|
|
|
|
|
|
|
|
let content = documents!([
|
|
|
|
{"id": 0, "name": "Kerollmops", "score": 78},
|
|
|
|
{"id": 1, "name": "ManyTheFish", "score": 75},
|
|
|
|
{"id": 2, "name": "Ferdi", "score": 39},
|
|
|
|
{"id": 3, "name": "Tommy", "score": 33}
|
|
|
|
]);
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-09-23 15:55:39 +02:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
// Check that there is 4 document now.
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
let count = index.number_of_documents(&rtxn).unwrap();
|
|
|
|
assert_eq!(count, 4);
|
|
|
|
}
|
2021-09-28 12:05:11 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_meilisearch_1714() {
|
|
|
|
let path = tempfile::tempdir().unwrap();
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(10 * 1024 * 1024); // 10 MB
|
|
|
|
let index = Index::new(options, &path).unwrap();
|
|
|
|
|
|
|
|
let content = documents!([
|
|
|
|
{"id": "123", "title": "小化妆包" },
|
|
|
|
{"id": "456", "title": "Ipad 包" }
|
|
|
|
]);
|
|
|
|
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
2021-12-08 14:12:07 +01:00
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let mut builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config.clone(), |_| ());
|
|
|
|
builder.add_documents(content).unwrap();
|
|
|
|
builder.execute().unwrap();
|
2021-09-28 12:05:11 +02:00
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
let rtxn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
// Only the first document should match.
|
|
|
|
let count = index.word_docids.get(&rtxn, "化妆包").unwrap().unwrap().len();
|
|
|
|
assert_eq!(count, 1);
|
|
|
|
|
|
|
|
// Only the second document should match.
|
|
|
|
let count = index.word_docids.get(&rtxn, "包").unwrap().unwrap().len();
|
|
|
|
assert_eq!(count, 1);
|
|
|
|
|
|
|
|
let mut search = crate::Search::new(&rtxn, &index);
|
|
|
|
search.query("化妆包");
|
|
|
|
search.authorize_typos(true);
|
|
|
|
search.optional_words(true);
|
|
|
|
|
|
|
|
// only 1 document should be returned
|
|
|
|
let crate::SearchResult { documents_ids, .. } = search.execute().unwrap();
|
|
|
|
assert_eq!(documents_ids.len(), 1);
|
|
|
|
}
|
2020-10-30 12:14:25 +01:00
|
|
|
}
|