I’m not sure if this already exists in some other library, but what I am trying to do is implement a typesense-style search that gives you results as you type in your search term.
The source is a json object that will be fetched by the client when the window loads. It will have approximately 10K keys that I want to search through. For example: https://redditstatsbot.com/json/alphabetbot.json
What I don’t want to do is have to type in the full key. The user must be able to get their unique ID by typing in the first few letters of their ID.
For example, typing in "str" would yield all keys that start with "str", ["stranger", "strongman", "stranded", ... ]
and then the user can select from the returned smaller list, whith the returned list growing or shrinking depending on the typed search term.
So basically, I don’t think that a full-blown Typesense implementation is necessary for my needs, because this is not a massive database, but I’m not really sure what my other options are.
3
Answers
The HTML and CSS can be styled, but this is just the basic implementation of it. Pretty much all it does is check the search bar’s input every time something is inputted, and then whatever item in the search results array starts the same as the search bar’s input is added onto a separate array. Then with your search result’s container, it fills up the divs’ inside of it with the separate array items, given the amount of results.
This is certainly solvable:
The above is an unoptimized proof-of-concept. We create events for previews so if you click them, then they refresh the search box and disappear. On the other hand, changing the search text induces a search for the first 10 matches.
You should try out something like this and see whether it works well for your large inputs. If not, then you will need to optimize performance.
A useful solution maybe to use something like minisearch package. Assuming you are loading an array of objects, you can try something like:
To use the package, make sure to include it as a script in your HTML:
You can query on
keyup
event on the searchinput
and use the results to display a dropdown selector. There are many good examples for that so I am leaving it out.This package can also handle prefix and fuzzy search. Hope that helps!