mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-26 14:54:27 +01:00
feat: Improve the readability of the ranking function
This commit is contained in:
parent
2e0193a39e
commit
0b02e31ce9
@ -78,12 +78,6 @@ impl Pool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn invert_sorts<F>(a: &Document, b: &Document, sorts: &[F]) -> bool
|
|
||||||
where F: Fn(&Document, &Document) -> Ordering,
|
|
||||||
{
|
|
||||||
sorts.iter().rev().all(|sort| sort(a, b) == Ordering::Equal)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoIterator for Pool {
|
impl IntoIterator for Pool {
|
||||||
type Item = Document;
|
type Item = Document;
|
||||||
type IntoIter = vec::IntoIter<Self::Item>;
|
type IntoIter = vec::IntoIter<Self::Item>;
|
||||||
@ -98,16 +92,23 @@ impl IntoIterator for Pool {
|
|||||||
exact,
|
exact,
|
||||||
];
|
];
|
||||||
|
|
||||||
for (i, sort) in sorts.iter().enumerate() {
|
{
|
||||||
|
let mut groups = vec![self.documents.as_mut_slice()];
|
||||||
|
|
||||||
|
for sort in sorts {
|
||||||
|
let mut temp = mem::replace(&mut groups, Vec::new());
|
||||||
let mut computed = 0;
|
let mut computed = 0;
|
||||||
for group in GroupByMut::new(&mut self.documents, |a, b| invert_sorts(a, b, &sorts[..i])) {
|
|
||||||
// TODO prefer using `sort_unstable_by_key` to allow reusing the key computation
|
for group in temp {
|
||||||
// `number of words` needs to be reversed, we can use the `cmp::Reverse` struct to do that
|
|
||||||
group.sort_unstable_by(sort);
|
group.sort_unstable_by(sort);
|
||||||
|
for group in GroupByMut::new(group, |a, b| sort(a, b) == Ordering::Equal) {
|
||||||
computed += group.len();
|
computed += group.len();
|
||||||
|
groups.push(group);
|
||||||
if computed >= self.limit { break }
|
if computed >= self.limit { break }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.documents.truncate(self.limit);
|
self.documents.truncate(self.limit);
|
||||||
self.documents.into_iter()
|
self.documents.into_iter()
|
||||||
|
Loading…
Reference in New Issue
Block a user