From 5f9a3546e0a504c2246274680e58358ed6e9a91d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Wed, 15 Jan 2020 15:14:24 +0100 Subject: [PATCH] Use an union instead of a sort for OR ops --- meilisearch-core/src/query_tree.rs | 32 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/meilisearch-core/src/query_tree.rs b/meilisearch-core/src/query_tree.rs index e6c778d71..6ac246963 100644 --- a/meilisearch-core/src/query_tree.rs +++ b/meilisearch-core/src/query_tree.rs @@ -332,25 +332,27 @@ pub fn traverse_query_tree<'o, 'txn>( println!("{:1$}OR", "", depth * 2); let before = Instant::now(); - let mut ids = Vec::new(); + let mut results = Vec::new(); for op in operations { - let docids = match cache.get(op) { - Some(docids) => docids, - None => { - let docids = match op { - Operation::And(ops) => execute_and(reader, ctx, cache, postings, depth + 1, &ops)?, - Operation::Or(ops) => execute_or(reader, ctx, cache, postings, depth + 1, &ops)?, - Operation::Query(query) => execute_query(reader, ctx, postings, depth + 1, &query)?, - }; - cache.entry(op).or_insert(docids) - } - }; - - ids.extend_from_slice(docids.as_ref()); + if cache.get(op).is_none() { + let docids = match op { + Operation::And(ops) => execute_and(reader, ctx, cache, postings, depth + 1, &ops)?, + Operation::Or(ops) => execute_or(reader, ctx, cache, postings, depth + 1, &ops)?, + Operation::Query(query) => execute_query(reader, ctx, postings, depth + 1, &query)?, + }; + cache.insert(op, docids); + } } - let docids = SetBuf::from_dirty(ids); + for op in operations { + if let Some(docids) = cache.get(op) { + results.push(docids.as_ref()); + } + } + + let op = sdset::multi::Union::new(results); + let docids = op.into_set_buf(); println!("{:3$}--- OR fetched {} documents in {:.02?}", "", docids.len(), before.elapsed(), depth * 2);