This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
238: Integration tests on filters and distinct r=Kerollmops a=ManyTheFish Fix #216 Fix #120 Co-authored-by: many <maxime@meilisearch.com>
- Loading branch information
Showing
5 changed files
with
281 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::collections::HashSet; | ||
|
||
use big_s::S; | ||
use milli::update::Settings; | ||
use milli::{Criterion, Search, SearchResult}; | ||
use Criterion::*; | ||
|
||
use crate::search::{self, EXTERNAL_DOCUMENTS_IDS}; | ||
|
||
macro_rules! test_distinct { | ||
($func:ident, $distinct:ident, $criteria:expr) => { | ||
#[test] | ||
fn $func() { | ||
let criteria = $criteria; | ||
let index = search::setup_search_index_with_criteria(&criteria); | ||
|
||
// update distinct attribute | ||
let mut wtxn = index.write_txn().unwrap(); | ||
let mut builder = Settings::new(&mut wtxn, &index, 0); | ||
builder.set_distinct_field(S(stringify!($distinct))); | ||
builder.execute(|_, _| ()).unwrap(); | ||
wtxn.commit().unwrap(); | ||
|
||
let rtxn = index.read_txn().unwrap(); | ||
|
||
let mut search = Search::new(&rtxn, &index); | ||
search.query(search::TEST_QUERY); | ||
search.limit(EXTERNAL_DOCUMENTS_IDS.len()); | ||
search.authorize_typos(true); | ||
search.optional_words(true); | ||
|
||
let SearchResult { documents_ids, .. } = search.execute().unwrap(); | ||
|
||
let mut distinct_values = HashSet::new(); | ||
let expected_external_ids: Vec<_> = search::expected_order(&criteria, true, true) | ||
.into_iter() | ||
.filter_map(|d| { | ||
if distinct_values.contains(&d.$distinct) { | ||
None | ||
} else { | ||
distinct_values.insert(d.$distinct.to_owned()); | ||
Some(d.id) | ||
} | ||
}) | ||
.collect(); | ||
|
||
let documents_ids = search::internal_to_external_ids(&index, &documents_ids); | ||
assert_eq!(documents_ids, expected_external_ids); | ||
} | ||
}; | ||
} | ||
|
||
test_distinct!( | ||
distinct_string_default_criteria, | ||
tag, | ||
vec![Words, Typo, Proximity, Attribute, Exactness] | ||
); | ||
test_distinct!( | ||
distinct_number_default_criteria, | ||
asc_desc_rank, | ||
vec![Words, Typo, Proximity, Attribute, Exactness] | ||
); | ||
test_distinct!(distinct_string_criterion_words, tag, vec![Words]); | ||
test_distinct!(distinct_number_criterion_words, asc_desc_rank, vec![Words]); | ||
test_distinct!(distinct_string_criterion_words_typo, tag, vec![Words, Typo]); | ||
test_distinct!(distinct_number_criterion_words_typo, asc_desc_rank, vec![Words, Typo]); | ||
test_distinct!(distinct_string_criterion_words_proximity, tag, vec![Words, Proximity]); | ||
test_distinct!(distinct_number_criterion_words_proximity, asc_desc_rank, vec![Words, Proximity]); | ||
test_distinct!(distinct_string_criterion_words_attribute, tag, vec![Words, Attribute]); | ||
test_distinct!(distinct_number_criterion_words_attribute, asc_desc_rank, vec![Words, Attribute]); | ||
test_distinct!(distinct_string_criterion_words_exactness, tag, vec![Words, Exactness]); | ||
test_distinct!(distinct_number_criterion_words_exactness, asc_desc_rank, vec![Words, Exactness]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use either::{Either, Left, Right}; | ||
use milli::{Criterion, FilterCondition, Search, SearchResult}; | ||
use Criterion::*; | ||
|
||
use crate::search::{self, EXTERNAL_DOCUMENTS_IDS}; | ||
|
||
macro_rules! test_filter { | ||
($func:ident, $filter:expr) => { | ||
#[test] | ||
fn $func() { | ||
let criteria = vec![Words, Typo, Proximity, Attribute, Exactness]; | ||
let index = search::setup_search_index_with_criteria(&criteria); | ||
let rtxn = index.read_txn().unwrap(); | ||
|
||
let filter_conditions = | ||
FilterCondition::from_array::<Vec<Either<Vec<&str>, &str>>, _, _, _>( | ||
&rtxn, &index, $filter, | ||
) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
let mut search = Search::new(&rtxn, &index); | ||
search.query(search::TEST_QUERY); | ||
search.limit(EXTERNAL_DOCUMENTS_IDS.len()); | ||
search.authorize_typos(true); | ||
search.optional_words(true); | ||
search.filter(filter_conditions); | ||
|
||
let SearchResult { documents_ids, .. } = search.execute().unwrap(); | ||
|
||
let filtered_ids = search::expected_filtered_ids($filter); | ||
let expected_external_ids: Vec<_> = search::expected_order(&criteria, true, true) | ||
.into_iter() | ||
.filter_map(|d| if filtered_ids.contains(&d.id) { Some(d.id) } else { None }) | ||
.collect(); | ||
|
||
let documents_ids = search::internal_to_external_ids(&index, &documents_ids); | ||
assert_eq!(documents_ids, expected_external_ids); | ||
} | ||
}; | ||
} | ||
|
||
test_filter!(eq_simple_string_filter, vec![Right("tag=red")]); | ||
test_filter!(eq_simple_number_filter, vec![Right("asc_desc_rank=1")]); | ||
test_filter!(eq_string_and_filter_return_empty, vec![Right("tag=red"), Right("tag=green")]); | ||
test_filter!(eq_mix_and_filter, vec![Right("tag=red"), Right("asc_desc_rank=1")]); | ||
test_filter!(eq_string_or_filter, vec![Left(vec!["tag=red", "tag=green"])]); | ||
test_filter!(eq_mix_or_filter, vec![Left(vec!["tag=red", "asc_desc_rank=1"])]); | ||
test_filter!(eq_number_or_filter, vec![Left(vec!["asc_desc_rank=3", "asc_desc_rank=1"])]); | ||
test_filter!(eq_complex_filter, vec![Left(vec!["tag=red", "tag=green"]), Right("asc_desc_rank=3")]); | ||
test_filter!( | ||
eq_complex_filter_2, | ||
vec![Left(vec!["tag=red", "tag=green"]), Left(vec!["asc_desc_rank=3", "asc_desc_rank=1"])] | ||
); | ||
test_filter!(greater_simple_number_filter, vec![Right("asc_desc_rank>1")]); | ||
test_filter!(greater_mix_and_filter, vec![Right("tag=red"), Right("asc_desc_rank>1")]); | ||
test_filter!(greater_mix_or_filter, vec![Left(vec!["tag=red", "asc_desc_rank>1"])]); | ||
test_filter!(greater_number_or_filter, vec![Left(vec!["asc_desc_rank>3", "asc_desc_rank>1"])]); | ||
test_filter!( | ||
greater_complex_filter, | ||
vec![Left(vec!["tag=red", "tag=green"]), Right("asc_desc_rank>3")] | ||
); | ||
test_filter!( | ||
greater_complex_filter_2, | ||
vec![Left(vec!["tag=red", "tag=green"]), Left(vec!["asc_desc_rank>3", "asc_desc_rank>1"])] | ||
); | ||
test_filter!(lower_simple_number_filter, vec![Right("asc_desc_rank<1")]); | ||
test_filter!(lower_mix_and_filter, vec![Right("tag=red"), Right("asc_desc_rank<1")]); | ||
test_filter!(lower_mix_or_filter, vec![Left(vec!["tag=red", "asc_desc_rank<1"])]); | ||
test_filter!(lower_number_or_filter, vec![Left(vec!["asc_desc_rank<3", "asc_desc_rank<1"])]); | ||
test_filter!( | ||
lower_complex_filter, | ||
vec![Left(vec!["tag=red", "tag=green"]), Right("asc_desc_rank<3")] | ||
); | ||
test_filter!( | ||
lower_complex_filter_2, | ||
vec![Left(vec!["tag=red", "tag=green"]), Left(vec!["asc_desc_rank<3", "asc_desc_rank<1"])] | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters