2019-12-12 00:02:10 +08:00
|
|
|
use std::cmp::{Ordering, Reverse};
|
2019-12-13 18:33:22 +08:00
|
|
|
use std::collections::hash_map::{HashMap, Entry};
|
|
|
|
use meilisearch_schema::SchemaAttr;
|
2019-10-02 23:34:32 +08:00
|
|
|
use slice_group_by::GroupBy;
|
2019-12-13 18:14:12 +08:00
|
|
|
use crate::{RawDocument, MResult};
|
2019-12-12 18:33:39 +08:00
|
|
|
use crate::bucket_sort::BareMatch;
|
|
|
|
use super::{Criterion, Context, ContextMut};
|
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" }
|
|
|
|
|
2019-12-13 18:14:12 +08:00
|
|
|
fn prepare<'h, 'p, 'tag, 'txn, 'q, 'a, 'r>(
|
|
|
|
&self,
|
2019-12-13 18:33:22 +08:00
|
|
|
ctx: ContextMut<'h, 'p, 'tag, 'txn, 'q, 'a>,
|
2019-12-13 18:14:12 +08:00
|
|
|
documents: &mut [RawDocument<'r, 'tag>],
|
|
|
|
) -> MResult<()>
|
|
|
|
{
|
2019-12-13 18:33:22 +08:00
|
|
|
let store = ctx.documents_fields_counts_store;
|
|
|
|
let reader = ctx.reader;
|
|
|
|
|
|
|
|
'documents: for doc in documents {
|
|
|
|
doc.raw_matches.sort_unstable_by_key(|bm| (bm.query_index, Reverse(bm.is_exact)));
|
|
|
|
|
|
|
|
// mark the document if we find a "one word field" that matches
|
|
|
|
let mut fields_counts = HashMap::new();
|
|
|
|
for group in doc.raw_matches.linear_group_by_key(|bm| bm.query_index) {
|
|
|
|
for group in group.linear_group_by_key(|bm| bm.is_exact) {
|
|
|
|
if !group[0].is_exact { break }
|
|
|
|
|
|
|
|
for bm in group {
|
|
|
|
for di in ctx.postings_lists[bm.postings_list].as_ref() {
|
|
|
|
|
|
|
|
let attr = SchemaAttr(di.attribute);
|
|
|
|
let count = match fields_counts.entry(attr) {
|
|
|
|
Entry::Occupied(entry) => *entry.get(),
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
let count = store.document_field_count(reader, doc.id, attr)?;
|
|
|
|
*entry.insert(count)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if count == Some(1) {
|
|
|
|
doc.contains_one_word_field = true;
|
|
|
|
continue 'documents
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 00:02:10 +08:00
|
|
|
}
|
2019-12-13 18:33:22 +08:00
|
|
|
|
2019-12-13 18:14:12 +08:00
|
|
|
Ok(())
|
2019-10-15 00:48:54 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:33:39 +08:00
|
|
|
fn evaluate(&self, _ctx: &Context, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
2019-12-12 00:02:10 +08:00
|
|
|
#[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-13 18:33:22 +08:00
|
|
|
// does it contains a "one word field"
|
|
|
|
lhs.contains_one_word_field.cmp(&rhs.contains_one_word_field).reverse()
|
|
|
|
// if not, with document contains the more exact words
|
|
|
|
.then_with(|| {
|
|
|
|
let lhs = sum_exact_query_words(&lhs.raw_matches);
|
|
|
|
let rhs = sum_exact_query_words(&rhs.raw_matches);
|
|
|
|
lhs.cmp(&rhs).reverse()
|
|
|
|
})
|
2019-10-02 23:34:32 +08:00
|
|
|
}
|
|
|
|
}
|