Luke Wheeler
Luke Wheeler

Reputation: 41

Using Firestore as a DB, how can I just provide multiple data filters to users without free-text search?

I'm building a fairly simple Next.js app that allows users to browse potentially millions of user profiles by 4-5 key facets (last login date, team, tags, a few other basic fields).

I soon discovered that Firestore won't allow me to run the queries I need to, if I wish to support multiple filters. I could get, for example, all the profiles with a matching team, but to filter all the other facets I'd have to do it on the front end and end up invoking far more document reads than I'd like to.

Algolia to the rescue! Works great, but boy is that cost going to rack up. I can't feasibly launch this app with Algolia or I'll have to remortgage my house. I also absolutely do not need text search, just basic faceting, so I really feel like Algolia is overkill here regardless of the cost implications.

I really do not want to start querying Firestore for an insane number of documents and then run the filtering on the client, unless this really is the BEST option for my needs. But I need advice.

Thanks guys!

Upvotes: 2

Views: 272

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 1

I soon discovered that Firestore won't allow me to run the queries I need to if I wish to support multiple filters.

That's not true, Firestore does for sure allow multiple filters.

I could get, for example, all the profiles with a matching team, but to filter all the other facets I'd have to do it on the front end

You can create a query and chain multiple whereEqualTo() functions. This means that you can filter by multiple fields at once. Here is an example:

rootRef.whereEqualTo("lastLoginData", lastLoginData).whereEqualTo("team", nameOfTeam);

You can chain as many functions as you want. That's an example for Android, but you can find the corresponding queries for each platform and programming language. If you also need to order the results, don't forget to create an index.

Algolia to the rescue!

Algolia is useful only if you need full-text searches in your app, otherwise, for simple queries, you can use the filters that are currently available.

Getting documents and filtering them o the client is not an option. Always filter the documents on the server and get only the results that you need.

Upvotes: 1

Related Questions