mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-23 10:37:41 +08:00
fix: Make the examples compile with the new Highlight type
This commit is contained in:
parent
6b6db2f8e6
commit
4f4b630ae9
@ -11,7 +11,7 @@ use std::error::Error;
|
|||||||
|
|
||||||
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
|
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use meilidb_core::Match;
|
use meilidb_core::Highlight;
|
||||||
|
|
||||||
use meilidb_data::Database;
|
use meilidb_data::Database;
|
||||||
use meilidb_schema::SchemaAttr;
|
use meilidb_schema::SchemaAttr;
|
||||||
@ -71,12 +71,12 @@ fn char_to_byte_range(index: usize, length: usize, text: &str) -> (usize, usize)
|
|||||||
(byte_index, byte_length)
|
(byte_index, byte_length)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_highlight_areas(text: &str, matches: &[Match]) -> Vec<usize> {
|
fn create_highlight_areas(text: &str, highlights: &[Highlight]) -> Vec<usize> {
|
||||||
let mut byte_indexes = BTreeMap::new();
|
let mut byte_indexes = BTreeMap::new();
|
||||||
|
|
||||||
for match_ in matches {
|
for highlight in highlights {
|
||||||
let char_index = match_.char_index as usize;
|
let char_index = highlight.char_index as usize;
|
||||||
let char_length = match_.char_length as usize;
|
let char_length = highlight.char_length as usize;
|
||||||
let (byte_index, byte_length) = char_to_byte_range(char_index, char_length, text);
|
let (byte_index, byte_length) = char_to_byte_range(char_index, char_length, text);
|
||||||
|
|
||||||
match byte_indexes.entry(byte_index) {
|
match byte_indexes.entry(byte_index) {
|
||||||
@ -111,26 +111,26 @@ fn create_highlight_areas(text: &str, matches: &[Match]) -> Vec<usize> {
|
|||||||
/// ```
|
/// ```
|
||||||
fn crop_text(
|
fn crop_text(
|
||||||
text: &str,
|
text: &str,
|
||||||
matches: impl IntoIterator<Item=Match>,
|
highlights: impl IntoIterator<Item=Highlight>,
|
||||||
context: usize,
|
context: usize,
|
||||||
) -> (String, Vec<Match>)
|
) -> (String, Vec<Highlight>)
|
||||||
{
|
{
|
||||||
let mut matches = matches.into_iter().peekable();
|
let mut highlights = highlights.into_iter().peekable();
|
||||||
|
|
||||||
let char_index = matches.peek().map(|m| m.char_index as usize).unwrap_or(0);
|
let char_index = highlights.peek().map(|m| m.char_index as usize).unwrap_or(0);
|
||||||
let start = char_index.saturating_sub(context);
|
let start = char_index.saturating_sub(context);
|
||||||
let text = text.chars().skip(start).take(context * 2).collect();
|
let text = text.chars().skip(start).take(context * 2).collect();
|
||||||
|
|
||||||
let matches = matches
|
let highlights = highlights
|
||||||
.take_while(|m| {
|
.take_while(|m| {
|
||||||
(m.char_index as usize) + (m.char_length as usize) <= start + (context * 2)
|
(m.char_index as usize) + (m.char_length as usize) <= start + (context * 2)
|
||||||
})
|
})
|
||||||
.map(|match_| {
|
.map(|highlight| {
|
||||||
Match { char_index: match_.char_index - start as u16, ..match_ }
|
Highlight { char_index: highlight.char_index - start as u16, ..highlight }
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
(text, matches)
|
(text, highlights)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@ -168,7 +168,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let number_of_documents = documents.len();
|
let number_of_documents = documents.len();
|
||||||
for mut doc in documents {
|
for mut doc in documents {
|
||||||
|
|
||||||
doc.matches.sort_unstable_by_key(|m| (m.char_index, m.char_index));
|
doc.highlights.sort_unstable_by_key(|m| (m.char_index, m.char_length));
|
||||||
|
|
||||||
let start_retrieve = Instant::now();
|
let start_retrieve = Instant::now();
|
||||||
let result = index.document::<Document>(Some(&fields), doc.id);
|
let result = index.document::<Document>(Some(&fields), doc.id);
|
||||||
@ -180,11 +180,11 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
print!("{}: ", name);
|
print!("{}: ", name);
|
||||||
|
|
||||||
let attr = schema.attribute(&name).unwrap();
|
let attr = schema.attribute(&name).unwrap();
|
||||||
let matches = doc.matches.iter()
|
let highlights = doc.highlights.iter()
|
||||||
.filter(|m| SchemaAttr::new(m.attribute) == attr)
|
.filter(|m| SchemaAttr::new(m.attribute) == attr)
|
||||||
.cloned();
|
.cloned();
|
||||||
let (text, matches) = crop_text(&text, matches, opt.char_context);
|
let (text, highlights) = crop_text(&text, highlights, opt.char_context);
|
||||||
let areas = create_highlight_areas(&text, &matches);
|
let areas = create_highlight_areas(&text, &highlights);
|
||||||
display_highlights(&text, &areas)?;
|
display_highlights(&text, &areas)?;
|
||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
@ -194,8 +194,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut matching_attributes = HashSet::new();
|
let mut matching_attributes = HashSet::new();
|
||||||
for _match in doc.matches {
|
for highlight in doc.highlights {
|
||||||
let attr = SchemaAttr::new(_match.attribute);
|
let attr = SchemaAttr::new(highlight.attribute);
|
||||||
let name = schema.attribute_name(attr);
|
let name = schema.attribute_name(attr);
|
||||||
matching_attributes.insert(name);
|
matching_attributes.insert(name);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user