MeiliSearch/meilisearch-core/src/criterion/attribute.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

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};
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" }
fn prepare<'h, 'p, 'tag, 'txn, 'q, 'r>(
2019-12-11 17:02:10 +01:00
&self,
ctx: ContextMut<'h, 'p, 'tag, 'txn, 'q>,
documents: &mut [RawDocument<'r, 'tag>],
2019-12-13 11:14:12 +01:00
) -> MResult<()>
{
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
}
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)
}
}