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.
This commit is contained in:
Tamo 2022-04-12 15:22:47 +02:00
parent f67167fa9f
commit 2ee210483f
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69
2 changed files with 7 additions and 22 deletions

View File

@ -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<ActionPolicy<{ actions::SETTINGS_UPDATE }>, MeiliSearch>,

View File

@ -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<String, Value>;
pub type Document = serde_json::Map<String, Value>;
type MatchesInfo = BTreeMap<String, Vec<MatchInfo>>;
#[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<A: AsRef<[u8]>>(
matching_words: &impl Matcher,
formatted_options: &BTreeMap<FieldId, FormatOptions>,
) -> Result<Document> {
// 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<A: AsRef<[u8]>>(
.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<A: AsRef<[u8]>>(
.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)
}