From 79046378937dbfb59d1c5280e6b8077aa0fe98ff Mon Sep 17 00:00:00 2001 From: Marin Postma Date: Wed, 5 May 2021 17:31:40 +0200 Subject: [PATCH] crop skeleton --- meilisearch-http/src/index/search.rs | 62 ++++++++++++++++------------ 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/meilisearch-http/src/index/search.rs b/meilisearch-http/src/index/search.rs index 50d163898..21a587a37 100644 --- a/meilisearch-http/src/index/search.rs +++ b/meilisearch-http/src/index/search.rs @@ -232,7 +232,8 @@ fn compute_formatted>( highlighter: &Highlighter, matching_words: &impl Matcher, all_formatted: &[FieldId], - to_highlight_ids: &HashSet, + to_highlight_fields: &HashSet, + to_crop_fields: &HashSet, ) -> anyhow::Result { let mut document = Document::new(); @@ -240,8 +241,8 @@ fn compute_formatted>( if let Some(value) = obkv.get(*field) { let mut value: Value = serde_json::from_slice(value)?; - if to_highlight_ids.contains(field) { - value = highlighter.highlight_value(value, matching_words); + if to_highlight_fields.contains(field) { + value = highlighter.format_value(value, matching_words, to_highlight_fields.contains(field)); } // This unwrap must be safe since we got the ids from the fields_ids_map just @@ -291,46 +292,55 @@ impl<'a, A: AsRef<[u8]>> Highlighter<'a, A> { Self { analyzer, marks } } - fn highlight_value(&self, value: Value, words_to_highlight: &impl Matcher) -> Value { + fn format_value( + &self, + value: Value, + matcher: &impl Matcher, + need_to_crop: Option, + need_to_highlight: bool, + ) -> Value { match value { - Value::Null => Value::Null, - Value::Bool(boolean) => Value::Bool(boolean), - Value::Number(number) => Value::Number(number), Value::String(old_string) => { - let mut string = String::new(); - let analyzed = self.analyzer.analyze(&old_string); - for (word, token) in analyzed.reconstruct() { - if token.is_word() { - let to_highlight = words_to_highlight.matches(token.text()); - if to_highlight { - string.push_str(&self.marks.0) - } - string.push_str(word); - if to_highlight { - string.push_str(&self.marks.1) - } - } else { - string.push_str(word); - } - } - Value::String(string) + let value = self.format_string(old_string, need_to_crop, need_to_highlight); + Value::String(value) } Value::Array(values) => Value::Array( values .into_iter() - .map(|v| self.highlight_value(v, words_to_highlight)) + .map(|v| self.format_value(v, matcher, None, need_to_highlight)) .collect(), ), Value::Object(object) => Value::Object( object .into_iter() - .map(|(k, v)| (k, self.highlight_value(v, words_to_highlight))) + .map(|(k, v)| (k, self.format_value(value, matcher, None, need_to_highlight))) .collect(), ), + value => value, } } + fn format_string(&self, s: String, need_to_crop: Option, need_to_highlight: bool) -> String { + let word_iter: Box> = if let Some(_crop_len) = need_to_crop { + // cropping iterator + todo!() + } else { + // normal Iterator + todo!() + }; + + word_iter.map(|(word, is_match)| { + if need_to_highlight && is_match { + // highlight word + todo!() + } else { + word + } + }) + .collect::() + } } + fn parse_facets( facets: &Value, index: &Index,