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 group_by::GroupBy;
pub fn exact(lhs: &Document, rhs: &Document) -> Ordering {
let contains_exact = |matches: &[Match]| matches.iter().any(|m| m.is_exact);
let key = |matches: &[Match]| -> usize {
GroupBy::new(matches, match_query_index).map(contains_exact).filter(Clone::clone).count()
};
key(&lhs.matches).cmp(&key(&rhs.matches))
#[inline]
fn contains_exact(matches: &[Match]) -> bool {
matches.iter().any(|m| m.is_exact)
}
#[inline]
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 group_by::GroupByMut;
use self::sum_of_typos::sum_of_typos;
use self::number_of_words::number_of_words;
use self::words_proximity::words_proximity;
use self::sum_of_words_attribute::sum_of_words_attribute;
use self::sum_of_words_position::sum_of_words_position;
use self::exact::exact;
use self::{
sum_of_typos::sum_of_typos,
number_of_words::number_of_words,
words_proximity::words_proximity,
sum_of_words_attribute::sum_of_words_attribute,
sum_of_words_position::sum_of_words_position,
exact::exact,
};
#[inline]
fn match_query_index(a: &Match, b: &Match) -> bool {

View File

@ -3,10 +3,15 @@ use Match;
use rank::{match_query_index, Document};
use group_by::GroupBy;
pub fn number_of_words(lhs: &Document, rhs: &Document) -> Ordering {
let key = |matches: &[Match]| -> usize {
GroupBy::new(matches, match_query_index).count()
};
key(&lhs.matches).cmp(&key(&rhs.matches)).reverse()
#[inline]
fn number_of_query_words(matches: &[Match]) -> usize {
GroupBy::new(matches, match_query_index).count()
}
#[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 group_by::GroupBy;
pub fn sum_of_typos(lhs: &Document, rhs: &Document) -> Ordering {
let key = |matches: &[Match]| -> u8 {
GroupBy::new(matches, match_query_index).map(|m| m[0].distance).sum()
};
key(&lhs.matches).cmp(&key(&rhs.matches))
#[inline]
fn sum_matches_typos(matches: &[Match]) -> u8 {
// 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).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 group_by::GroupBy;
pub fn sum_of_words_attribute(lhs: &Document, rhs: &Document) -> Ordering {
let key = |matches: &[Match]| -> u8 {
GroupBy::new(matches, match_query_index).map(|m| m[0].attribute).sum()
};
key(&lhs.matches).cmp(&key(&rhs.matches))
#[inline]
fn sum_matches_attributes(matches: &[Match]) -> u8 {
// 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
}).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 group_by::GroupBy;
fn key(matches: &[Match]) -> u32 {
GroupBy::new(matches, match_query_index).map(|m| m[0].attribute_index).sum()
#[inline]
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 {
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)
}