From a463ae821eea2af1aae46ed08792890bdb9d13f0 Mon Sep 17 00:00:00 2001 From: many Date: Thu, 25 Feb 2021 10:49:25 +0100 Subject: [PATCH] Add methods optional_words and authorize_typos on the query tree --- milli/src/search/query_tree.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/milli/src/search/query_tree.rs b/milli/src/search/query_tree.rs index 64babe053..87245ce20 100644 --- a/milli/src/search/query_tree.rs +++ b/milli/src/search/query_tree.rs @@ -157,6 +157,8 @@ trait Context { pub struct QueryTreeBuilder<'a> { rtxn: &'a heed::RoTxn<'a>, index: &'a Index, + optional_words: bool, + authorize_typos: bool, } impl<'a> Context for QueryTreeBuilder<'a> { @@ -177,7 +179,25 @@ impl<'a> QueryTreeBuilder<'a> { /// Create a `QueryTreeBuilder` from a heed ReadOnly transaction `rtxn` /// and an Index `index`. pub fn new(rtxn: &'a heed::RoTxn<'a>, index: &'a Index) -> Self { - Self { rtxn, index } + Self { rtxn, index, optional_words: true, authorize_typos: true } + } + + /// if `optional_words` is set to `false` the query tree will be + /// generated forcing all query words to be present in each matching documents + /// (the criterion `words` will be ignored). + /// default value if not called: `true` + pub fn optional_words(&mut self, optional_words: bool) -> &mut Self { + self.optional_words = optional_words; + self + } + + /// if `authorize_typos` is set to `false` the query tree will be generated + /// forcing all query words to match documents without any typo + /// (the criterion `typo` will be ignored). + /// default value if not called: `true` + pub fn authorize_typos(&mut self, authorize_typos: bool) -> &mut Self { + self.authorize_typos = authorize_typos; + self } /// Build the query tree: @@ -187,16 +207,10 @@ impl<'a> QueryTreeBuilder<'a> { /// - if `authorize_typos` is set to `false` the query tree will be generated /// forcing all query words to match documents without any typo /// (the criterion `typo` will be ignored) - pub fn build( - &self, - optional_words: bool, - authorize_typos: bool, - query: TokenStream, - ) -> anyhow::Result> - { + pub fn build(&self, query: TokenStream) -> anyhow::Result> { let primitive_query = create_primitive_query(query); if !primitive_query.is_empty() { - create_query_tree(self, optional_words, authorize_typos, primitive_query).map(Some) + create_query_tree(self, self.optional_words, self.authorize_typos, primitive_query).map(Some) } else { Ok(None) }