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 builder = QueryBuilder::new(snapshot)?;
|
||||
let documents = builder.query(query, 0..20);
|
||||
let documents = builder.query(query, 20);
|
||||
|
||||
Ok(documents)
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
pub mod criterion;
|
||||
mod ranked_stream;
|
||||
mod query_builder;
|
||||
mod distinct_map;
|
||||
|
||||
use crate::{Match, DocumentId};
|
||||
|
||||
pub use self::ranked_stream::{QueryBuilder, DistinctQueryBuilder};
|
||||
pub use self::query_builder::{QueryBuilder, DistinctQueryBuilder};
|
||||
|
||||
#[inline]
|
||||
fn match_query_index(a: &Match, b: &Match) -> bool {
|
||||
|
@ -109,23 +109,28 @@ impl<T, C> QueryBuilder<T, C>
|
||||
where T: Deref<Target=DB>,
|
||||
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 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 mut computed = 0;
|
||||
|
||||
for group in tmp_groups {
|
||||
|
||||
group.sort_unstable_by(|a, b| criterion.evaluate(a, b));
|
||||
for group in GroupByMut::new(group, |a, b| criterion.eq(a, b)) {
|
||||
|
||||
computed += group.len();
|
||||
groups.push(group);
|
||||
if computed >= limit { break 'group }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let range = clamp_range(range, 0..documents.len());
|
||||
documents[range].to_vec()
|
||||
documents.truncate(limit);
|
||||
documents
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user