From 04d34cb8aae2736d30f191d9569bf0b824a6cc73 Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Thu, 12 Dec 2019 16:36:42 +0100 Subject: [PATCH] Search; return formated section only if it's necessary --- meilisearch-http/src/helpers/meilisearch.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/meilisearch-http/src/helpers/meilisearch.rs b/meilisearch-http/src/helpers/meilisearch.rs index fc7958c49..6ce77ba91 100644 --- a/meilisearch-http/src/helpers/meilisearch.rs +++ b/meilisearch-http/src/helpers/meilisearch.rs @@ -243,7 +243,14 @@ impl<'a> SearchBuilder<'a> { .map_err(|e| Error::RetrieveDocument(doc.id.0, e.to_string()))? .ok_or(Error::DocumentNotFound(doc.id.0))?; - let mut formatted = document.clone(); + let has_attributes_to_highlight = self.attributes_to_highlight.is_some(); + let has_attributes_to_crop = self.attributes_to_crop.is_some(); + + let mut formatted = if has_attributes_to_highlight || has_attributes_to_crop { + document.clone() + } else { + IndexMap::new() + }; let mut matches = doc.highlights.clone(); // Crops fields if needed @@ -494,7 +501,7 @@ fn calculate_highlights( matches: &MatchesInfos, attributes_to_highlight: &HashSet, ) -> IndexMap { - let mut highlight_result = IndexMap::new(); + let mut highlight_result = document.clone(); for (attribute, matches) in matches.iter() { if attributes_to_highlight.contains(attribute) {