I am trying to search Documents in a Collection which contain a certain key.
Here is how I structured my Firestore database:
-- FirestoreRoot
|-- Products (Collection)
|-- Departments (Document)
|-- Food (Colletion)
|-- {Id} (Document)
-- description : "this is my very first description"
-- keywords :
-- 0 : this
-- 1 : is
-- 2 : my
-- 3 : very
-- 4 : first
-- 5 : description
In the example below, I was able to search by using a substring for the first word in the description. To this date, this method does not work for querying the following words. In the example below, typing the letters "thi" is enough to return documents.
CollectionReference colecRef = FirebaseFirestore.getInstance()
.collection("Products")
.document("Departments")
.collection("Food");
Query query = colecRef;
query.whereGreaterThanOrEqualTo("description", searchField.getText().toString().toLowerCase())
.whereLessThan("description", searchField.getText().toString().toLowerCase()+'\uf8ff')
.get().addOnSuccessListener(SearchActivity.this, new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Log.i(TAG, "onSuccess: "+documentSnapshot);
}
}
});
In my case, I need to search by using 1 or multiple keys, for example: first or first description. And by using the whole word or only a substring, for example: first or descr
I've tried to use whereArrayContains(), but I can't use it typing multiple keys or substring.
query.whereArrayContains("keywords",searchField.getText().toString().toLowerCase())
.get().addOnSuccessListener(SearchActivity.this, new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Log.i(TAG, "onSuccess: "+documentSnapshot);
}
}
})
I appreciate any help.
android-studiotag for questions about the Android Studio IDE itself. For questions about Android programming in general, use theandroidtag.