From 9f320590d3bee1248494001330b99cb2a39095d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Wed, 26 Jun 2019 16:28:51 +0200 Subject: [PATCH] feat: Support query words concatenation --- meilidb-core/src/query_builder.rs | 71 ++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/meilidb-core/src/query_builder.rs b/meilidb-core/src/query_builder.rs index 147908906..b48d4b696 100644 --- a/meilidb-core/src/query_builder.rs +++ b/meilidb-core/src/query_builder.rs @@ -22,18 +22,17 @@ const NGRAMS: usize = 3; struct Automaton { index: usize, - is_synonym: bool, - number_words: usize, + is_exact: bool, dfa: DfaExt, } impl Automaton { - fn synonym(index: usize, number_words: usize, dfa: DfaExt) -> Automaton { - Automaton { index, is_synonym: true, number_words, dfa } + fn exact(index: usize, dfa: DfaExt) -> Automaton { + Automaton { index, is_exact: true, dfa } } - fn original(index: usize, number_words: usize, dfa: DfaExt) -> Automaton { - Automaton { index, is_synonym: false, number_words, dfa } + fn non_exact(index: usize, dfa: DfaExt) -> Automaton { + Automaton { index, is_exact: false, dfa } } } @@ -58,17 +57,24 @@ fn generate_automatons(query: &str, store: &S) -> Result(query: &str, store: &S) -> Result(query: &str, store: &S) -> Result { let mut iter = matches.into_iter(); - assert_matches!(iter.next(), Some(Match { query_index: 0, .. })); + assert_matches!(iter.next(), Some(Match { query_index: 0, distance: 0, .. })); // téléphone + assert_matches!(iter.next(), Some(Match { query_index: 0, distance: 1, .. })); // telephone + assert_matches!(iter.next(), Some(Match { query_index: 0, distance: 2, .. })); // télephone + assert_matches!(iter.next(), None); + }); + assert_matches!(iter.next(), None); + } + + #[test] + fn simple_concatenation() { + let store = InMemorySetStore::from_iter(vec![ + ("iphone", &[doc_index(0, 0)][..]), + ("case", &[doc_index(0, 1)][..]), + ]); + + let builder = QueryBuilder::new(&store); + let results = builder.query("i phone case", 0..20).unwrap(); + let mut iter = results.into_iter(); + + assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches }) => { + let mut iter = matches.into_iter(); + assert_matches!(iter.next(), Some(Match { query_index: 0, word_index: 0, distance: 0, .. })); // iphone + assert_matches!(iter.next(), Some(Match { query_index: 1, word_index: 0, distance: 1, .. })); // phone + assert_matches!(iter.next(), Some(Match { query_index: 2, word_index: 1, distance: 0, .. })); // case assert_matches!(iter.next(), None); }); assert_matches!(iter.next(), None);