From 6a10b679ca3330f2cc083b6c5b1674c654153b7e Mon Sep 17 00:00:00 2001 From: Samyak S Sarnayak Date: Thu, 13 Oct 2022 19:13:40 +0530 Subject: [PATCH] Add test for phrase search with stop words Originally written by ManyTheFish here: https://gist.github.com/ManyTheFish/f840e37cb2d2e029ce05396b4d540762 Co-authored-by: ManyTheFish --- milli/tests/search/mod.rs | 1 + milli/tests/search/phrase_search.rs | 35 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 milli/tests/search/phrase_search.rs diff --git a/milli/tests/search/mod.rs b/milli/tests/search/mod.rs index c8b01648c..78640cfb9 100644 --- a/milli/tests/search/mod.rs +++ b/milli/tests/search/mod.rs @@ -18,6 +18,7 @@ mod filters; mod query_criteria; mod sort; mod typo_tolerance; +mod phrase_search; pub const TEST_QUERY: &'static str = "hello world america"; diff --git a/milli/tests/search/phrase_search.rs b/milli/tests/search/phrase_search.rs new file mode 100644 index 000000000..87b3fd511 --- /dev/null +++ b/milli/tests/search/phrase_search.rs @@ -0,0 +1,35 @@ +use milli::update::{IndexerConfig, Settings}; +use milli::{Index, Search, TermsMatchingStrategy}; + +fn set_stop_words(index: &Index, stop_words: &[&str]) { + let mut wtxn = index.write_txn().unwrap(); + let config = IndexerConfig::default(); + + let mut builder = Settings::new(&mut wtxn, &index, &config); + let stop_words = stop_words.into_iter().map(|s| s.to_string()).collect(); + builder.set_stop_words(stop_words); + builder.execute(|_| ()).unwrap(); + wtxn.commit().unwrap(); +} + +#[test] +fn test_phrase_search_with_stop_words() { + let criteria = []; + let index = super::setup_search_index_with_criteria(&criteria); + + // Add stop_words + set_stop_words(&index, &["a", "an", "the", "of"]); + + // Phrase search containing stop words + let txn = index.read_txn().unwrap(); + + let mut search = Search::new(&txn, &index); + search.query("\"the use of force\""); + search.limit(10); + search.authorize_typos(false); + search.terms_matching_strategy(TermsMatchingStrategy::All); + + let result = search.execute().unwrap(); + // 1 document should match + assert_eq!(result.documents_ids.len(), 1); +}