jeancroy / FuzzySearch

:mag: Fast autocomplete suggestion engine using approximate string matching
MIT License
194 stars 32 forks source link

updating the index in the middle of search #43

Open halukkaramete opened 1 year ago

halukkaramete commented 1 year ago

Hi Jean,

I've got a 13,000 item JSON. and works great....
but sometimes, I need to add, say 1000-to-2000 more items if I detect a certain pattern in the user input!

The example, you give in the docs is as follows but it is only for a single item addding.

    var data = ["survey","surgery","insurgence"];
    var searcher = new FuzzySearch({source:data});
    var query = "assurance";
    var result = searcher.search(query)
    var newEntry = "surgeon";
    searcher.add(newEntry);
    var newResult = searcher.search("surgeo");

And, this example is only for a single dimension array! In my case, the json array is multiple item! And I specify which key to search for too ( thru the final arg of setsource function )

When adding 1000 or more items, repeating code such as var newEntry = "surgeon";searcher.add(newEntry); 1000 times does not make sense... I also do not know how to point to a key in a more complex json array.

Is there a way in the API to tell the engine, "here! add this JSON file on top of the first json loaded! " or perhaps, completely change the json with another json in the middle of the search..

Again, the scenario is something like this..

User types a string, typeahead start working based on the original json data set... user input reaches the 4th word (and because it is a long input or my algorithm detects a certain pattern in the user input), I then replace the org json with another data set, so that the input at that moment is searched against the new json data set... And if the user starts removing words (or deletes that pattern), I can load the first json...

Can I do something like this?

jeancroy commented 1 year ago

https://github.com/jeancroy/FuzzySearch/blob/master/src/source.js#L111-L117

This is how the initial JSON is processed. Just a simple loop over add() You can do the same if you want to add multiple items. Add() is not only for simple 1D, it process literally everything that is compatible with search.

If you want to be smart and only add new entry. Look at options.identify_item this allow to setup an ID and avoid duplicate.