Compare commits

...

6 Commits

Author SHA1 Message Date
YoEight
328718ee90 improve documentation 2025-12-11 16:30:04 -05:00
YoEight
a2878efafe time for some tests 2025-12-11 16:11:22 -05:00
YoEight
f392e0a0f8 threading down on_missing_document param 2025-12-11 15:54:11 -05:00
YoEight
e359325dbd rename to a clearer name 2025-12-11 14:31:16 -05:00
YoEight
d5f66c195d introduce DocumentCreationPolicy 2025-12-11 14:13:11 -05:00
YoEight
9f64b0de66 Allow strict document update without creating missing documents 2025-12-11 12:49:34 -05:00
22 changed files with 212 additions and 86 deletions

View File

@@ -113,9 +113,9 @@ fn main() {
for op in &operations {
match op {
Either::Left(documents) => {
indexer.replace_documents(documents).unwrap()
}
Either::Left(documents) => indexer
.replace_documents(documents, Default::default())
.unwrap(),
Either::Right(ids) => indexer.delete_documents(ids),
}
}

View File

@@ -164,6 +164,7 @@ impl<'a> Dump<'a> {
content_file: content_uuid.ok_or(Error::CorruptedDump)?,
documents_count,
allow_index_creation,
on_missing_document: Default::default(),
},
KindDump::DocumentDeletion { documents_ids } => KindWithContent::DocumentDeletion {
documents_ids,

View File

@@ -40,6 +40,7 @@ fn doc_imp(
content_file: Uuid::new_v4(),
documents_count: 0,
allow_index_creation,
on_missing_document: Default::default(),
}
}

View File

@@ -2,7 +2,7 @@ use std::fmt;
use std::io::ErrorKind;
use meilisearch_types::heed::RoTxn;
use meilisearch_types::milli::update::IndexDocumentsMethod;
use meilisearch_types::milli::update::{IndexDocumentsMethod, MissingDocumentPolicy};
use meilisearch_types::settings::{Settings, Unchecked};
use meilisearch_types::tasks::{BatchStopReason, Kind, KindWithContent, Status, Task};
use roaring::RoaringBitmap;
@@ -63,8 +63,8 @@ pub(crate) enum Batch {
#[derive(Debug)]
pub(crate) enum DocumentOperation {
Replace(Uuid),
Update(Uuid),
Replace { content_file: Uuid, on_missing_document: MissingDocumentPolicy },
Update { content_file: Uuid, on_missing_document: MissingDocumentPolicy },
Delete(Vec<String>),
}
@@ -293,13 +293,22 @@ impl IndexScheduler {
for task in tasks.iter() {
match task.kind {
KindWithContent::DocumentAdditionOrUpdate {
content_file, method, ..
content_file,
method,
on_missing_document,
..
} => match method {
IndexDocumentsMethod::ReplaceDocuments => {
operations.push(DocumentOperation::Replace(content_file))
operations.push(DocumentOperation::Replace {
content_file,
on_missing_document,
})
}
IndexDocumentsMethod::UpdateDocuments => {
operations.push(DocumentOperation::Update(content_file))
operations.push(DocumentOperation::Update {
content_file,
on_missing_document,
})
}
_ => unreachable!("Unknown document merging method"),
},

View File

@@ -77,8 +77,8 @@ impl IndexScheduler {
let mut content_files = Vec::new();
for operation in &operations {
match operation {
DocumentOperation::Replace(content_uuid)
| DocumentOperation::Update(content_uuid) => {
DocumentOperation::Replace { content_file: content_uuid, .. }
| DocumentOperation::Update { content_file: content_uuid, .. } => {
let content_file = self.queue.file_store.get_update(*content_uuid)?;
let mmap = unsafe { memmap2::Mmap::map(&content_file)? };
content_files.push(mmap);
@@ -100,16 +100,16 @@ impl IndexScheduler {
let embedders = self.embedders(index_uid.clone(), embedders)?;
for operation in operations {
match operation {
DocumentOperation::Replace(_content_uuid) => {
DocumentOperation::Replace { content_file: _, on_missing_document } => {
let mmap = content_files_iter.next().unwrap();
indexer
.replace_documents(mmap)
.replace_documents(mmap, on_missing_document)
.map_err(|e| Error::from_milli(e, Some(index_uid.clone())))?;
}
DocumentOperation::Update(_content_uuid) => {
DocumentOperation::Update { content_file: _, on_missing_document } => {
let mmap = content_files_iter.next().unwrap();
indexer
.update_documents(mmap)
.update_documents(mmap, on_missing_document)
.map_err(|e| Error::from_milli(e, Some(index_uid.clone())))?;
}
DocumentOperation::Delete(document_ids) => {

View File

@@ -294,6 +294,7 @@ fn document_addition_and_index_deletion() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -482,6 +483,7 @@ fn document_addition_and_index_deletion_on_unexisting_index() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,

View File

@@ -31,6 +31,7 @@ fn document_addition() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -67,6 +68,7 @@ fn document_addition_and_document_deletion() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -133,6 +135,7 @@ fn document_deletion_and_document_addition() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -185,6 +188,7 @@ fn test_document_replace() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -236,6 +240,7 @@ fn test_document_update() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -289,6 +294,7 @@ fn test_mixed_document_addition() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -340,6 +346,7 @@ fn test_document_replace_without_autobatching() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -395,6 +402,7 @@ fn test_document_update_without_autobatching() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -454,6 +462,7 @@ fn test_document_addition_cant_create_index_without_index() {
content_file: uuid,
documents_count,
allow_index_creation: false,
on_missing_document: Default::default(),
},
None,
false,
@@ -506,6 +515,7 @@ fn test_document_addition_cant_create_index_without_index_without_autobatching()
content_file: uuid,
documents_count,
allow_index_creation: false,
on_missing_document: Default::default(),
},
None,
false,
@@ -568,6 +578,7 @@ fn test_document_addition_cant_create_index_with_index() {
content_file: uuid,
documents_count,
allow_index_creation: false,
on_missing_document: Default::default(),
},
None,
false,
@@ -635,6 +646,7 @@ fn test_document_addition_cant_create_index_with_index_without_autobatching() {
content_file: uuid,
documents_count,
allow_index_creation: false,
on_missing_document: Default::default(),
},
None,
false,
@@ -707,6 +719,7 @@ fn test_document_addition_mixed_rights_with_index() {
content_file: uuid,
documents_count,
allow_index_creation,
on_missing_document: Default::default(),
},
None,
false,
@@ -764,6 +777,7 @@ fn test_document_addition_mixed_right_without_index_starts_with_cant_create() {
content_file: uuid,
documents_count,
allow_index_creation,
on_missing_document: Default::default(),
},
None,
false,
@@ -820,6 +834,7 @@ fn test_document_addition_with_multiple_primary_key() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -883,6 +898,7 @@ fn test_document_addition_with_multiple_primary_key_batch_wrong_key() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -943,6 +959,7 @@ fn test_document_addition_with_bad_primary_key() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -1029,6 +1046,7 @@ fn test_document_addition_with_set_and_null_primary_key() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -1104,6 +1122,7 @@ fn test_document_addition_with_set_and_null_primary_key_inference_works() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,

View File

@@ -173,6 +173,7 @@ fn import_vectors() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -263,6 +264,7 @@ fn import_vectors() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -399,6 +401,7 @@ fn import_vectors_first_and_embedder_later() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -539,6 +542,7 @@ fn import_vectors_first_and_embedder_later() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -640,6 +644,7 @@ fn delete_document_containing_vector() {
content_file: uuid,
documents_count,
allow_index_creation: false,
on_missing_document: Default::default(),
},
None,
false,
@@ -818,6 +823,7 @@ fn delete_embedder_with_user_provided_vectors() {
content_file: uuid,
documents_count,
allow_index_creation: false,
on_missing_document: Default::default(),
},
None,
false,

View File

@@ -52,6 +52,7 @@ fn fail_in_process_batch_for_document_addition() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -94,6 +95,7 @@ fn fail_in_update_task_after_process_batch_success_for_document_addition() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,
@@ -160,6 +162,7 @@ fn fail_in_process_batch_for_document_deletion() {
content_file: uuid,
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
},
None,
false,

View File

@@ -197,6 +197,7 @@ pub(crate) fn replace_document_import_task(
content_file: Uuid::from_u128(content_file_uuid),
documents_count,
allow_index_creation: true,
on_missing_document: Default::default(),
}
}

View File

@@ -255,6 +255,7 @@ InvalidIndexLimit , InvalidRequest , BAD_REQU
InvalidIndexOffset , InvalidRequest , BAD_REQUEST ;
InvalidIndexPrimaryKey , InvalidRequest , BAD_REQUEST ;
InvalidIndexCustomMetadata , InvalidRequest , BAD_REQUEST ;
InvalidSkipCreation , InvalidRequest , BAD_REQUEST ;
InvalidIndexUid , InvalidRequest , BAD_REQUEST ;
InvalidMultiSearchFacets , InvalidRequest , BAD_REQUEST ;
InvalidMultiSearchFacetsByIndex , InvalidRequest , BAD_REQUEST ;

View File

@@ -5,7 +5,7 @@ use std::str::FromStr;
use byte_unit::Byte;
use enum_iterator::Sequence;
use milli::update::IndexDocumentsMethod;
use milli::update::{IndexDocumentsMethod, MissingDocumentPolicy};
use milli::Object;
use roaring::RoaringBitmap;
use serde::{Deserialize, Serialize, Serializer};
@@ -114,6 +114,7 @@ pub enum KindWithContent {
content_file: Uuid,
documents_count: u64,
allow_index_creation: bool,
on_missing_document: MissingDocumentPolicy,
},
DocumentDeletion {
index_uid: String,

View File

@@ -629,7 +629,7 @@ fn import_dump(
let mmap = unsafe { memmap2::Mmap::map(index_reader.documents_file())? };
indexer.replace_documents(&mmap)?;
indexer.replace_documents(&mmap, Default::default())?;
let indexer_config = index_scheduler.indexer_config();
let pool = &indexer_config.thread_pool;

View File

@@ -20,7 +20,7 @@ use meilisearch_types::heed::RoTxn;
use meilisearch_types::index_uid::IndexUid;
use meilisearch_types::milli::documents::sort::recursive_sort;
use meilisearch_types::milli::index::EmbeddingsWithMetadata;
use meilisearch_types::milli::update::IndexDocumentsMethod;
use meilisearch_types::milli::update::{IndexDocumentsMethod, MissingDocumentPolicy};
use meilisearch_types::milli::vector::parsed_vectors::ExplicitVectors;
use meilisearch_types::milli::{AscDesc, DocumentId};
use meilisearch_types::serde_cs::vec::CS;
@@ -687,6 +687,11 @@ pub struct UpdateDocumentsQuery {
#[param(example = "custom")]
#[deserr(default, error = DeserrQueryParamError<InvalidIndexCustomMetadata>)]
pub custom_metadata: Option<String>,
#[param(example = "true")]
#[deserr(default, try_from(&String) = from_string_skip_creation -> DeserrQueryParamError<InvalidSkipCreation>, error = DeserrQueryParamError<InvalidSkipCreation>)]
/// Only update documents if they already exist.
pub skip_creation: Option<bool>,
}
#[derive(Deserialize, Debug, Deserr, IntoParams)]
@@ -711,6 +716,23 @@ fn from_char_csv_delimiter(
}
}
fn from_string_skip_creation(
s: &String,
) -> Result<Option<bool>, DeserrQueryParamError<InvalidSkipCreation>> {
if s.eq_ignore_ascii_case("true") {
return Ok(Some(true));
}
if s.eq_ignore_ascii_case("false") {
return Ok(Some(false));
}
Err(DeserrQueryParamError::new(
format!("skipCreation must be either `true` or `false`. Found: `{}`", s),
Code::InvalidSkipCreation,
))
}
aggregate_methods!(
Replaced => "Documents Added",
Updated => "Documents Updated",
@@ -840,6 +862,7 @@ pub async fn replace_documents(
params.custom_metadata,
dry_run,
allow_index_creation,
params.skip_creation,
&req,
)
.await?;
@@ -943,6 +966,7 @@ pub async fn update_documents(
params.custom_metadata,
dry_run,
allow_index_creation,
params.skip_creation,
&req,
)
.await?;
@@ -963,6 +987,7 @@ async fn document_addition(
custom_metadata: Option<String>,
dry_run: bool,
allow_index_creation: bool,
skip_creation: Option<bool>,
req: &HttpRequest,
) -> Result<SummarizedTaskView, MeilisearchHttpError> {
let mime_type = extract_mime_type(req)?;
@@ -1083,6 +1108,11 @@ async fn document_addition(
primary_key,
allow_index_creation,
index_uid: index_uid.to_string(),
on_missing_document: if matches!(skip_creation, Some(true)) {
MissingDocumentPolicy::Skip
} else {
MissingDocumentPolicy::Create
},
};
let scheduler = index_scheduler.clone();

View File

@@ -64,7 +64,7 @@ pub fn setup_search_index_with_criteria(criteria: &[Criterion]) -> Index {
let payload = unsafe { memmap2::Mmap::map(&file).unwrap() };
// index documents
indexer.replace_documents(&payload).unwrap();
indexer.replace_documents(&payload, Default::default()).unwrap();
let indexer_alloc = Bump::new();
let (document_changes, operation_stats, primary_key) = indexer

View File

@@ -70,9 +70,11 @@ impl TempIndex {
let mut indexer = indexer::DocumentOperation::new();
match self.index_documents_config.update_method {
IndexDocumentsMethod::ReplaceDocuments => {
indexer.replace_documents(&documents).unwrap()
indexer.replace_documents(&documents, Default::default()).unwrap()
}
IndexDocumentsMethod::UpdateDocuments => {
indexer.update_documents(&documents, Default::default()).unwrap()
}
IndexDocumentsMethod::UpdateDocuments => indexer.update_documents(&documents).unwrap(),
}
let indexer_alloc = Bump::new();
@@ -232,7 +234,7 @@ fn aborting_indexation() {
{ "id": 2, "name": "bob", "age": 20 },
{ "id": 2, "name": "bob", "age": 20 },
]);
indexer.replace_documents(&payload).unwrap();
indexer.replace_documents(&payload, Default::default()).unwrap();
let indexer_alloc = Bump::new();
let (document_changes, _operation_stats, primary_key) = indexer

View File

@@ -67,6 +67,21 @@ pub enum IndexDocumentsMethod {
UpdateDocuments,
}
/// Controls whether new documents should be created when they don't already exist.
///
/// This policy is checked when processing a document whose ID is not found in the index.
/// It applies to both update and replace operations.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MissingDocumentPolicy {
/// Create the document if it doesn't exist. This is the default behavior.
#[default]
Create,
/// Skip the document silently if it doesn't exist. No error is returned, the document is simply
/// not indexed.
Skip,
}
pub struct IndexDocuments<'t, 'i, 'a, FP, FA> {
wtxn: &'t mut heed::RwTxn<'i>,
index: &'i Index,
@@ -1971,10 +1986,10 @@ mod tests {
let mut new_fields_ids_map = db_fields_ids_map.clone();
let mut indexer = indexer::DocumentOperation::new();
indexer.replace_documents(&doc1).unwrap();
indexer.replace_documents(&doc2).unwrap();
indexer.replace_documents(&doc3).unwrap();
indexer.replace_documents(&doc4).unwrap();
indexer.replace_documents(&doc1, Default::default()).unwrap();
indexer.replace_documents(&doc2, Default::default()).unwrap();
indexer.replace_documents(&doc3, Default::default()).unwrap();
indexer.replace_documents(&doc4, Default::default()).unwrap();
let indexer_alloc = Bump::new();
let (_document_changes, operation_stats, _primary_key) = indexer
@@ -2024,10 +2039,10 @@ mod tests {
let mut new_fields_ids_map = db_fields_ids_map.clone();
let mut indexer = indexer::DocumentOperation::new();
indexer.replace_documents(&doc1).unwrap();
indexer.update_documents(&doc2).unwrap();
indexer.update_documents(&doc3).unwrap();
indexer.update_documents(&doc4).unwrap();
indexer.replace_documents(&doc1, Default::default()).unwrap();
indexer.update_documents(&doc2, Default::default()).unwrap();
indexer.update_documents(&doc3, Default::default()).unwrap();
indexer.update_documents(&doc4, Default::default()).unwrap();
let indexer_alloc = Bump::new();
let (document_changes, operation_stats, primary_key) = indexer
@@ -2112,11 +2127,11 @@ mod tests {
let mut new_fields_ids_map = db_fields_ids_map.clone();
let mut indexer = indexer::DocumentOperation::new();
indexer.replace_documents(&doc1).unwrap();
indexer.update_documents(&doc2).unwrap();
indexer.update_documents(&doc3).unwrap();
indexer.replace_documents(&doc4).unwrap();
indexer.update_documents(&doc5).unwrap();
indexer.replace_documents(&doc1, Default::default()).unwrap();
indexer.update_documents(&doc2, Default::default()).unwrap();
indexer.update_documents(&doc3, Default::default()).unwrap();
indexer.replace_documents(&doc4, Default::default()).unwrap();
indexer.update_documents(&doc5, Default::default()).unwrap();
let indexer_alloc = Bump::new();
let (document_changes, operation_stats, primary_key) = indexer
@@ -2307,7 +2322,7 @@ mod tests {
let indexer_alloc = Bump::new();
let embedders = RuntimeEmbedders::default();
let mut indexer = indexer::DocumentOperation::new();
indexer.replace_documents(&documents).unwrap();
indexer.replace_documents(&documents, Default::default()).unwrap();
indexer.delete_documents(&["2"]);
let (document_changes, _operation_stats, primary_key) = indexer
.into_changes(
@@ -2362,13 +2377,13 @@ mod tests {
{ "id": 3, "name": "jean", "age": 25 },
]);
let mut indexer = indexer::DocumentOperation::new();
indexer.update_documents(&documents).unwrap();
indexer.update_documents(&documents, Default::default()).unwrap();
let documents = documents!([
{ "id": 2, "catto": "jorts" },
{ "id": 3, "legs": 4 },
]);
indexer.update_documents(&documents).unwrap();
indexer.update_documents(&documents, Default::default()).unwrap();
indexer.delete_documents(&["1", "2"]);
let indexer_alloc = Bump::new();
@@ -2426,7 +2441,7 @@ mod tests {
let indexer_alloc = Bump::new();
let embedders = RuntimeEmbedders::default();
let mut indexer = indexer::DocumentOperation::new();
indexer.update_documents(&documents).unwrap();
indexer.update_documents(&documents, Default::default()).unwrap();
let (document_changes, _operation_stats, primary_key) = indexer
.into_changes(
@@ -2479,7 +2494,7 @@ mod tests {
let indexer_alloc = Bump::new();
let embedders = RuntimeEmbedders::default();
let mut indexer = indexer::DocumentOperation::new();
indexer.update_documents(&documents).unwrap();
indexer.update_documents(&documents, Default::default()).unwrap();
indexer.delete_documents(&["1", "2"]);
let (document_changes, _operation_stats, primary_key) = indexer
@@ -2536,7 +2551,7 @@ mod tests {
{ "id": 2, "doggo": { "name": "jean", "age": 20 } },
{ "id": 3, "name": "bob", "age": 25 },
]);
indexer.update_documents(&documents).unwrap();
indexer.update_documents(&documents, Default::default()).unwrap();
let (document_changes, _operation_stats, primary_key) = indexer
.into_changes(
@@ -2595,7 +2610,7 @@ mod tests {
{ "id": 2, "doggo": { "name": "jean", "age": 20 } },
{ "id": 3, "name": "bob", "age": 25 },
]);
indexer.update_documents(&documents).unwrap();
indexer.update_documents(&documents, Default::default()).unwrap();
indexer.delete_documents(&["1", "2", "1", "2"]);
@@ -2651,7 +2666,7 @@ mod tests {
let documents = documents!([
{ "id": 1, "doggo": "kevin" },
]);
indexer.update_documents(&documents).unwrap();
indexer.update_documents(&documents, Default::default()).unwrap();
let (document_changes, _operation_stats, primary_key) = indexer
.into_changes(
@@ -2705,7 +2720,7 @@ mod tests {
{ "id": 1, "catto": "jorts" },
]);
indexer.replace_documents(&documents).unwrap();
indexer.replace_documents(&documents, Default::default()).unwrap();
let (document_changes, _operation_stats, primary_key) = indexer
.into_changes(
@@ -2916,7 +2931,7 @@ mod tests {
let documents = documents!([
{ "id": 1, "doggo": "bernese" },
]);
indexer.replace_documents(&documents).unwrap();
indexer.replace_documents(&documents, Default::default()).unwrap();
// FINISHING
let (document_changes, _operation_stats, primary_key) = indexer
@@ -2978,7 +2993,7 @@ mod tests {
let documents = documents!([
{ "id": 0, "catto": "jorts" },
]);
indexer.replace_documents(&documents).unwrap();
indexer.replace_documents(&documents, Default::default()).unwrap();
let (document_changes, _operation_stats, primary_key) = indexer
.into_changes(
@@ -3036,7 +3051,7 @@ mod tests {
let documents = documents!([
{ "id": 1, "catto": "jorts" },
]);
indexer.replace_documents(&documents).unwrap();
indexer.replace_documents(&documents, Default::default()).unwrap();
let (document_changes, _operation_stats, primary_key) = indexer
.into_changes(

View File

@@ -21,7 +21,7 @@ use crate::update::new::indexer::current_edition::sharding::Shards;
use crate::update::new::steps::IndexingStep;
use crate::update::new::thread_local::MostlySend;
use crate::update::new::{DocumentIdentifiers, Insertion, Update};
use crate::update::{AvailableIds, IndexDocumentsMethod};
use crate::update::{AvailableIds, IndexDocumentsMethod, MissingDocumentPolicy};
use crate::{DocumentId, Error, FieldsIdsMap, Index, InternalError, Result, UserError};
#[derive(Default)]
@@ -37,20 +37,28 @@ impl<'pl> DocumentOperation<'pl> {
/// Append a replacement of documents.
///
/// The payload is expected to be in the NDJSON format
pub fn replace_documents(&mut self, payload: &'pl Mmap) -> Result<()> {
pub fn replace_documents(
&mut self,
payload: &'pl Mmap,
on_missing_document: MissingDocumentPolicy,
) -> Result<()> {
#[cfg(unix)]
payload.advise(memmap2::Advice::Sequential)?;
self.operations.push(Payload::Replace(&payload[..]));
self.operations.push(Payload::Replace { payload: &payload[..], on_missing_document });
Ok(())
}
/// Append an update of documents.
///
/// The payload is expected to be in the NDJSON format
pub fn update_documents(&mut self, payload: &'pl Mmap) -> Result<()> {
pub fn update_documents(
&mut self,
payload: &'pl Mmap,
on_missing_document: MissingDocumentPolicy,
) -> Result<()> {
#[cfg(unix)]
payload.advise(memmap2::Advice::Sequential)?;
self.operations.push(Payload::Update(&payload[..]));
self.operations.push(Payload::Update { payload: &payload[..], on_missing_document });
Ok(())
}
@@ -98,34 +106,40 @@ impl<'pl> DocumentOperation<'pl> {
let mut bytes = 0;
let result = match operation {
Payload::Replace(payload) => extract_addition_payload_changes(
indexer,
index,
rtxn,
primary_key_from_op,
&mut primary_key,
new_fields_ids_map,
&mut available_docids,
&mut bytes,
&docids_version_offsets,
IndexDocumentsMethod::ReplaceDocuments,
shards,
payload,
),
Payload::Update(payload) => extract_addition_payload_changes(
indexer,
index,
rtxn,
primary_key_from_op,
&mut primary_key,
new_fields_ids_map,
&mut available_docids,
&mut bytes,
&docids_version_offsets,
IndexDocumentsMethod::UpdateDocuments,
shards,
payload,
),
Payload::Replace { payload, on_missing_document } => {
extract_addition_payload_changes(
indexer,
index,
rtxn,
primary_key_from_op,
&mut primary_key,
new_fields_ids_map,
&mut available_docids,
&mut bytes,
&docids_version_offsets,
IndexDocumentsMethod::ReplaceDocuments,
shards,
payload,
on_missing_document,
)
}
Payload::Update { payload, on_missing_document } => {
extract_addition_payload_changes(
indexer,
index,
rtxn,
primary_key_from_op,
&mut primary_key,
new_fields_ids_map,
&mut available_docids,
&mut bytes,
&docids_version_offsets,
IndexDocumentsMethod::UpdateDocuments,
shards,
payload,
on_missing_document,
)
}
Payload::Deletion(to_delete) => extract_deletion_payload_changes(
index,
rtxn,
@@ -180,6 +194,7 @@ fn extract_addition_payload_changes<'r, 'pl: 'r>(
method: IndexDocumentsMethod,
shards: Option<&Shards>,
payload: &'pl [u8],
on_missing_document: MissingDocumentPolicy,
) -> Result<hashbrown::HashMap<&'pl str, PayloadOperations<'pl>>> {
use IndexDocumentsMethod::{ReplaceDocuments, UpdateDocuments};
@@ -271,6 +286,10 @@ fn extract_addition_payload_changes<'r, 'pl: 'r>(
match method {
ReplaceDocuments => {
if matches!(on_missing_document, MissingDocumentPolicy::Skip) {
continue;
}
entry.insert(PayloadOperations::new_replacement(
docid,
true, // is new
@@ -278,6 +297,10 @@ fn extract_addition_payload_changes<'r, 'pl: 'r>(
));
}
UpdateDocuments => {
if matches!(on_missing_document, MissingDocumentPolicy::Skip) {
continue;
}
entry.insert(PayloadOperations::new_update(
docid,
true, // is new
@@ -297,6 +320,12 @@ fn extract_addition_payload_changes<'r, 'pl: 'r>(
},
Entry::Vacant(entry) => match method {
ReplaceDocuments => {
if payload_operations.is_new
&& matches!(on_missing_document, MissingDocumentPolicy::Skip)
{
continue;
}
entry.insert(PayloadOperations::new_replacement(
payload_operations.docid,
payload_operations.is_new,
@@ -304,6 +333,12 @@ fn extract_addition_payload_changes<'r, 'pl: 'r>(
));
}
UpdateDocuments => {
if payload_operations.is_new
&& matches!(on_missing_document, MissingDocumentPolicy::Skip)
{
continue;
}
entry.insert(PayloadOperations::new_update(
payload_operations.docid,
payload_operations.is_new,
@@ -448,8 +483,8 @@ pub struct DocumentOperationChanges<'pl> {
}
pub enum Payload<'pl> {
Replace(&'pl [u8]),
Update(&'pl [u8]),
Replace { payload: &'pl [u8], on_missing_document: MissingDocumentPolicy },
Update { payload: &'pl [u8], on_missing_document: MissingDocumentPolicy },
Deletion(&'pl [&'pl str]),
}

View File

@@ -47,7 +47,7 @@ fn test_facet_distribution_with_no_facet_values() {
let documents = mmap_from_objects(vec![doc1, doc2]);
// index documents
indexer.replace_documents(&documents).unwrap();
indexer.replace_documents(&documents, Default::default()).unwrap();
let indexer_alloc = Bump::new();
let (document_changes, _operation_stats, primary_key) = indexer

View File

@@ -85,7 +85,7 @@ pub fn setup_search_index_with_criteria(criteria: &[Criterion]) -> Index {
let payload = unsafe { memmap2::Mmap::map(&file).unwrap() };
// index documents
indexer.replace_documents(&payload).unwrap();
indexer.replace_documents(&payload, Default::default()).unwrap();
let indexer_alloc = Bump::new();
let (document_changes, operation_stats, primary_key) = indexer

View File

@@ -319,7 +319,7 @@ fn criteria_ascdesc() {
file.sync_all().unwrap();
let payload = unsafe { memmap2::Mmap::map(&file).unwrap() };
indexer.replace_documents(&payload).unwrap();
indexer.replace_documents(&payload, Default::default()).unwrap();
let (document_changes, _operation_stats, primary_key) = indexer
.into_changes(
&indexer_alloc,

View File

@@ -126,7 +126,7 @@ fn test_typo_disabled_on_word() {
let embedders = RuntimeEmbedders::default();
let mut indexer = indexer::DocumentOperation::new();
indexer.replace_documents(&documents).unwrap();
indexer.replace_documents(&documents, Default::default()).unwrap();
let indexer_alloc = Bump::new();
let (document_changes, _operation_stats, primary_key) = indexer