2019-12-11 18:37:26 +01:00
|
|
|
use std::cmp::Ordering;
|
2019-12-11 17:02:10 +01:00
|
|
|
use slice_group_by::GroupBy;
|
2019-12-13 11:14:12 +01:00
|
|
|
use crate::{RawDocument, MResult};
|
2019-12-12 11:33:39 +01:00
|
|
|
use crate::bucket_sort::SimpleMatch;
|
2019-12-13 12:38:54 +01:00
|
|
|
use super::{Criterion, Context, ContextMut, prepare_bare_matches};
|
2019-12-11 17:02:10 +01:00
|
|
|
|
|
|
|
pub struct Attribute;
|
|
|
|
|
|
|
|
impl Criterion for Attribute {
|
|
|
|
fn name(&self) -> &str { "attribute" }
|
|
|
|
|
2020-01-13 14:36:06 +01:00
|
|
|
fn prepare<'h, 'p, 'tag, 'txn, 'q, 'r>(
|
2019-12-11 17:02:10 +01:00
|
|
|
&self,
|
2020-01-13 14:36:06 +01:00
|
|
|
ctx: ContextMut<'h, 'p, 'tag, 'txn, 'q>,
|
2019-12-12 11:33:39 +01:00
|
|
|
documents: &mut [RawDocument<'r, 'tag>],
|
2019-12-13 11:14:12 +01:00
|
|
|
) -> MResult<()>
|
|
|
|
{
|
2020-01-13 14:36:06 +01:00
|
|
|
prepare_bare_matches(documents, ctx.postings_lists, ctx.query_mapping);
|
2019-12-13 11:14:12 +01:00
|
|
|
Ok(())
|
2019-12-11 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:33:39 +01:00
|
|
|
fn evaluate(&self, _ctx: &Context, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
2019-12-11 17:02:10 +01:00
|
|
|
#[inline]
|
2019-12-11 18:37:26 +01:00
|
|
|
fn sum_of_attribute(matches: &[SimpleMatch]) -> usize {
|
|
|
|
let mut sum_of_attribute = 0;
|
2019-12-11 17:02:10 +01:00
|
|
|
for group in matches.linear_group_by_key(|bm| bm.query_index) {
|
2019-12-11 18:37:26 +01:00
|
|
|
sum_of_attribute += group[0].attribute as usize;
|
2019-12-11 17:02:10 +01:00
|
|
|
}
|
2019-12-11 18:37:26 +01:00
|
|
|
sum_of_attribute
|
2019-12-11 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 18:37:26 +01:00
|
|
|
let lhs = sum_of_attribute(&lhs.processed_matches);
|
|
|
|
let rhs = sum_of_attribute(&rhs.processed_matches);
|
2019-12-11 17:02:10 +01:00
|
|
|
|
|
|
|
lhs.cmp(&rhs)
|
|
|
|
}
|
|
|
|
}
|