What Is The Best Way To Implement Simple Text Search
Solution 1:
I would suggest you to use Algolia for searching, it has got a lot of features, because in your case you are having 750 reads for one search and if the number of documents and/or searches increases then there will huge number of reads required and it would cost you a lot.
eg: if there are 750 documents and 100 searches a day, then you have 75,000 reads a day and in that case you are out of free quota which gives 50,000 reads per day
if you don't prefer algolia then you can use elastic search also.
Solution 2:
After Algolia raised their prices 10x times and presented it as simplifying
their pricing model it's time to think more before implementing.
If you don't want to use third party APIs.
If you feel like search for word presence is enough for you.
If you don't need any phonetic algorithm be a part of your search.
You can use just Firebase.
Say you want your funkoData[].name
to be searchable.
- Concatenate all
funkoData[].name
together and lowercase it.
funkoData.map(v=>v.name).join(" ").toLowerCase()
- Split your string by all types of whitespaces and special chars.
funkoData.map(v=>v.name).join(" ").toLowerCase().split(/[\s-\.,!?]/)
- Filter for short words and empty strings.
funkoData.map(v=>v.name).join(" ").toLowerCase().split(/[\s-\.,!?]/).filter(v=>v.length>4)
- Compute this using cloud functions.
Full example:
let searchable = documentData.funkoData
.map(v=>v.name)
.join(" ")
.toLowerCase()
.split(/[\s-\.,!?]/)
.filter(v=>v.length>4);
searchable = Array.from(newSet(searchable)); //remove duplicatesawait documentRef.update({_searchArray: searchable});
Now. Search is possible with array-contains
or array-contains-any
:
const query = "Loki";
const normalizedQuery = query.trim().toLowerCase();
firestoreRef.collection("funkoPops").where('_searchArray', 'array-contains', normalizedQuery).get()
PS: If your searchable array has a potential to become huge, you can move it to a separate collection with the same ids
and trigger search using https callable cloud function
returning just ids of documents that fit search criteria. Why? Because using Admin SDK you have an option to skip specified fields to be returned in a query response.
Post a Comment for "What Is The Best Way To Implement Simple Text Search"