From 21c1473e0c49b3b79a0ebe142c48f177992e9776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Tue, 14 Jan 2020 11:38:04 +0100 Subject: [PATCH] Introduce the distance data --- meilisearch-core/src/bucket_sort.rs | 4 ++-- meilisearch-core/src/query_tree.rs | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/meilisearch-core/src/bucket_sort.rs b/meilisearch-core/src/bucket_sort.rs index 37eba6b57..935e81571 100644 --- a/meilisearch-core/src/bucket_sort.rs +++ b/meilisearch-core/src/bucket_sort.rs @@ -96,7 +96,7 @@ where let mut bare_matches = Vec::new(); mk_arena!(arena); - for ((query, input), matches) in queries { + for ((query, input, distance), matches) in queries { let postings_list_view = PostingsListView::original(Rc::from(input), Rc::new(matches)); // TODO optimize the filter by skipping docids that have already been seen @@ -109,7 +109,7 @@ where let bare_match = BareMatch { document_id, query_index: u16::try_from(query.id).unwrap(), - distance: 0, + distance: distance, is_exact: true, // TODO where can I find this info? postings_list: posting_list_index, }; diff --git a/meilisearch-core/src/query_tree.rs b/meilisearch-core/src/query_tree.rs index 5eae8c3bd..505d2613f 100644 --- a/meilisearch-core/src/query_tree.rs +++ b/meilisearch-core/src/query_tree.rs @@ -266,7 +266,8 @@ pub fn create_query_tree( Ok((operation, mapping)) } -pub type Postings<'o, 'txn> = HashMap<(&'o Query, Vec), Cow<'txn, Set>>; +pub type Distance = u8; +pub type Postings<'o, 'txn> = HashMap<(&'o Query, Vec, Distance), Cow<'txn, Set>>; pub type Cache<'o, 'txn> = HashMap<&'o Operation, Cow<'txn, Set>>; pub struct QueryResult<'o, 'txn> { @@ -372,7 +373,8 @@ pub fn traverse_query_tree<'o, 'txn>( if *prefix && word.len() == 1 { let prefix = [word.as_bytes()[0], 0, 0, 0]; let result = ctx.prefix_postings_lists.prefix_postings_list(reader, prefix)?.unwrap_or_default(); - postings.insert((query, word.clone().into_bytes()), result.matches); + let distance = 0; + postings.insert((query, word.clone().into_bytes(), distance), result.matches); result.docids } else { let dfa = if *prefix { build_prefix_dfa(word) } else { build_dfa(word) }; @@ -387,9 +389,10 @@ pub fn traverse_query_tree<'o, 'txn>( let before = Instant::now(); let mut docids = Vec::new(); while let Some(input) = stream.next() { + let distance = dfa.eval(input).to_u8(); if let Some(result) = ctx.postings_lists.postings_list(reader, input)? { docids.extend_from_slice(&result.docids); - postings.insert((query, input.to_owned()), result.matches); + postings.insert((query, input.to_owned(), distance), result.matches); } } println!("{:3$}docids extend ({:?}) took {:.02?}", "", docids.len(), before.elapsed(), depth * 2); @@ -414,9 +417,10 @@ pub fn traverse_query_tree<'o, 'txn>( let mut docids = Vec::new(); while let Some(input) = stream.next() { + let distance = dfa.eval(input).to_u8(); if let Some(result) = ctx.postings_lists.postings_list(reader, input)? { docids.extend_from_slice(&result.docids); - postings.insert((query, input.to_owned()), result.matches); + postings.insert((query, input.to_owned(), distance), result.matches); } } @@ -446,7 +450,8 @@ pub fn traverse_query_tree<'o, 'txn>( println!("{:2$}docids construction took {:.02?}", "", before.elapsed(), depth * 2); let matches = Cow::Owned(SetBuf::new(matches).unwrap()); - postings.insert((query, vec![]), matches); + let distance = 0; + postings.insert((query, vec![], distance), matches); Cow::Owned(docids) } else {