From 2ee210483f2ead933f6c12c4d7deeee2ce09af9f Mon Sep 17 00:00:00 2001 From: Tamo Date: Tue, 12 Apr 2022 15:22:47 +0200 Subject: [PATCH] fix(search): remove the back and forth between the IndexMap and the serde_json::Map This is ok because we're using the preserve_order feature in serde_json which is already internally using an IndexMap. --- .../src/routes/indexes/settings.rs | 8 +++---- meilisearch-lib/src/index/search.rs | 21 +++---------------- 2 files changed, 7 insertions(+), 22 deletions(-) diff --git a/meilisearch-http/src/routes/indexes/settings.rs b/meilisearch-http/src/routes/indexes/settings.rs index 601f0d833..ab7d42e6f 100644 --- a/meilisearch-http/src/routes/indexes/settings.rs +++ b/meilisearch-http/src/routes/indexes/settings.rs @@ -21,11 +21,11 @@ macro_rules! make_setting_route { use meilisearch_lib::milli::update::Setting; use meilisearch_lib::{index::Settings, index_controller::Update, MeiliSearch}; - use crate::analytics::Analytics; - use crate::extractors::authentication::{policies::*, GuardedData}; - use crate::extractors::sequential_extractor::SeqHandler; - use crate::task::SummarizedTaskView; use meilisearch_error::ResponseError; + use $crate::analytics::Analytics; + use $crate::extractors::authentication::{policies::*, GuardedData}; + use $crate::extractors::sequential_extractor::SeqHandler; + use $crate::task::SummarizedTaskView; pub async fn delete( meilisearch: GuardedData, MeiliSearch>, diff --git a/meilisearch-lib/src/index/search.rs b/meilisearch-lib/src/index/search.rs index c63be6aab..8d7b900f5 100644 --- a/meilisearch-lib/src/index/search.rs +++ b/meilisearch-lib/src/index/search.rs @@ -4,7 +4,6 @@ use std::str::FromStr; use std::time::Instant; use either::Either; -use indexmap::IndexMap; use milli::tokenizer::{Analyzer, AnalyzerConfig, Token}; use milli::{AscDesc, FieldId, FieldsIdsMap, Filter, MatchingWords, SortError}; use regex::Regex; @@ -16,7 +15,7 @@ use crate::index::error::FacetError; use super::error::{IndexError, Result}; use super::index::Index; -pub type Document = IndexMap; +pub type Document = serde_json::Map; type MatchesInfo = BTreeMap>; #[derive(Serialize, Debug, Clone, PartialEq)] @@ -499,10 +498,6 @@ fn make_document( .map(|&fid| field_ids_map.name(fid).expect("Missing field name")); let document = permissive_json_pointer::select_values(&document, attributes_to_retrieve); - - // then we need to convert the `serde_json::Map` into an `IndexMap`. - let document = document.into_iter().collect(); - Ok(document) } @@ -513,12 +508,6 @@ fn format_fields>( matching_words: &impl Matcher, formatted_options: &BTreeMap, ) -> Result { - // Convert the `IndexMap` into a `serde_json::Map`. - let document = document - .iter() - .map(|(k, v)| (k.clone(), v.clone())) - .collect(); - let selectors: Vec<_> = formatted_options .keys() // This unwrap must be safe since we got the ids from the fields_ids_map just @@ -526,7 +515,7 @@ fn format_fields>( .map(|&fid| field_ids_map.name(fid).unwrap()) .collect(); - let mut document = permissive_json_pointer::select_values(&document, selectors.iter().copied()); + let mut document = permissive_json_pointer::select_values(document, selectors.iter().copied()); permissive_json_pointer::map_leaf_values(&mut document, selectors, |key, value| { // To get the formatting option of each key we need to see all the rules that applies @@ -542,13 +531,9 @@ fn format_fields>( .fold(FormatOptions::default(), |acc, (_, option)| { acc.merge(*option) }); - // TODO: remove this useless clone - *value = formatter.format_value(value.clone(), matching_words, format); + *value = formatter.format_value(std::mem::take(value), matching_words, format); }); - // we need to convert back the `serde_json::Map` into an `IndexMap`. - let document = document.into_iter().collect(); - Ok(document) }