mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-23 05:14:27 +01:00
fix unaligned highlight
This commit is contained in:
parent
ce789682cc
commit
a94ee167fc
@ -352,10 +352,14 @@ fn aligned_crop(text: &str, match_index: usize, context: usize) -> (usize, usize
|
|||||||
return (match_index, 1 + text.chars().skip(match_index).take_while(is_word_component).count());
|
return (match_index, 1 + text.chars().skip(match_index).take_while(is_word_component).count());
|
||||||
}
|
}
|
||||||
let start = match match_index.saturating_sub(context) {
|
let start = match match_index.saturating_sub(context) {
|
||||||
n if n == 0 => n,
|
0 => 0,
|
||||||
n => word_end_index(n)
|
n => {
|
||||||
|
let word_end_index = word_end_index(n);
|
||||||
|
// skip whitespaces if any
|
||||||
|
word_end_index + text.chars().skip(word_end_index).take_while(char::is_ascii_whitespace).count()
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let end = word_end_index(start + 2 * context);
|
let end = word_end_index(match_index + context);
|
||||||
|
|
||||||
(start, end - start)
|
(start, end - start)
|
||||||
}
|
}
|
||||||
@ -370,15 +374,21 @@ fn crop_text(
|
|||||||
let char_index = matches.peek().map(|m| m.char_index as usize).unwrap_or(0);
|
let char_index = matches.peek().map(|m| m.char_index as usize).unwrap_or(0);
|
||||||
let (start, count) = aligned_crop(text, char_index, context);
|
let (start, count) = aligned_crop(text, char_index, context);
|
||||||
|
|
||||||
//TODO do something about the double allocation
|
// TODO do something about double allocation
|
||||||
let text = text.chars().skip(start).take(count).collect::<String>().trim().to_string();
|
let text = text
|
||||||
|
.chars()
|
||||||
|
.skip(start)
|
||||||
|
.take(count)
|
||||||
|
.collect::<String>()
|
||||||
|
.trim()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
// update matches index to match the new cropped text
|
// update matches index to match the new cropped text
|
||||||
let matches = matches
|
let matches = matches
|
||||||
.take_while(|m| (m.char_index as usize) + (m.char_length as usize) <= start + (context * 2))
|
.take_while(|m| (m.char_index as usize) + (m.char_length as usize) <= start + count)
|
||||||
.map(|match_| Highlight {
|
.map(|m| Highlight {
|
||||||
char_index: match_.char_index - start as u16,
|
char_index: m.char_index - start as u16,
|
||||||
..match_
|
..m
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -424,7 +434,7 @@ fn calculate_matches(
|
|||||||
let mut matches_result: HashMap<String, Vec<MatchPosition>> = HashMap::new();
|
let mut matches_result: HashMap<String, Vec<MatchPosition>> = HashMap::new();
|
||||||
for m in matches.iter() {
|
for m in matches.iter() {
|
||||||
if let Some(attribute) = schema.name(FieldId::new(m.attribute)) {
|
if let Some(attribute) = schema.name(FieldId::new(m.attribute)) {
|
||||||
if let Some(attributes_to_retrieve) = attributes_to_retrieve.clone() {
|
if let Some(ref attributes_to_retrieve) = attributes_to_retrieve {
|
||||||
if !attributes_to_retrieve.contains(attribute) {
|
if !attributes_to_retrieve.contains(attribute) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -470,10 +480,10 @@ fn calculate_highlights(
|
|||||||
|
|
||||||
let longest_matches = matches
|
let longest_matches = matches
|
||||||
.linear_group_by_key(|m| m.start)
|
.linear_group_by_key(|m| m.start)
|
||||||
.map(|group| group.last().unwrap());
|
.map(|group| group.last().unwrap())
|
||||||
|
.filter(move |m| m.start >= index);
|
||||||
|
|
||||||
for m in longest_matches {
|
for m in longest_matches {
|
||||||
if m.start >= index {
|
|
||||||
let before = value.get(index..m.start);
|
let before = value.get(index..m.start);
|
||||||
let highlighted = value.get(m.start..(m.start + m.length));
|
let highlighted = value.get(m.start..(m.start + m.length));
|
||||||
if let (Some(before), Some(highlighted)) = (before, highlighted) {
|
if let (Some(before), Some(highlighted)) = (before, highlighted) {
|
||||||
@ -486,13 +496,11 @@ fn calculate_highlights(
|
|||||||
error!("value: {:?}; index: {:?}, match: {:?}", value, index, m);
|
error!("value: {:?}; index: {:?}, match: {:?}", value, index, m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
highlighted_value.extend(value[index..].iter());
|
highlighted_value.extend(value[index..].iter());
|
||||||
highlight_result.insert(attribute.to_string(), Value::String(highlighted_value));
|
highlight_result.insert(attribute.to_string(), Value::String(highlighted_value));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
highlight_result
|
highlight_result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -524,13 +532,12 @@ mod tests {
|
|||||||
// mixed charset
|
// mixed charset
|
||||||
let (start, length) = aligned_crop(&text, 5, 3);
|
let (start, length) = aligned_crop(&text, 5, 3);
|
||||||
let cropped = text.chars().skip(start).take(length).collect::<String>().trim().to_string();
|
let cropped = text.chars().skip(start).take(length).collect::<String>().trim().to_string();
|
||||||
assert_eq!("isのス", cropped);
|
assert_eq!("isの", cropped);
|
||||||
|
|
||||||
// split regular word / CJK word, no space
|
// split regular word / CJK word, no space
|
||||||
let (start, length) = aligned_crop(&text, 7, 1);
|
let (start, length) = aligned_crop(&text, 7, 1);
|
||||||
let cropped = text.chars().skip(start).take(length).collect::<String>().trim().to_string();
|
let cropped = text.chars().skip(start).take(length).collect::<String>().trim().to_string();
|
||||||
assert_eq!("のス", cropped);
|
assert_eq!("の", cropped);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user