2019-12-12 00:02:10 +08:00
|
|
|
use std::cmp::{Ordering, Reverse};
|
2019-10-15 00:48:54 +08:00
|
|
|
|
2019-12-12 00:02:10 +08:00
|
|
|
use compact_arena::SmallArena;
|
2019-10-02 23:34:32 +08:00
|
|
|
use slice_group_by::GroupBy;
|
2019-10-15 00:48:54 +08:00
|
|
|
|
2019-12-12 00:02:10 +08:00
|
|
|
use crate::automaton::QueryEnhancer;
|
|
|
|
use crate::bucket_sort::{PostingsListView, BareMatch, QueryWordAutomaton};
|
|
|
|
use crate::RawDocument;
|
|
|
|
use super::Criterion;
|
2019-10-02 23:34:32 +08:00
|
|
|
|
|
|
|
pub struct Exact;
|
|
|
|
|
|
|
|
impl Criterion for Exact {
|
2019-12-12 00:02:10 +08:00
|
|
|
fn name(&self) -> &str { "exact" }
|
|
|
|
|
|
|
|
fn prepare(
|
|
|
|
&self,
|
|
|
|
documents: &mut [RawDocument],
|
|
|
|
postings_lists: &mut SmallArena<PostingsListView>,
|
|
|
|
query_enhancer: &QueryEnhancer,
|
|
|
|
automatons: &[QueryWordAutomaton],
|
|
|
|
) {
|
|
|
|
for document in documents {
|
|
|
|
document.raw_matches.sort_unstable_by_key(|bm| (bm.query_index, Reverse(bm.is_exact)));
|
|
|
|
}
|
2019-10-15 00:48:54 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 00:02:10 +08:00
|
|
|
fn evaluate(
|
|
|
|
&self,
|
|
|
|
lhs: &RawDocument,
|
|
|
|
rhs: &RawDocument,
|
|
|
|
postings_lists: &SmallArena<PostingsListView>,
|
|
|
|
) -> Ordering
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn sum_exact_query_words(matches: &[BareMatch]) -> usize {
|
|
|
|
let mut sum_exact_query_words = 0;
|
|
|
|
|
|
|
|
for group in matches.linear_group_by_key(|bm| bm.query_index) {
|
|
|
|
sum_exact_query_words += group[0].is_exact as usize;
|
|
|
|
}
|
2019-10-15 00:48:54 +08:00
|
|
|
|
2019-12-12 00:02:10 +08:00
|
|
|
sum_exact_query_words
|
|
|
|
}
|
2019-10-15 00:48:54 +08:00
|
|
|
|
2019-12-12 00:02:10 +08:00
|
|
|
let lhs = sum_exact_query_words(&lhs.raw_matches);
|
|
|
|
let rhs = sum_exact_query_words(&rhs.raw_matches);
|
2019-10-02 23:34:32 +08:00
|
|
|
|
2019-12-12 00:02:10 +08:00
|
|
|
lhs.cmp(&rhs).reverse()
|
2019-10-02 23:34:32 +08:00
|
|
|
}
|
|
|
|
}
|