From 284d8a24e0caf2376a68aeb1fd63691e4b2270c9 Mon Sep 17 00:00:00 2001 From: ad hoc Date: Mon, 4 Apr 2022 13:59:29 +0200 Subject: [PATCH] add intergration test for disabled typon on word --- milli/tests/search/typo_tolerance.rs | 81 +++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/milli/tests/search/typo_tolerance.rs b/milli/tests/search/typo_tolerance.rs index 00e6853cc..7d19e4ab0 100644 --- a/milli/tests/search/typo_tolerance.rs +++ b/milli/tests/search/typo_tolerance.rs @@ -1,5 +1,10 @@ -use milli::update::{IndexerConfig, Settings}; -use milli::{Criterion, Search}; +use std::collections::BTreeSet; + +use heed::EnvOpenOptions; +use milli::update::{IndexDocuments, IndexDocumentsConfig, IndexerConfig, Settings}; +use milli::{Criterion, Index, Search}; +use serde_json::json; +use tempfile::tempdir; use Criterion::*; #[test] @@ -93,3 +98,75 @@ fn test_typo_tolerance_two_typo() { let result = search.execute().unwrap(); assert_eq!(result.documents_ids.len(), 1); } + +#[test] +fn test_typo_disabled_on_word() { + let tmp = tempdir().unwrap(); + let mut options = EnvOpenOptions::new(); + options.map_size(4096 * 100); + let index = Index::new(options, tmp.path()).unwrap(); + + let documents = json!([ + { + "id": 1usize, + "data": "zealand", + }, + { + "id": 2usize, + "data": "zearand", + }, + ]); + + let mut writer = std::io::Cursor::new(Vec::new()); + let mut builder = milli::documents::DocumentBatchBuilder::new(&mut writer).unwrap(); + let documents = serde_json::to_vec(&documents).unwrap(); + builder.extend_from_json(std::io::Cursor::new(documents)).unwrap(); + builder.finish().unwrap(); + + writer.set_position(0); + + let documents = milli::documents::DocumentBatchReader::from_reader(writer).unwrap(); + + let mut txn = index.write_txn().unwrap(); + let config = IndexerConfig::default(); + let indexing_config = IndexDocumentsConfig::default(); + let mut builder = IndexDocuments::new(&mut txn, &index, &config, indexing_config, |_| ()); + + builder.add_documents(documents).unwrap(); + + builder.execute().unwrap(); + txn.commit().unwrap(); + + // basic typo search with default typo settings + { + let txn = index.read_txn().unwrap(); + + let mut search = Search::new(&txn, &index); + search.query("zealand"); + search.limit(10); + search.authorize_typos(true); + search.optional_words(true); + + let result = search.execute().unwrap(); + assert_eq!(result.documents_ids.len(), 2); + } + + let mut txn = index.write_txn().unwrap(); + + let config = IndexerConfig::default(); + let mut builder = Settings::new(&mut txn, &index, &config); + let mut exact_words = BTreeSet::new(); + // sealand doesn't allow typos anymore + exact_words.insert("zealand".to_string()); + builder.set_exact_words(exact_words); + builder.execute(|_| ()).unwrap(); + + let mut search = Search::new(&txn, &index); + search.query("zealand"); + search.limit(10); + search.authorize_typos(true); + search.optional_words(true); + + let result = search.execute().unwrap(); + assert_eq!(result.documents_ids.len(), 1); +}