Looking for a cheap way to add full-text search to firestore
Asked Answered
M

2

7

Here is what I want to do

Lets assume I have an Angular Dart application with a firestore backend that e.g. stores books. Each book has a title, a summary and a published date. Now I want to add a fulltext search with pagination to my app. Since firestore does not have fulltext search I need to add this myself if I want to have e.g the first 10 books containing "cat" ordered by published date.

Failed attempts

My first idea was to use the Elasticsearch docker container and deploy that on Google Cloud. Initially I tried to do this on Google Kubernetes but besides being a bit over-complicated it seems to cost way too much. Next I looked into Google App Engine with docker but this still seems to come up to 60$ per month since the Elasticsearch container needs 4GB ram.

So now I am stuck what to do. Look into Google Compute Engine and running docker inside a VM. I wonder how much that costs? Or maybe write something to interface with Google Cloud Search from a Google App instance exposing the results somehow?

All of this seems to be way to complicated for what I want to do. I might have like a thousand books to index which comes up to maybe 10MB in data and only a few thousand queries per month.

Mulderig answered 12/5, 2018 at 14:58 Comment(1)
E
1

Algolia seems like the perfect/best solution for your use case. It is SO easy to setup and is lightning fast. Have a look at their docs and pricing and test it out yourself. There’s also a sample firestore function that may interest you.

Edwards answered 13/5, 2018 at 14:53 Comment(3)
Unfortunately the cheapest option is still 35$ per month. The maximum I could warrant for a side project would be around 5$. And no I can't use the open source plan.Mulderig
You say you only have about a thousand books. The free tier let’s you have 10000 books. Nevertheless, I’m not sure of any other way for less than $5. You could deploy Elasticsearch on a g1 compute engine instance for 14$ but that’s a shared cpu, so I’m not sure how reliable that would be. Also this wouldnt be that complicated. You dont need kubernetes to deploy elasticsearch. Providing a simple startup script that downloads Java/Elasticsearch when you create your VM would suffice. If your interested in a script, i can post one here.Edwards
Looks like there is no longer a free tier for Algolia, unless your project is open sourceReputed
E
0

If you don't want to use services like algolia and Typesense and you don't need any ai or prebuilt tools then I have one simple solution using list.

In order to implement this solution, you need to split the string by spaces and exclude certain words such as "as," "is," "the," "a," "this," "that," conjunctions, and demonstrative pronouns. Then, you can add a list of strings to a Firestore document within a book document. Utilize appropriate methods to ensure that all relevant field data is included for the purpose of searching the document by words.

When searching, use "array-contains-any" instead of "==" and split the query like a Firebase document field. See example below.

I hope this will be helpful in finding what you are looking for. Let me know if you need any further assistance.

var bookCollectionName = 'bookCollectionName';
    var wordListFieldName = 'wordlistfieldname';
    var bookSearchQueryWordList = ['your', 'search', 'query', 'words'];

    var collectionRef = $firebaseFirestore.collection(bookCollectionName);
    var query = collectionRef.where(wordListFieldName, 'array-contains-any', bookSearchQueryWordList);

query.get().then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            // Handle the document data here
            console.log(doc.id, ' => ', doc.data());
        });
    }).catch(function(error) {
        console.error('Error getting documents: ', error);
    });

Note: change your collection name and field name according to your names

Embank answered 20/11, 2023 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.