Firestore Trigram Fuzzy Search

Firestore Trigram Fuzzy Search

5 min read

The Trigram search is just that, it uses Trigrams. Trigrams are groups of 3 letters stored in a database to help with searching. Some versions of SQL use this technique, and unless you are a database designer, you may have never heard of it. Here is an example of a trigram for the word jonathan: ``` jon, ona, nat, ath, tha, han ``` There are 6 trigrams. If I search for all six of them, I will get an exact match. If I search for 5 of the 6 trigrams (6 possible ways), I can get a typo-tolerant search, or what is know as a fuzzy search. I will get close enough to what I am looking for. This is one way search engines can find close matches. Google probably stores every typo individually and uses AI to determine what you're looking for. But we do not have that luxury. To use this: ```typescript await trigramIndex(change, context, { fields: ['content', 'summary'] }); ``` Simply add this index. Note: all my new searches and counters handle events in the backend to ensure you do not have a repeated function. That is it! Now you can use my backend callable function: ```typescript return await trigramSearch({ query: q.query, col: 'posts', }); ``` But there is a huge downside: it is very, very slow. It takes a second for the micro-service to load, then it uses promises, slower than observables, and fetches back to the client. A trigram algorithm is particularly slow as it must combine many searches together with out each trigram. I highly recommend you write your own version on the frontend, or use this Observable Version to see the gist of it, no pun intended. This will dramatically increase the speed. I am only writing backend code for the moment, but this will get you started, or for the backend Promise Version to copy to the frontend. This search is awesome, and decent speed as long as you use observables on the front end. You can see the example on this website using observables, which is not too bad. Relevant search and full-text search are other options, depending on your needs. If speed and number of reads is more important than typos, I recommend Relevant search. If space is more important, I recommend my old Full Text Search. Good luck!
search
cloudfunction
index