From 5b4e4bb8583625f409e62f6c194947970a6a8082 Mon Sep 17 00:00:00 2001 From: John Braun Date: Fri, 8 Oct 2021 15:07:15 +0200 Subject: [PATCH 1/3] Highlight numbers (int) as string in formatted JSON --- meilisearch-lib/src/index/search.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/meilisearch-lib/src/index/search.rs b/meilisearch-lib/src/index/search.rs index a0ea26127..25771a5d4 100644 --- a/meilisearch-lib/src/index/search.rs +++ b/meilisearch-lib/src/index/search.rs @@ -555,6 +555,10 @@ impl<'a, A: AsRef<[u8]>> Formatter<'a, A> { }) .collect(), ), + Value::Number(number) => { + let number_string_value = self.format_string(number.to_string(), matcher, format_options); + Value::String(number_string_value) + } value => value, } } From 3756f5a0ca055287609cbc4d982663943a19c3bb Mon Sep 17 00:00:00 2001 From: John Braun Date: Fri, 8 Oct 2021 15:07:45 +0200 Subject: [PATCH 2/3] Add test for highlighting numbers --- meilisearch-lib/src/index/search.rs | 89 +++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/meilisearch-lib/src/index/search.rs b/meilisearch-lib/src/index/search.rs index 25771a5d4..7655c2dfb 100644 --- a/meilisearch-lib/src/index/search.rs +++ b/meilisearch-lib/src/index/search.rs @@ -793,6 +793,95 @@ mod test { assert_eq!(value["author"], "J. R. R. Tolkien"); } + #[test] + fn formatted_with_highlight_in_number() { + let stop_words = fst::Set::default(); + let mut config = AnalyzerConfig::default(); + config.stop_words(&stop_words); + let analyzer = Analyzer::new(config); + let formatter = Formatter::new(&analyzer, (String::from(""), String::from(""))); + + let mut fields = FieldsIdsMap::new(); + let title = fields.insert("title").unwrap(); + let author = fields.insert("author").unwrap(); + let publication_year = fields.insert("publication_year").unwrap(); + + + let mut buf = Vec::new(); + let mut obkv = obkv::KvWriter::new(&mut buf); + + obkv.insert( + title, + Value::String("The Hobbit".into()).to_string().as_bytes(), + ) + .unwrap(); + + obkv.finish().unwrap(); + obkv = obkv::KvWriter::new(&mut buf); + + obkv.insert( + author, + Value::String("J. R. R. Tolkien".into()) + .to_string() + .as_bytes(), + ) + .unwrap(); + obkv.finish().unwrap(); + + obkv = obkv::KvWriter::new(&mut buf); + + obkv.insert( + publication_year, + Value::Number(1937.into()) + .to_string() + .as_bytes(), + ) + .unwrap(); + + obkv.finish().unwrap(); + + let obkv = obkv::KvReader::new(&buf); + + let mut formatted_options = BTreeMap::new(); + formatted_options.insert( + title, + FormatOptions { + highlight: false, + crop: None, + }, + ); + formatted_options.insert( + author, + FormatOptions { + highlight: false, + crop: None, + }, + ); + formatted_options.insert( + publication_year, + FormatOptions { + highlight: true, + crop: None, + }, + ); + + let mut matching_words = BTreeMap::new(); + matching_words.insert("1937", Some(4)); + + let value = format_fields( + &fields, + obkv, + &formatter, + &matching_words, + &formatted_options, + ) + .unwrap(); + + assert_eq!(value["title"], "The Hobbit"); + assert_eq!(value["author"], "J. R. R. Tolkien"); + assert_eq!(value["publication_year"], "1937"); + } + /// https://github.com/meilisearch/MeiliSearch/issues/1368 #[test] fn formatted_with_highlight_emoji() { From be75426e6402c16d96fc5ed40479ede761b34826 Mon Sep 17 00:00:00 2001 From: John Braun Date: Fri, 15 Oct 2021 21:32:29 +0200 Subject: [PATCH 3/3] Apply formatting according code style guidelines --- meilisearch-lib/src/index/search.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/meilisearch-lib/src/index/search.rs b/meilisearch-lib/src/index/search.rs index 7655c2dfb..8602adb0e 100644 --- a/meilisearch-lib/src/index/search.rs +++ b/meilisearch-lib/src/index/search.rs @@ -556,7 +556,8 @@ impl<'a, A: AsRef<[u8]>> Formatter<'a, A> { .collect(), ), Value::Number(number) => { - let number_string_value = self.format_string(number.to_string(), matcher, format_options); + let number_string_value = + self.format_string(number.to_string(), matcher, format_options); Value::String(number_string_value) } value => value, @@ -806,7 +807,6 @@ mod test { let author = fields.insert("author").unwrap(); let publication_year = fields.insert("publication_year").unwrap(); - let mut buf = Vec::new(); let mut obkv = obkv::KvWriter::new(&mut buf); @@ -832,9 +832,7 @@ mod test { obkv.insert( publication_year, - Value::Number(1937.into()) - .to_string() - .as_bytes(), + Value::Number(1937.into()).to_string().as_bytes(), ) .unwrap();