489: fix distinct count bug r=curquiza a=MarinPostma

fix https://github.com/meilisearch/meilisearch/issues/2152

I think the issue was that we didn't take off the excluded candidates from the initial candidates when returning the candidates with the search result.


Co-authored-by: ad hoc <postma.marin@protonmail.com>
This commit is contained in:
bors[bot] 2022-04-13 10:15:30 +00:00 committed by GitHub
commit 3828635fb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 15 deletions

View File

@ -469,6 +469,8 @@ struct SettingsUpdate {
criteria: Option<Vec<String>>, criteria: Option<Vec<String>>,
#[structopt(long)] #[structopt(long)]
exact_attributes: Option<Vec<String>>, exact_attributes: Option<Vec<String>>,
#[structopt(long)]
distinct_attribute: Option<String>,
} }
impl Performer for SettingsUpdate { impl Performer for SettingsUpdate {
@ -503,6 +505,14 @@ impl Performer for SettingsUpdate {
} }
} }
if let Some(distinct_attr) = self.distinct_attribute {
if !distinct_attr.is_empty() {
update.set_distinct_field(distinct_attr);
} else {
update.reset_distinct_field();
}
}
let mut bars = Vec::new(); let mut bars = Vec::new();
let progesses = MultiProgress::new(); let progesses = MultiProgress::new();
for _ in 0..4 { for _ in 0..4 {

View File

@ -243,7 +243,11 @@ impl<'a> Search<'a> {
excluded_candidates = candidates.into_excluded(); excluded_candidates = candidates.into_excluded();
} }
Ok(SearchResult { matching_words, candidates: initial_candidates, documents_ids }) Ok(SearchResult {
matching_words,
candidates: initial_candidates - excluded_candidates,
documents_ids,
})
} }
} }

View File

@ -8,7 +8,7 @@ use Criterion::*;
use crate::search::{self, EXTERNAL_DOCUMENTS_IDS}; use crate::search::{self, EXTERNAL_DOCUMENTS_IDS};
macro_rules! test_distinct { macro_rules! test_distinct {
($func:ident, $distinct:ident, $criteria:expr) => { ($func:ident, $distinct:ident, $criteria:expr, $n_res:expr) => {
#[test] #[test]
fn $func() { fn $func() {
let criteria = $criteria; let criteria = $criteria;
@ -30,7 +30,9 @@ macro_rules! test_distinct {
search.authorize_typos(true); search.authorize_typos(true);
search.optional_words(true); search.optional_words(true);
let SearchResult { documents_ids, .. } = search.execute().unwrap(); let SearchResult { documents_ids, candidates, .. } = search.execute().unwrap();
assert_eq!(candidates.len(), $n_res);
let mut distinct_values = HashSet::new(); let mut distinct_values = HashSet::new();
let expected_external_ids: Vec<_> = search::expected_order(&criteria, true, true, &[]) let expected_external_ids: Vec<_> = search::expected_order(&criteria, true, true, &[])
@ -54,20 +56,22 @@ macro_rules! test_distinct {
test_distinct!( test_distinct!(
distinct_string_default_criteria, distinct_string_default_criteria,
tag, tag,
vec![Words, Typo, Proximity, Attribute, Exactness] vec![Words, Typo, Proximity, Attribute, Exactness],
3
); );
test_distinct!( test_distinct!(
distinct_number_default_criteria, distinct_number_default_criteria,
asc_desc_rank, asc_desc_rank,
vec![Words, Typo, Proximity, Attribute, Exactness] vec![Words, Typo, Proximity, Attribute, Exactness],
7
); );
test_distinct!(distinct_string_criterion_words, tag, vec![Words]); test_distinct!(distinct_string_criterion_words, tag, vec![Words], 3);
test_distinct!(distinct_number_criterion_words, asc_desc_rank, vec![Words]); test_distinct!(distinct_number_criterion_words, asc_desc_rank, vec![Words], 7);
test_distinct!(distinct_string_criterion_words_typo, tag, vec![Words, Typo]); test_distinct!(distinct_string_criterion_words_typo, tag, vec![Words, Typo], 3);
test_distinct!(distinct_number_criterion_words_typo, asc_desc_rank, vec![Words, Typo]); test_distinct!(distinct_number_criterion_words_typo, asc_desc_rank, vec![Words, Typo], 7);
test_distinct!(distinct_string_criterion_words_proximity, tag, vec![Words, Proximity]); test_distinct!(distinct_string_criterion_words_proximity, tag, vec![Words, Proximity], 3);
test_distinct!(distinct_number_criterion_words_proximity, asc_desc_rank, vec![Words, Proximity]); test_distinct!(distinct_number_criterion_words_proximity, asc_desc_rank, vec![Words, Proximity], 7);
test_distinct!(distinct_string_criterion_words_attribute, tag, vec![Words, Attribute]); test_distinct!(distinct_string_criterion_words_attribute, tag, vec![Words, Attribute], 3);
test_distinct!(distinct_number_criterion_words_attribute, asc_desc_rank, vec![Words, Attribute]); test_distinct!(distinct_number_criterion_words_attribute, asc_desc_rank, vec![Words, Attribute], 7);
test_distinct!(distinct_string_criterion_words_exactness, tag, vec![Words, Exactness]); test_distinct!(distinct_string_criterion_words_exactness, tag, vec![Words, Exactness], 3);
test_distinct!(distinct_number_criterion_words_exactness, asc_desc_rank, vec![Words, Exactness]); test_distinct!(distinct_number_criterion_words_exactness, asc_desc_rank, vec![Words, Exactness], 7);