While Fogmeister's answer can help you reduce the number of reads significantly, I'll try to provide you another approach, but it will imply a change in your database structure. So this is what I would do.
First of all I will stop searching the maritime_terms
collection in the way you do. Instead of that, I will create a document that will hold all the terms that you have in the database. Every term will be added in an array of terms. According to the length of the terms, it's possible to fit in one document all terms. I say that because the documents have limits. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:
Maximum size for a document: 1 MiB (1,048,576 bytes)
As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much.
If all the terms do not fit in a single document, then you can simply create a document for each letter from the alphabet. Since you know what you search, it will be very easy to know what document to read. So take a look at the following schema:
Firestore-root
|
--- alphabetTerms (collection)
|
--- a (document)
| |
| --- aTerms: ["A-Term", "A-Term", "A-Term"]
|
--- b (document)
|
--- bTerms: ["B-Term", "B-Term", "B-Term"]
To get the a
document, simply get a reference and make a get()
call:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference aRef = rootRef.collection("alphabetTerms").document("a");
aRef.get().addOnCompleteListener(/* ... */);
Even if the the aTerms
is an array, when you make a get()
call, you'll get it as a List<String>
and not as an array. Then you can simply search that list of strings using:
termFromTheList.contains(searchedTerm);
One thing to note is that all terms must be unique in order to use this solution.
This solution it's a much better solution than the one you are using right now since it allow you to search a substring. For instance, if you want to search in broad reach
for reach
, this is not possible, unless you are using a third party service like Algolia.
Once you find a match, get it and create a new query:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference maritimeTermsRef = rootRef.collection("maritime_terms");
Query searchQuery = maritimeTermsRef.whereEqualTo("term", searchedTerm);
searchQuery.get().addOnCompleteListener(/* ... */);
Using the solution above, you'll ending up having only one document read for finding the term in the document and one document read for displaying the details of the term.
So you'll reduce the number of reads from 150 to only 2.