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

Commit 302d6cc

Browse files
bors[bot]loiclec
andauthored
Merge #761
761: Integrate deserr r=irevoire a=loiclec 1. `Setting<T>` now implements `DeserializeFromValue` 2. The settings now store ranking rules as strongly typed `Criterion` instead of `String`, since the validation of the ranking rules will be done on meilisearch's side from now on Co-authored-by: Loïc Lecrenier <loic.lecrenier@me.com>
2 parents 21b7d70 + 02fd06e commit 302d6cc

File tree

9 files changed

+45
-29
lines changed

9 files changed

+45
-29
lines changed

benchmarks/benches/utils.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ use std::fs::{create_dir_all, remove_dir_all, File};
44
use std::io::{self, BufRead, BufReader, Cursor, Read, Seek};
55
use std::num::ParseFloatError;
66
use std::path::Path;
7+
use std::str::FromStr;
78

89
use criterion::BenchmarkId;
910
use milli::documents::{DocumentsBatchBuilder, DocumentsBatchReader};
1011
use milli::heed::EnvOpenOptions;
1112
use milli::update::{
1213
IndexDocuments, IndexDocumentsConfig, IndexDocumentsMethod, IndexerConfig, Settings,
1314
};
14-
use milli::{Filter, Index, Object, TermsMatchingStrategy};
15+
use milli::{Criterion, Filter, Index, Object, TermsMatchingStrategy};
1516
use serde_json::Value;
1617

1718
pub struct Conf<'a> {
@@ -80,7 +81,7 @@ pub fn base_setup(conf: &Conf) -> Index {
8081
builder.reset_criteria();
8182
builder.reset_stop_words();
8283

83-
let criterion = criterion.iter().map(|s| s.to_string()).collect();
84+
let criterion = criterion.iter().map(|s| Criterion::from_str(s).unwrap()).collect();
8485
builder.set_criteria(criterion);
8586
}
8687

cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ impl Performer for SettingsUpdate {
521521

522522
if let Some(criteria) = self.criteria {
523523
if !criteria.is_empty() {
524-
update.set_criteria(criteria);
524+
update.set_criteria(criteria.iter().map(|c| c.parse()).collect::<Result<_, _>>()?);
525525
} else {
526526
update.reset_criteria();
527527
}

milli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ byteorder = "1.4.3"
1212
charabia = { version = "0.7.0", default-features = false }
1313
concat-arrays = "0.1.2"
1414
crossbeam-channel = "0.5.6"
15+
deserr = "0.1.4"
1516
either = "1.8.0"
1617
flatten-serde-json = { path = "../flatten-serde-json" }
1718
fst = "0.4.7"

milli/src/search/criteria/asc_desc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ mod tests {
343343
use maplit::hashset;
344344

345345
use crate::index::tests::TempIndex;
346-
use crate::{AscDesc, Filter, Search, SearchResult};
346+
use crate::{AscDesc, Criterion, Filter, Search, SearchResult};
347347

348348
// Note that in this test, only the iterative sort algorithms are used. Set the CANDIDATES_THESHOLD
349349
// constant to 0 to ensure that the other sort algorithms are also correct.
@@ -356,7 +356,7 @@ mod tests {
356356
settings.set_primary_key("id".to_owned());
357357
settings
358358
.set_sortable_fields(maplit::hashset! { S("id"), S("mod_10"), S("mod_20") });
359-
settings.set_criteria(vec!["sort".to_owned()]);
359+
settings.set_criteria(vec![Criterion::Sort]);
360360
})
361361
.unwrap();
362362

@@ -443,7 +443,7 @@ mod tests {
443443
settings.set_primary_key("id".to_owned());
444444
settings.set_filterable_fields(hashset! { S("id"), S("mod_10"), S("mod_20") });
445445
settings.set_sortable_fields(hashset! { S("id"), S("mod_10"), S("mod_20") });
446-
settings.set_criteria(vec!["sort".to_owned()]);
446+
settings.set_criteria(vec![Criterion::Sort]);
447447
})
448448
.unwrap();
449449

milli/src/search/criteria/exactness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ mod tests {
497497
create_disjoint_combinations, create_non_disjoint_combinations,
498498
};
499499
use crate::snapshot_tests::display_bitmap;
500-
use crate::SearchResult;
500+
use crate::{Criterion, SearchResult};
501501

502502
#[test]
503503
fn test_exact_words_subcriterion() {
@@ -506,7 +506,7 @@ mod tests {
506506
index
507507
.update_settings(|settings| {
508508
settings.set_primary_key(S("id"));
509-
settings.set_criteria(vec!["exactness".to_owned()]);
509+
settings.set_criteria(vec![Criterion::Exactness]);
510510
})
511511
.unwrap();
512512

milli/src/search/criteria/proximity.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ mod tests {
599599

600600
use crate::documents::{DocumentsBatchBuilder, DocumentsBatchReader};
601601
use crate::index::tests::TempIndex;
602-
use crate::{CriterionImplementationStrategy, SearchResult};
602+
use crate::{Criterion, CriterionImplementationStrategy, SearchResult};
603603

604604
fn documents_with_enough_different_words_for_prefixes(prefixes: &[&str]) -> Vec<crate::Object> {
605605
let mut documents = Vec::new();
@@ -627,9 +627,9 @@ mod tests {
627627
.update_settings(|settings| {
628628
settings.set_primary_key(S("id"));
629629
settings.set_criteria(vec![
630-
"words".to_owned(),
631-
"typo".to_owned(),
632-
"proximity".to_owned(),
630+
Criterion::Words,
631+
Criterion::Typo,
632+
Criterion::Proximity,
633633
]);
634634
})
635635
.unwrap();

milli/src/update/settings.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::{BTreeSet, HashMap, HashSet};
22
use std::result::Result as StdResult;
33

44
use charabia::{Tokenizer, TokenizerBuilder};
5+
use deserr::{DeserializeError, DeserializeFromValue};
56
use itertools::Itertools;
67
use serde::{Deserialize, Deserializer, Serialize, Serializer};
78
use time::OffsetDateTime;
@@ -22,6 +23,25 @@ pub enum Setting<T> {
2223
NotSet,
2324
}
2425

26+
impl<T, E> DeserializeFromValue<E> for Setting<T>
27+
where
28+
T: DeserializeFromValue<E>,
29+
E: DeserializeError,
30+
{
31+
fn deserialize_from_value<V: deserr::IntoValue>(
32+
value: deserr::Value<V>,
33+
location: deserr::ValuePointerRef,
34+
) -> std::result::Result<Self, E> {
35+
match value {
36+
deserr::Value::Null => Ok(Setting::Reset),
37+
_ => T::deserialize_from_value(value, location).map(Setting::Set),
38+
}
39+
}
40+
fn default() -> Option<Self> {
41+
Some(Self::NotSet)
42+
}
43+
}
44+
2545
impl<T> Default for Setting<T> {
2646
fn default() -> Self {
2747
Self::NotSet
@@ -93,7 +113,7 @@ pub struct Settings<'a, 't, 'u, 'i> {
93113
displayed_fields: Setting<Vec<String>>,
94114
filterable_fields: Setting<HashSet<String>>,
95115
sortable_fields: Setting<HashSet<String>>,
96-
criteria: Setting<Vec<String>>,
116+
criteria: Setting<Vec<Criterion>>,
97117
stop_words: Setting<BTreeSet<String>>,
98118
distinct_field: Setting<String>,
99119
synonyms: Setting<HashMap<String, Vec<String>>>,
@@ -173,7 +193,7 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> {
173193
self.criteria = Setting::Reset;
174194
}
175195

176-
pub fn set_criteria(&mut self, criteria: Vec<String>) {
196+
pub fn set_criteria(&mut self, criteria: Vec<Criterion>) {
177197
self.criteria = Setting::Set(criteria);
178198
}
179199

@@ -526,14 +546,9 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> {
526546
}
527547

528548
fn update_criteria(&mut self) -> Result<()> {
529-
match self.criteria {
530-
Setting::Set(ref fields) => {
531-
let mut new_criteria = Vec::new();
532-
for name in fields {
533-
let criterion: Criterion = name.parse()?;
534-
new_criteria.push(criterion);
535-
}
536-
self.index.put_criteria(self.wtxn, &new_criteria)?;
549+
match &self.criteria {
550+
Setting::Set(criteria) => {
551+
self.index.put_criteria(self.wtxn, criteria)?;
537552
}
538553
Setting::Reset => {
539554
self.index.delete_criteria(self.wtxn)?;
@@ -977,7 +992,7 @@ mod tests {
977992
index
978993
.update_settings(|settings| {
979994
settings.set_displayed_fields(vec![S("name")]);
980-
settings.set_criteria(vec![S("age:asc")]);
995+
settings.set_criteria(vec![Criterion::Asc("age".to_owned())]);
981996
})
982997
.unwrap();
983998

@@ -1246,7 +1261,7 @@ mod tests {
12461261
.update_settings(|settings| {
12471262
settings.set_displayed_fields(vec!["hello".to_string()]);
12481263
settings.set_filterable_fields(hashset! { S("age"), S("toto") });
1249-
settings.set_criteria(vec!["toto:asc".to_string()]);
1264+
settings.set_criteria(vec![Criterion::Asc(S("toto"))]);
12501265
})
12511266
.unwrap();
12521267

@@ -1280,7 +1295,7 @@ mod tests {
12801295
.update_settings(|settings| {
12811296
settings.set_displayed_fields(vec!["hello".to_string()]);
12821297
// It is only Asc(toto), there is a facet database but it is denied to filter with toto.
1283-
settings.set_criteria(vec!["toto:asc".to_string()]);
1298+
settings.set_criteria(vec![Criterion::Asc(S("toto"))]);
12841299
})
12851300
.unwrap();
12861301

milli/tests/search/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ pub fn setup_search_index_with_criteria(criteria: &[Criterion]) -> Index {
3838

3939
let mut builder = Settings::new(&mut wtxn, &index, &config);
4040

41-
let criteria = criteria.iter().map(|c| c.to_string()).collect();
42-
builder.set_criteria(criteria);
41+
builder.set_criteria(criteria.to_vec());
4342
builder.set_filterable_fields(hashset! {
4443
S("tag"),
4544
S("asc_desc_rank"),

milli/tests/search/query_criteria.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ fn criteria_mixup() {
344344
//update criteria
345345
let mut wtxn = index.write_txn().unwrap();
346346
let mut builder = Settings::new(&mut wtxn, &index, &config);
347-
builder.set_criteria(criteria.iter().map(ToString::to_string).collect());
347+
builder.set_criteria(criteria.clone());
348348
builder.execute(|_| (), || false).unwrap();
349349
wtxn.commit().unwrap();
350350

@@ -436,7 +436,7 @@ fn criteria_ascdesc() {
436436

437437
let mut wtxn = index.write_txn().unwrap();
438438
let mut builder = Settings::new(&mut wtxn, &index, &config);
439-
builder.set_criteria(vec![criterion.to_string()]);
439+
builder.set_criteria(vec![criterion.clone()]);
440440
builder.execute(|_| (), || false).unwrap();
441441
wtxn.commit().unwrap();
442442

0 commit comments

Comments
 (0)