2018-10-10 16:57:21 +02:00
|
|
|
mod sum_of_typos;
|
|
|
|
mod number_of_words;
|
|
|
|
mod words_proximity;
|
|
|
|
mod sum_of_words_attribute;
|
|
|
|
mod sum_of_words_position;
|
|
|
|
mod exact;
|
2018-12-10 20:14:16 +01:00
|
|
|
mod sort_by;
|
|
|
|
mod document_id;
|
2018-10-10 16:57:21 +02:00
|
|
|
|
|
|
|
use std::cmp::Ordering;
|
2018-12-07 17:59:03 +01:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
use rocksdb::DB;
|
2018-12-07 14:41:06 +01:00
|
|
|
|
|
|
|
use crate::database::DatabaseView;
|
2018-10-10 16:57:21 +02:00
|
|
|
use crate::rank::Document;
|
|
|
|
|
|
|
|
pub use self::{
|
2018-10-11 14:04:41 +02:00
|
|
|
sum_of_typos::SumOfTypos,
|
|
|
|
number_of_words::NumberOfWords,
|
|
|
|
words_proximity::WordsProximity,
|
|
|
|
sum_of_words_attribute::SumOfWordsAttribute,
|
|
|
|
sum_of_words_position::SumOfWordsPosition,
|
|
|
|
exact::Exact,
|
2018-12-10 20:14:16 +01:00
|
|
|
sort_by::SortBy,
|
|
|
|
document_id::DocumentId,
|
2018-10-10 16:57:21 +02:00
|
|
|
};
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
pub trait Criterion<D>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> Ordering;
|
2018-10-10 16:57:21 +02:00
|
|
|
|
2018-10-11 14:04:41 +02:00
|
|
|
#[inline]
|
2018-12-07 17:59:03 +01:00
|
|
|
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> bool {
|
2018-12-07 14:41:06 +01:00
|
|
|
self.evaluate(lhs, rhs, view) == Ordering::Equal
|
2018-10-11 14:04:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-10 16:57:21 +02:00
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
impl<'a, D, T: Criterion<D> + ?Sized> Criterion<D> for &'a T
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> Ordering {
|
2018-12-07 14:41:06 +01:00
|
|
|
(**self).evaluate(lhs, rhs, view)
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> bool {
|
2018-12-07 14:41:06 +01:00
|
|
|
(**self).eq(lhs, rhs, view)
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|
2018-10-11 14:04:41 +02:00
|
|
|
}
|
2018-10-10 16:57:21 +02:00
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
impl<D, T: Criterion<D> + ?Sized> Criterion<D> for Box<T>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
fn evaluate(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> Ordering {
|
2018-12-07 14:41:06 +01:00
|
|
|
(**self).evaluate(lhs, rhs, view)
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|
|
|
|
|
2018-12-07 17:59:03 +01:00
|
|
|
fn eq(&self, lhs: &Document, rhs: &Document, view: &DatabaseView<D>) -> bool {
|
2018-12-07 14:41:06 +01:00
|
|
|
(**self).eq(lhs, rhs, view)
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 23:20:18 +01:00
|
|
|
#[derive(Default)]
|
2018-12-10 11:36:09 +01:00
|
|
|
pub struct CriteriaBuilder<D>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
inner: Vec<Box<dyn Criterion<D>>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D> CriteriaBuilder<D>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
pub fn new() -> CriteriaBuilder<D> {
|
|
|
|
CriteriaBuilder { inner: Vec::new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_capacity(capacity: usize) -> CriteriaBuilder<D> {
|
|
|
|
CriteriaBuilder { inner: Vec::with_capacity(capacity) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reserve(&mut self, additional: usize) {
|
|
|
|
self.inner.reserve(additional)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add<C>(mut self, criterion: C) -> CriteriaBuilder<D>
|
|
|
|
where C: 'static + Criterion<D>,
|
|
|
|
{
|
|
|
|
self.push(criterion);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push<C>(&mut self, criterion: C)
|
|
|
|
where C: 'static + Criterion<D>,
|
|
|
|
{
|
|
|
|
self.inner.push(Box::new(criterion));
|
|
|
|
}
|
|
|
|
|
2018-12-10 20:14:16 +01:00
|
|
|
pub fn build(self) -> Criteria<D> {
|
|
|
|
Criteria { inner: self.inner }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Criteria<D>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
inner: Vec<Box<dyn Criterion<D>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D> Default for Criteria<D>
|
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
CriteriaBuilder::with_capacity(7)
|
|
|
|
.add(SumOfTypos)
|
|
|
|
.add(NumberOfWords)
|
|
|
|
.add(WordsProximity)
|
|
|
|
.add(SumOfWordsAttribute)
|
|
|
|
.add(SumOfWordsPosition)
|
|
|
|
.add(Exact)
|
|
|
|
.add(DocumentId)
|
|
|
|
.build()
|
2018-12-10 11:36:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 20:14:16 +01:00
|
|
|
impl<D> AsRef<[Box<dyn Criterion<D>>]> for Criteria<D>
|
2018-12-07 17:59:03 +01:00
|
|
|
where D: Deref<Target=DB>
|
|
|
|
{
|
2018-12-10 20:14:16 +01:00
|
|
|
fn as_ref(&self) -> &[Box<dyn Criterion<D>>] {
|
|
|
|
&self.inner
|
|
|
|
}
|
2018-10-10 16:57:21 +02:00
|
|
|
}
|