Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search in Firestore using multiple fields of documents for android?

I'm using a Firebase Firestore for android to store data. I tried to search data from documents.

My Firestore structure is:

Collection (Products) - Document (Auto Generated ID) - Field (NumberOfPeople,OfferStartDate,OfferEndDate,OfferPrice,Location)

I wrote query for search data on those fields.

CollectionReference collectionOfProducts = db.collection("Products");

collectionOfProducts
                    .whereEqualTo("Location", location)
                    .whereGreaterThanOrEqualTo("OfferPrice", offerPrice)
                    .whereLessThanOrEqualTo("OfferPrice", offerPrice)
                    .whereGreaterThanOrEqualTo("OfferStartDate", date)
                    .whereLessThanOrEqualTo("OfferEndDate", date)
                    .get()

I want search result like this: An offer which is between start date and end date, where offer price is greater than equal or less than equal on give price range. This query is not working in android studio.

How to do this in firestore firebase?

like image 773
Sam Jubayer Avatar asked Mar 22 '18 06:03

Sam Jubayer


People also ask

Can I search for content in Cloud Firestore?

Register Most apps allow users to search app content. For example, you may want to search for posts containing a certain word or notes you've written about a specific topic. Cloud Firestore doesn't support native indexing or search for text fields in documents.

What is the use case for FireStore in Android?

A common use case in Android applications is to provide the user with the functionality to search through a list of records. While Firestore is a solid database to power read heavy applications, it does not offer an out of the box, full text search.

How to do a full text search in FireStore?

The official Firestore documentation regarding full text search suggests us to use a third-party search service such as Elastic Search or Algolia. However, choosing the right third-party search service comes with its own overheads such as pricing, maintaining an additional service layer etc.

What do I need to know to use FireStore?

You must know the basics of Android, Firestore, and Kotlin. The most important part of the Firestore is to define the database structure. So that we can query each keyword and match it with our actual display result.


2 Answers

According to the official documentation regarding Cloud Firestore queries, please note that there some query limitations:

In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.

So a Query object that contains a call to both methods:

.whereGreaterThanOrEqualTo("OfferStartDate", date)
.whereLessThanOrEqualTo("OfferEndDate", date)

Is actually not possible, as "OfferStartDate" and "OfferEndDate" are different properties.

The best solution I can think of is to use only one of these method calls and do the other filtering on the client.

Another possible solution might be to use denormalization and duplicate the data in certain intervals. In this way, you'll always know the time periods and you'll be able to create the corresponding queries.

like image 88
Alex Mamo Avatar answered Oct 11 '22 22:10

Alex Mamo


To the best of my knowledge, Firestore only lets you use where<Greater/Less>ThanOrEqualTo() and where<Greater/Less>Than() a single field and all other filter operations on other fields can only be whereEqualTo().

Some workarounds for your specific case include -

1) Modifying your query to

collectionOfProducts
                    .whereGreaterThanOrEqualTo("OfferStartDate", date)
                    .whereEqualTo("Location", location)
                    .get() 

And then performing the subsequent filtering on the result in your app code.

Alternately, you can perform your filter on "OfferPrice" and "Location" in your query and the remaining filters can be applied to the query result.

2) You can use firebase functions or other server code to write logic that performs customized filtering and fetch the result that way.

like image 20
Karan Modi Avatar answered Oct 11 '22 23:10

Karan Modi