1

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.

1
  • 2
    Please only use the android-studio tag for questions about the Android Studio IDE itself. For questions about Android programming in general, use the android tag. Commented Nov 20, 2019 at 23:14

1 Answer 1

2

Firestore currently only offers two ways to query the contents of an array:

As you can see, searching substrings in arrays isn't going to work at all. You might want to consider using another database in tandem with Firestore in order to satisfy these specific queries, as Firestore is not very well suited for them.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using firestore:20.0.0, but whereArrayContainsAny() is not available for Query and .collection("Food") .where("keywords", "array-contains-any", ["first", "description"]) is also not available. What am I missing?
Is there something close to whereArrayContainsAny() or whereArrayContains() for Firebase Realtime Database?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.