leontoeides / indicium

A simple in-memory search for collections and key-value stores.
https://crates.io/crates/indicium
Apache License 2.0
64 stars 3 forks source link

Hello AutoCompleteType have unexcepted behavior #1

Closed csh0101 closed 2 years ago

csh0101 commented 2 years ago

struct MyType<'a>(&'a str);

impl<'a> Indexable for MyType<'a> { fn strings(&self) -> Vec { vec![self.0.to_string()] } }

impl From<&'static str> for MyType<'static> { fn from(input: &'static str) -> Self { MyType(input) } }

use indicium::simple::AutocompleteType; use indicium::simple::SearchIndex; use indicium::simple::SearchIndexBuilder;

fn main() { let mut search_index: SearchIndex = SearchIndexBuilder::default() .autocomplete_type(AutocompleteType::Global) .build();

search_index.insert(&0, &MyType::from("apple"));
search_index.insert(&1, &MyType::from("ball"));
search_index.insert(&3, &MyType::from("bird"));
search_index.insert(&4, &MyType::from("birthday"));
search_index.insert(&5, &MyType::from("red"));

let autocomplete_options: Vec<String> = search_index.autocomplete("a very big bi");

assert_eq!(
    autocomplete_options,
    vec!["a very big bird", "a very big birthday"]
);

}


Behavor: the left do not equal with right, because the 'a' is not autocomplete
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `["very big bird", "very big birthday"]`,
 right: `["a very big bird", "a very big birthday"]`', src/main.rs:34:5
* rustc version
1.62.0
* rustup show 
Default host: aarch64-apple-darwin
rustup home:  /Users/cs/.rustup

installed toolchains
--------------------

stable-aarch64-apple-darwin (default)
1.61.0-aarch64-apple-darwin

active toolchain
----------------

stable-aarch64-apple-darwin (default)
rustc 1.62.0 (a8314ef7d 2022-06-27)
leontoeides commented 2 years ago

Thank you for the feedback @csh0101. Sorry for the delay responding, I did not see that you had posted this until now.

When you instantiate the SearchIndexBuilder with default settings by using SearchIndexBuilder::default() it will automatically remove small keywords such as "a", "an", "and", "the", etc. If you add SearchIndexBuilder::default().exclude_keywords(None), small words will no longer be excluded from searches and autocompletion options. You can also use exclude_keywords to add support for small word removal in other languages.

If I re-run your example with the following builder clause, it will work as expected and pass the test:

let mut search_index: SearchIndex<usize> = SearchIndexBuilder::default()
    .autocomplete_type(AutocompleteType::Global)
    .exclude_keywords(None)
    .build();

Also note that the newest version of Indicium supports all types that implement the ToString trait. Now you can do this:

search_index.insert(&0, &"apple");
search_index.insert(&1, &"ball");
search_index.insert(&3, &"bird");
search_index.insert(&4, &"birthday");
search_index.insert(&5, &"red");

Without having to implement anything.

I hope this helps. Let me know if you have any other questions.

leontoeides commented 2 years ago

Closing this issue. If you have any other issues or questions, feel free to add