mirror of
				https://github.com/meilisearch/meilisearch.git
				synced 2025-11-04 01:46:28 +00:00 
			
		
		
		
	Revert "Revert "Sort at query time""
This commit is contained in:
		
				
					committed by
					
						
						Kerollmops
					
				
			
			
				
	
			
			
			
						parent
						
							879d5e8799
						
					
				
				
					commit
					89d0758713
				
			@@ -1,6 +1,6 @@
 | 
			
		||||
use big_s::S;
 | 
			
		||||
use milli::update::Settings;
 | 
			
		||||
use milli::{Criterion, Search, SearchResult};
 | 
			
		||||
use milli::{AscDesc, Criterion, Search, SearchResult};
 | 
			
		||||
use Criterion::*;
 | 
			
		||||
 | 
			
		||||
use crate::search::{self, EXTERNAL_DOCUMENTS_IDS};
 | 
			
		||||
@@ -11,7 +11,7 @@ const ALLOW_OPTIONAL_WORDS: bool = true;
 | 
			
		||||
const DISALLOW_OPTIONAL_WORDS: bool = false;
 | 
			
		||||
 | 
			
		||||
macro_rules! test_criterion {
 | 
			
		||||
    ($func:ident, $optional_word:ident, $authorize_typos:ident, $criteria:expr) => {
 | 
			
		||||
    ($func:ident, $optional_word:ident, $authorize_typos:ident, $criteria:expr, $sort_criteria:expr) => {
 | 
			
		||||
        #[test]
 | 
			
		||||
        fn $func() {
 | 
			
		||||
            let criteria = $criteria;
 | 
			
		||||
@@ -23,82 +23,168 @@ macro_rules! test_criterion {
 | 
			
		||||
            search.limit(EXTERNAL_DOCUMENTS_IDS.len());
 | 
			
		||||
            search.authorize_typos($authorize_typos);
 | 
			
		||||
            search.optional_words($optional_word);
 | 
			
		||||
            search.sort_criteria($sort_criteria);
 | 
			
		||||
 | 
			
		||||
            let SearchResult { documents_ids, .. } = search.execute().unwrap();
 | 
			
		||||
 | 
			
		||||
            let expected_external_ids: Vec<_> =
 | 
			
		||||
                search::expected_order(&criteria, $authorize_typos, $optional_word)
 | 
			
		||||
                    .into_iter()
 | 
			
		||||
                    .map(|d| d.id)
 | 
			
		||||
                    .collect();
 | 
			
		||||
            let expected_external_ids: Vec<_> = search::expected_order(
 | 
			
		||||
                &criteria,
 | 
			
		||||
                $authorize_typos,
 | 
			
		||||
                $optional_word,
 | 
			
		||||
                &$sort_criteria[..],
 | 
			
		||||
            )
 | 
			
		||||
            .into_iter()
 | 
			
		||||
            .map(|d| d.id)
 | 
			
		||||
            .collect();
 | 
			
		||||
            let documents_ids = search::internal_to_external_ids(&index, &documents_ids);
 | 
			
		||||
            assert_eq!(documents_ids, expected_external_ids);
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
test_criterion!(none_allow_typo, ALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![]);
 | 
			
		||||
test_criterion!(none_disallow_typo, DISALLOW_OPTIONAL_WORDS, DISALLOW_TYPOS, vec![]);
 | 
			
		||||
test_criterion!(words_allow_typo, ALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![Words]);
 | 
			
		||||
test_criterion!(attribute_allow_typo, DISALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![Attribute]);
 | 
			
		||||
test_criterion!(attribute_disallow_typo, DISALLOW_OPTIONAL_WORDS, DISALLOW_TYPOS, vec![Attribute]);
 | 
			
		||||
test_criterion!(exactness_allow_typo, DISALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![Exactness]);
 | 
			
		||||
test_criterion!(exactness_disallow_typo, DISALLOW_OPTIONAL_WORDS, DISALLOW_TYPOS, vec![Exactness]);
 | 
			
		||||
test_criterion!(proximity_allow_typo, DISALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![Proximity]);
 | 
			
		||||
test_criterion!(proximity_disallow_typo, DISALLOW_OPTIONAL_WORDS, DISALLOW_TYPOS, vec![Proximity]);
 | 
			
		||||
test_criterion!(none_allow_typo, ALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![], vec![]);
 | 
			
		||||
test_criterion!(none_disallow_typo, DISALLOW_OPTIONAL_WORDS, DISALLOW_TYPOS, vec![], vec![]);
 | 
			
		||||
test_criterion!(words_allow_typo, ALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![Words], vec![]);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    attribute_allow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Attribute],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    attribute_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Attribute],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    exactness_allow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Exactness],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    exactness_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Exactness],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    proximity_allow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Proximity],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    proximity_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Proximity],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    asc_allow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Asc(S("asc_desc_rank"))]
 | 
			
		||||
    vec![Asc(S("asc_desc_rank"))],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    asc_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Asc(S("asc_desc_rank"))]
 | 
			
		||||
    vec![Asc(S("asc_desc_rank"))],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    desc_allow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Desc(S("asc_desc_rank"))]
 | 
			
		||||
    vec![Desc(S("asc_desc_rank"))],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    desc_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Desc(S("asc_desc_rank"))]
 | 
			
		||||
    vec![Desc(S("asc_desc_rank"))],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    asc_unexisting_field_allow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Asc(S("unexisting_field"))]
 | 
			
		||||
    vec![Asc(S("unexisting_field"))],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    asc_unexisting_field_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Asc(S("unexisting_field"))]
 | 
			
		||||
    vec![Asc(S("unexisting_field"))],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    desc_unexisting_field_allow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Desc(S("unexisting_field"))]
 | 
			
		||||
    vec![Desc(S("unexisting_field"))],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    desc_unexisting_field_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Desc(S("unexisting_field"))]
 | 
			
		||||
    vec![Desc(S("unexisting_field"))],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(empty_sort_by_allow_typo, DISALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![Sort], vec![]);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    empty_sort_by_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Sort],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    sort_by_asc_allow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Sort],
 | 
			
		||||
    vec![AscDesc::Asc(S("tag"))]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    sort_by_asc_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Sort],
 | 
			
		||||
    vec![AscDesc::Asc(S("tag"))]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    sort_by_desc_allow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Sort],
 | 
			
		||||
    vec![AscDesc::Desc(S("tag"))]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    sort_by_desc_disallow_typo,
 | 
			
		||||
    DISALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    DISALLOW_TYPOS,
 | 
			
		||||
    vec![Sort],
 | 
			
		||||
    vec![AscDesc::Desc(S("tag"))]
 | 
			
		||||
);
 | 
			
		||||
test_criterion!(
 | 
			
		||||
    default_criteria_order,
 | 
			
		||||
    ALLOW_OPTIONAL_WORDS,
 | 
			
		||||
    ALLOW_TYPOS,
 | 
			
		||||
    vec![Words, Typo, Proximity, Attribute, Exactness]
 | 
			
		||||
    vec![Words, Typo, Proximity, Attribute, Exactness],
 | 
			
		||||
    vec![]
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
@@ -262,7 +348,7 @@ fn criteria_mixup() {
 | 
			
		||||
        let SearchResult { documents_ids, .. } = search.execute().unwrap();
 | 
			
		||||
 | 
			
		||||
        let expected_external_ids: Vec<_> =
 | 
			
		||||
            search::expected_order(&criteria, ALLOW_OPTIONAL_WORDS, ALLOW_TYPOS)
 | 
			
		||||
            search::expected_order(&criteria, ALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, &[])
 | 
			
		||||
                .into_iter()
 | 
			
		||||
                .map(|d| d.id)
 | 
			
		||||
                .collect();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user