mirror of
https://github.com/meilisearch/meilisearch.git
synced 2025-02-20 01:27:52 +08:00
feat: Introduce the CriteriaBuilder
This commit is contained in:
parent
5bebd4469c
commit
23cce69dc5
@ -7,7 +7,6 @@ mod exact;
|
|||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::vec;
|
|
||||||
|
|
||||||
use rocksdb::DB;
|
use rocksdb::DB;
|
||||||
|
|
||||||
@ -70,18 +69,55 @@ where D: Deref<Target=DB>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO there is too much Box here, can we use
|
pub struct CriteriaBuilder<D>
|
||||||
// static references or static closures
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(self) -> Vec<Box<dyn Criterion<D>>> {
|
||||||
|
self.inner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn default<D>() -> Vec<Box<dyn Criterion<D>>>
|
pub fn default<D>() -> Vec<Box<dyn Criterion<D>>>
|
||||||
where D: Deref<Target=DB>
|
where D: Deref<Target=DB>
|
||||||
{
|
{
|
||||||
vec![
|
CriteriaBuilder::with_capacity(7)
|
||||||
Box::new(SumOfTypos),
|
.add(SumOfTypos)
|
||||||
Box::new(NumberOfWords),
|
.add(NumberOfWords)
|
||||||
Box::new(WordsProximity),
|
.add(WordsProximity)
|
||||||
Box::new(SumOfWordsAttribute),
|
.add(SumOfWordsAttribute)
|
||||||
Box::new(SumOfWordsPosition),
|
.add(SumOfWordsPosition)
|
||||||
Box::new(Exact),
|
.add(Exact)
|
||||||
Box::new(DocumentId),
|
.add(DocumentId)
|
||||||
]
|
.build()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user