From 21d122a8703a17bccd69732e1675fc6d5aa4edad Mon Sep 17 00:00:00 2001 From: qdequele Date: Mon, 13 Jan 2020 19:34:49 +0100 Subject: [PATCH] rewrite indexed_pos -> field_id for hightligths --- meilisearch-core/src/bucket_sort.rs | 1 + meilisearch-core/src/lib.rs | 25 +++++++++++++++++++++++-- meilisearch-schema/src/schema.rs | 9 +++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/meilisearch-core/src/bucket_sort.rs b/meilisearch-core/src/bucket_sort.rs index 5489ff970..0fd4d0c58 100644 --- a/meilisearch-core/src/bucket_sort.rs +++ b/meilisearch-core/src/bucket_sort.rs @@ -330,6 +330,7 @@ where // once we classified the documents related to the current // automatons we save that as the next valid result let mut seen = BufferedDistinctMap::new(&mut distinct_map); + let schema = main_store.schema(reader)?.unwrap(); let mut documents = Vec::with_capacity(range.len()); for raw_document in raw_documents.into_iter().skip(distinct_raw_offset) { diff --git a/meilisearch-core/src/lib.rs b/meilisearch-core/src/lib.rs index ca4714ed8..2ed0ceeed 100644 --- a/meilisearch-core/src/lib.rs +++ b/meilisearch-core/src/lib.rs @@ -16,7 +16,6 @@ mod ranked_map; mod raw_document; mod reordered_attrs; mod update; -// mod fields_map; pub mod settings; pub mod criterion; pub mod raw_indexer; @@ -27,7 +26,6 @@ pub use self::database::{BoxUpdateFn, Database, MainT, UpdateT}; pub use self::error::{Error, MResult}; pub use self::number::{Number, ParseNumberError}; pub use self::ranked_map::RankedMap; -// pub use self::fields_map::FieldsMap; pub use self::raw_document::RawDocument; pub use self::store::Index; pub use self::update::{EnqueuedUpdateResult, ProcessedUpdateResult, UpdateStatus, UpdateType}; @@ -57,6 +55,7 @@ fn highlights_from_raw_document<'a, 'tag, 'txn>( queries_kinds: &HashMap, arena: &SmallArena<'tag, PostingsListView<'txn>>, searchable_attrs: Option<&ReorderedAttrs>, + schema: &Schema, ) -> Vec { let mut highlights = Vec::new(); @@ -83,6 +82,15 @@ fn highlights_from_raw_document<'a, 'tag, 'txn>( .and_then(|sa| sa.reverse(di.attribute)) .unwrap_or(di.attribute); + let attribute = match schema.indexed_pos_to_field_id(attribute) { + Some(field_id) => field_id.0, + None => { + error!("Cannot convert indexed_pos {} to field_id", attribute); + trace!("Schema is compronized; {:?}", schema); + continue + } + }; + let highlight = Highlight { attribute: attribute, char_index: di.char_index, @@ -113,6 +121,7 @@ impl Document { queries_kinds: &HashMap, arena: &SmallArena<'tag, PostingsListView<'txn>>, searchable_attrs: Option<&ReorderedAttrs>, + schema: &Schema, ) -> Document { let highlights = highlights_from_raw_document( @@ -120,6 +129,7 @@ impl Document { queries_kinds, arena, searchable_attrs, + schema, ); Document { id: raw_document.id, highlights } @@ -131,6 +141,7 @@ impl Document { queries_kinds: &HashMap, arena: &SmallArena<'tag, PostingsListView<'txn>>, searchable_attrs: Option<&ReorderedAttrs>, + schema: &Schema, ) -> Document { use crate::bucket_sort::SimpleMatch; @@ -140,6 +151,7 @@ impl Document { queries_kinds, arena, searchable_attrs, + schema, ); let mut matches = Vec::new(); @@ -148,6 +160,15 @@ impl Document { .and_then(|sa| sa.reverse(sm.attribute)) .unwrap_or(sm.attribute); + let attribute = match schema.indexed_pos_to_field_id(attribute) { + Some(field_id) => field_id.0, + None => { + error!("Cannot convert indexed_pos {} to field_id", attribute); + trace!("Schema is compronized; {:?}", schema); + continue + } + }; + matches.push(SimpleMatch { attribute, ..sm }); } matches.sort_unstable(); diff --git a/meilisearch-schema/src/schema.rs b/meilisearch-schema/src/schema.rs index b3b62fbea..8b7fd30e3 100644 --- a/meilisearch-schema/src/schema.rs +++ b/meilisearch-schema/src/schema.rs @@ -167,6 +167,15 @@ impl Schema { self.indexed_map.get(&id) } + pub fn indexed_pos_to_field_id>(&self, pos: I) -> Option { + let indexed_pos = pos.into().0 as usize; + if indexed_pos < self.indexed.len() { + Some(self.indexed[indexed_pos as usize]) + } else { + None + } + } + pub fn update_ranked>(&mut self, data: impl IntoIterator) -> SResult<()> { self.ranked = HashSet::new(); for name in data {