This is an issue I came across while trying to mix geo location with Firestore. Long story short - I need restaurants around user's location. In order to get geo search done I use Algolia. When I do the request to Algolia it returns an array of unique restaurant IDs which correspond to Firestore document ids. This works just fine.
What makes things complicated is that I need two more conditions - I need to restrict the query to restaurants with average rating >= 8. And also I want to limit the count of the returned documents (2, 5, 20 etc).
So this is how it should look like in pseudo code:
db.restaurantsCollection
.documentIds([111, 222, 333, 444, 555, 666, 777, 888, 999])
.whereField("averageRating", isGreaterThanOrEqualTo: 8)
.order(by: "averageRating", descending: true)
.limit(to: 2)
.getDocuments()
I know as of today Firestore doesn't support queries with multiple document ids. So what is the most optimized way to perform such a query?
If I set the documentId
as an id
field in my document and then iterate through all of the returned ids from Algolia and do something like this (then I can do the ordering and limiting in pure Swift):
for id in ids {
db.restaurantsCollection
.whereField("averageRating", isGreaterThanOrEqualTo: 8)
.whereField("id", isEqualTo: id)
.getDocuments()
}
But still this means a lot of requests. Any other ideas?