feat: Clean-up ranking functions

This commit is contained in:
Kerollmops 2018-08-25 13:15:04 +02:00 committed by Clément Renault
parent 34e0211567
commit c32f014108
6 changed files with 77 additions and 34 deletions

View File

@ -3,11 +3,20 @@ use Match;
use rank::{match_query_index, Document}; use rank::{match_query_index, Document};
use group_by::GroupBy; use group_by::GroupBy;
pub fn exact(lhs: &Document, rhs: &Document) -> Ordering { #[inline]
let contains_exact = |matches: &[Match]| matches.iter().any(|m| m.is_exact); fn contains_exact(matches: &[Match]) -> bool {
let key = |matches: &[Match]| -> usize { matches.iter().any(|m| m.is_exact)
GroupBy::new(matches, match_query_index).map(contains_exact).filter(Clone::clone).count() }
};
#[inline]
key(&lhs.matches).cmp(&key(&rhs.matches)) fn number_exact_matches(matches: &[Match]) -> usize {
GroupBy::new(matches, match_query_index).map(contains_exact).count()
}
#[inline]
pub fn exact(lhs: &Document, rhs: &Document) -> Ordering {
let lhs = number_exact_matches(&lhs.matches);
let rhs = number_exact_matches(&rhs.matches);
lhs.cmp(&rhs)
} }

View File

@ -14,12 +14,14 @@ use metadata::{DocIndexes, OpWithStateBuilder, UnionWithState};
use {Match, DocumentId}; use {Match, DocumentId};
use group_by::GroupByMut; use group_by::GroupByMut;
use self::sum_of_typos::sum_of_typos; use self::{
use self::number_of_words::number_of_words; sum_of_typos::sum_of_typos,
use self::words_proximity::words_proximity; number_of_words::number_of_words,
use self::sum_of_words_attribute::sum_of_words_attribute; words_proximity::words_proximity,
use self::sum_of_words_position::sum_of_words_position; sum_of_words_attribute::sum_of_words_attribute,
use self::exact::exact; sum_of_words_position::sum_of_words_position,
exact::exact,
};
#[inline] #[inline]
fn match_query_index(a: &Match, b: &Match) -> bool { fn match_query_index(a: &Match, b: &Match) -> bool {

View File

@ -3,10 +3,15 @@ use Match;
use rank::{match_query_index, Document}; use rank::{match_query_index, Document};
use group_by::GroupBy; use group_by::GroupBy;
pub fn number_of_words(lhs: &Document, rhs: &Document) -> Ordering { #[inline]
let key = |matches: &[Match]| -> usize { fn number_of_query_words(matches: &[Match]) -> usize {
GroupBy::new(matches, match_query_index).count() GroupBy::new(matches, match_query_index).count()
}; }
key(&lhs.matches).cmp(&key(&rhs.matches)).reverse() #[inline]
pub fn number_of_words(lhs: &Document, rhs: &Document) -> Ordering {
let lhs = number_of_query_words(&lhs.matches);
let rhs = number_of_query_words(&rhs.matches);
lhs.cmp(&rhs).reverse()
} }

View File

@ -3,10 +3,19 @@ use Match;
use rank::{match_query_index, Document}; use rank::{match_query_index, Document};
use group_by::GroupBy; use group_by::GroupBy;
pub fn sum_of_typos(lhs: &Document, rhs: &Document) -> Ordering { #[inline]
let key = |matches: &[Match]| -> u8 { fn sum_matches_typos(matches: &[Match]) -> u8 {
GroupBy::new(matches, match_query_index).map(|m| m[0].distance).sum() // note that GroupBy will never return an empty group
}; // so we can do this assumption safely
GroupBy::new(matches, match_query_index).map(|group| unsafe {
key(&lhs.matches).cmp(&key(&rhs.matches)) group.get_unchecked(0).distance
}).sum()
}
#[inline]
pub fn sum_of_typos(lhs: &Document, rhs: &Document) -> Ordering {
let lhs = sum_matches_typos(&lhs.matches);
let rhs = sum_matches_typos(&rhs.matches);
lhs.cmp(&rhs)
} }

View File

@ -3,10 +3,19 @@ use Match;
use rank::{match_query_index, Document}; use rank::{match_query_index, Document};
use group_by::GroupBy; use group_by::GroupBy;
pub fn sum_of_words_attribute(lhs: &Document, rhs: &Document) -> Ordering { #[inline]
let key = |matches: &[Match]| -> u8 { fn sum_matches_attributes(matches: &[Match]) -> u8 {
GroupBy::new(matches, match_query_index).map(|m| m[0].attribute).sum() // note that GroupBy will never return an empty group
}; // so we can do this assumption safely
GroupBy::new(matches, match_query_index).map(|group| unsafe {
key(&lhs.matches).cmp(&key(&rhs.matches)) group.get_unchecked(0).attribute
}).sum()
}
#[inline]
pub fn sum_of_words_attribute(lhs: &Document, rhs: &Document) -> Ordering {
let lhs = sum_matches_attributes(&lhs.matches);
let rhs = sum_matches_attributes(&rhs.matches);
lhs.cmp(&rhs)
} }

View File

@ -3,10 +3,19 @@ use Match;
use rank::{match_query_index, Document}; use rank::{match_query_index, Document};
use group_by::GroupBy; use group_by::GroupBy;
fn key(matches: &[Match]) -> u32 { #[inline]
GroupBy::new(matches, match_query_index).map(|m| m[0].attribute_index).sum() fn sum_matches_attribute_index(matches: &[Match]) -> u32 {
// note that GroupBy will never return an empty group
// so we can do this assumption safely
GroupBy::new(matches, match_query_index).map(|group| unsafe {
group.get_unchecked(0).attribute_index
}).sum()
} }
#[inline]
pub fn sum_of_words_position(lhs: &Document, rhs: &Document) -> Ordering { pub fn sum_of_words_position(lhs: &Document, rhs: &Document) -> Ordering {
key(&lhs.matches).cmp(&key(&rhs.matches)) let lhs = sum_matches_attribute_index(&lhs.matches);
let rhs = sum_matches_attribute_index(&rhs.matches);
lhs.cmp(&rhs)
} }