mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-22 18:17:39 +08:00
fix: Improve the bucket sort algorithm
This commit is contained in:
parent
731ed11153
commit
2c3d71dd8f
@ -121,7 +121,7 @@ impl Index {
|
|||||||
let snapshot = self.database.snapshot();
|
let snapshot = self.database.snapshot();
|
||||||
|
|
||||||
let builder = QueryBuilder::new(snapshot)?;
|
let builder = QueryBuilder::new(snapshot)?;
|
||||||
let documents = builder.query(query, 0..20);
|
let documents = builder.query(query, 20);
|
||||||
|
|
||||||
Ok(documents)
|
Ok(documents)
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
pub mod criterion;
|
pub mod criterion;
|
||||||
mod ranked_stream;
|
mod query_builder;
|
||||||
mod distinct_map;
|
mod distinct_map;
|
||||||
|
|
||||||
use crate::{Match, DocumentId};
|
use crate::{Match, DocumentId};
|
||||||
|
|
||||||
pub use self::ranked_stream::{QueryBuilder, DistinctQueryBuilder};
|
pub use self::query_builder::{QueryBuilder, DistinctQueryBuilder};
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn match_query_index(a: &Match, b: &Match) -> bool {
|
fn match_query_index(a: &Match, b: &Match) -> bool {
|
||||||
|
@ -109,23 +109,28 @@ impl<T, C> QueryBuilder<T, C>
|
|||||||
where T: Deref<Target=DB>,
|
where T: Deref<Target=DB>,
|
||||||
C: Criterion,
|
C: Criterion,
|
||||||
{
|
{
|
||||||
pub fn query(&self, query: &str, range: Range<usize>) -> Vec<Document> {
|
pub fn query(&self, query: &str, limit: usize) -> Vec<Document> {
|
||||||
let mut documents = self.query_all(query);
|
let mut documents = self.query_all(query);
|
||||||
let mut groups = vec![documents.as_mut_slice()];
|
let mut groups = vec![documents.as_mut_slice()];
|
||||||
|
|
||||||
for criterion in &self.criteria {
|
'group: for criterion in &self.criteria {
|
||||||
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
let tmp_groups = mem::replace(&mut groups, Vec::new());
|
||||||
|
let mut computed = 0;
|
||||||
|
|
||||||
for group in tmp_groups {
|
for group in tmp_groups {
|
||||||
|
|
||||||
group.sort_unstable_by(|a, b| criterion.evaluate(a, b));
|
group.sort_unstable_by(|a, b| criterion.evaluate(a, b));
|
||||||
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b)) {
|
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b)) {
|
||||||
|
|
||||||
|
computed += group.len();
|
||||||
groups.push(group);
|
groups.push(group);
|
||||||
|
if computed >= limit { break 'group }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let range = clamp_range(range, 0..documents.len());
|
documents.truncate(limit);
|
||||||
documents[range].to_vec()
|
documents
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user