Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit d3731dd

Browse files
Merge #706
706: Limit the reindexing caused by updating settings when not needed r=curquiza a=GregoryConrad ## What does this PR do? When updating index settings using `update::Settings`, sometimes a `reindex` of `update::Settings` is triggered when it doesn't need to be. This PR aims to prevent those unnecessary `reindex` calls. For reference, here is a snippet from the current `execute` method in `update::Settings`: ```rust // ... if stop_words_updated || faceted_updated || synonyms_updated || searchable_updated || exact_attributes_updated { self.reindex(&progress_callback, &should_abort, old_fields_ids_map)?; } ``` - [x] `faceted_updated` - looks good as-is ✅ - [x] `stop_words_updated` - looks good as-is ✅ - [x] `synonyms_updated` - looks good as-is ✅ - [x] `searchable_updated` - fixed in this PR - [x] `exact_attributes_updated` - fixed in this PR ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Gregory Conrad <[email protected]>
2 parents 51a2613 + 87e2bc3 commit d3731dd

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

milli/src/index.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,9 @@ impl Index {
560560
}
561561

562562
pub(crate) fn delete_all_searchable_fields(&self, wtxn: &mut RwTxn) -> heed::Result<bool> {
563-
self.delete_searchable_fields(wtxn)?;
564-
self.delete_user_defined_searchable_fields(wtxn)
563+
let did_delete_searchable = self.delete_searchable_fields(wtxn)?;
564+
let did_delete_user_defined = self.delete_user_defined_searchable_fields(wtxn)?;
565+
Ok(did_delete_searchable || did_delete_user_defined)
565566
}
566567

567568
/// Writes the searchable fields, when this list is specified, only these are indexed.
@@ -1145,9 +1146,8 @@ impl Index {
11451146
}
11461147

11471148
/// Clears the exact attributes from the store.
1148-
pub(crate) fn delete_exact_attributes(&self, txn: &mut RwTxn) -> Result<()> {
1149-
self.main.delete::<_, Str>(txn, main_key::EXACT_ATTRIBUTES)?;
1150-
Ok(())
1149+
pub(crate) fn delete_exact_attributes(&self, txn: &mut RwTxn) -> heed::Result<bool> {
1150+
self.main.delete::<_, Str>(txn, main_key::EXACT_ATTRIBUTES)
11511151
}
11521152

11531153
pub fn max_values_per_facet(&self, txn: &RoTxn) -> heed::Result<Option<usize>> {

milli/src/update/settings.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,21 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> {
349349
fn update_searchable(&mut self) -> Result<bool> {
350350
match self.searchable_fields {
351351
Setting::Set(ref fields) => {
352+
// Check to see if the searchable fields changed before doing anything else
353+
let old_fields = self.index.searchable_fields(self.wtxn)?;
354+
let did_change = match old_fields {
355+
// If old_fields is Some, let's check to see if the fields actually changed
356+
Some(old_fields) => {
357+
let new_fields = fields.iter().map(String::as_str).collect::<Vec<_>>();
358+
new_fields != old_fields
359+
}
360+
// If old_fields is None, the fields have changed (because they are being set)
361+
None => true,
362+
};
363+
if !did_change {
364+
return Ok(false);
365+
}
366+
352367
// every time the searchable attributes are updated, we need to update the
353368
// ids for any settings that uses the facets. (distinct_fields, filterable_fields).
354369
let old_fields_ids_map = self.index.fields_ids_map(self.wtxn)?;
@@ -373,13 +388,11 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> {
373388
&new_fields_ids_map,
374389
)?;
375390
self.index.put_fields_ids_map(self.wtxn, &new_fields_ids_map)?;
391+
Ok(true)
376392
}
377-
Setting::Reset => {
378-
self.index.delete_all_searchable_fields(self.wtxn)?;
379-
}
380-
Setting::NotSet => return Ok(false),
393+
Setting::Reset => Ok(self.index.delete_all_searchable_fields(self.wtxn)?),
394+
Setting::NotSet => Ok(false),
381395
}
382-
Ok(true)
383396
}
384397

385398
fn update_stop_words(&mut self) -> Result<bool> {
@@ -465,14 +478,18 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> {
465478
fn update_exact_attributes(&mut self) -> Result<bool> {
466479
match self.exact_attributes {
467480
Setting::Set(ref attrs) => {
468-
let attrs = attrs.iter().map(String::as_str).collect::<Vec<_>>();
469-
self.index.put_exact_attributes(self.wtxn, &attrs)?;
470-
Ok(true)
471-
}
472-
Setting::Reset => {
473-
self.index.delete_exact_attributes(self.wtxn)?;
474-
Ok(true)
481+
let old_attrs = self.index.exact_attributes(self.wtxn)?;
482+
let old_attrs = old_attrs.into_iter().map(String::from).collect::<HashSet<_>>();
483+
484+
if attrs != &old_attrs {
485+
let attrs = attrs.iter().map(String::as_str).collect::<Vec<_>>();
486+
self.index.put_exact_attributes(self.wtxn, &attrs)?;
487+
Ok(true)
488+
} else {
489+
Ok(false)
490+
}
475491
}
492+
Setting::Reset => Ok(self.index.delete_exact_attributes(self.wtxn)?),
476493
Setting::NotSet => Ok(false),
477494
}
478495
}

0 commit comments

Comments
 (0)