mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-30 08:44:27 +01:00
Improve the QueryEnhancer by doing a single lookup
This commit is contained in:
parent
902625601a
commit
f87c67fcad
@ -147,35 +147,37 @@ impl<S: AsRef<str>> QueryEnhancerBuilder<'_, S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(self) -> QueryEnhancer {
|
pub fn build(self) -> QueryEnhancer {
|
||||||
QueryEnhancer {
|
let interval_tree = FakeIntervalTree::new(self.real_to_origin);
|
||||||
origins: self.origins,
|
let mut table = Vec::new();
|
||||||
real_to_origin: FakeIntervalTree::new(self.real_to_origin),
|
|
||||||
|
for real in 0.. {
|
||||||
|
match replacement(&self.origins, &interval_tree, real) {
|
||||||
|
Some(range) => table.push(range),
|
||||||
|
None => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QueryEnhancer { table }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
/// Returns the query indices that represent this real query index.
|
||||||
pub struct QueryEnhancer {
|
fn replacement(
|
||||||
origins: Vec<usize>,
|
origins: &[usize],
|
||||||
real_to_origin: FakeIntervalTree,
|
real_to_origin: &FakeIntervalTree,
|
||||||
}
|
real: u32,
|
||||||
|
) -> Option<Range<u32>>
|
||||||
impl QueryEnhancer {
|
{
|
||||||
/// Returns the query indices that represent this real query index.
|
|
||||||
pub fn replacement(&self, real: u32) -> Range<u32> {
|
|
||||||
let real = real as usize;
|
let real = real as usize;
|
||||||
|
|
||||||
// query the fake interval tree with the real query index
|
// query the fake interval tree with the real query index
|
||||||
let (range, (origin, real_length)) = self
|
let (range, (origin, real_length)) = real_to_origin.query(real)?;
|
||||||
.real_to_origin
|
|
||||||
.query(real)
|
|
||||||
.expect("real has never been declared");
|
|
||||||
|
|
||||||
// if `real` is the end bound of the range
|
// if `real` is the end bound of the range
|
||||||
if (range.start + real_length - 1) == real {
|
if (range.start + real_length - 1) == real {
|
||||||
let mut count = range.len();
|
let mut count = range.len();
|
||||||
let mut new_origin = origin;
|
let mut new_origin = origin;
|
||||||
for (i, slice) in self.origins[new_origin..].windows(2).enumerate() {
|
for (i, slice) in origins[new_origin..].windows(2).enumerate() {
|
||||||
let len = slice[1] - slice[0];
|
let len = slice[1] - slice[0];
|
||||||
count = count.saturating_sub(len);
|
count = count.saturating_sub(len);
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
@ -185,25 +187,36 @@ impl QueryEnhancer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let n = real - range.start;
|
let n = real - range.start;
|
||||||
let start = self.origins[origin];
|
let start = origins[origin];
|
||||||
let end = self.origins[new_origin + 1];
|
let end = origins[new_origin + 1];
|
||||||
let remaining = (end - start) - n;
|
let remaining = (end - start) - n;
|
||||||
|
|
||||||
Range {
|
Some(Range {
|
||||||
start: (start + n) as u32,
|
start: (start + n) as u32,
|
||||||
end: (start + n + remaining) as u32,
|
end: (start + n + remaining) as u32,
|
||||||
}
|
})
|
||||||
} else {
|
} else {
|
||||||
// just return the origin along with
|
// just return the origin along with
|
||||||
// the real position of the word
|
// the real position of the word
|
||||||
let n = real as usize - range.start;
|
let n = real as usize - range.start;
|
||||||
let origin = self.origins[origin];
|
let origin = origins[origin];
|
||||||
|
|
||||||
Range {
|
Some(Range {
|
||||||
start: (origin + n) as u32,
|
start: (origin + n) as u32,
|
||||||
end: (origin + n + 1) as u32,
|
end: (origin + n + 1) as u32,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct QueryEnhancer {
|
||||||
|
table: Vec<Range<u32>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl QueryEnhancer {
|
||||||
|
/// Returns the query indices that represent this real query index.
|
||||||
|
pub fn replacement(&self, real: u32) -> Range<u32> {
|
||||||
|
self.table[real as usize].clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user