0

I want to perform OR query operation in Firestore using Java client. I am trying to merge the queries.

Code :

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query firstQuery = rootRef...
Query secondQuery = rootRef...

Task firstTask = firstQuery.get();
Task secondTask = secondQuery.get();

Task combinedTask = Tasks.whenAllSuccess(firstTask, secondTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
    @Override
    public void onSuccess(List<Object> list) {
         //Do what you need to do with your list
    }
});

as suggested in Firestore - Merging two queries locally

But it is not working for me, could anyone please suggest to me how to join query shot operations using java client for fire store.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Neha
  • 1
  • Is your onSuccess even triggered? – Alex Mamo Oct 04 '21 at 14:29
  • I am unable to create the task object, For me :query.get() does not gives back task object , ApiFuture qs = query.get(); object is of this type , I am using maven dependency dependency> com.google.firebase firebase-admin 8.0.1 com.google.api-client google-api-client 1.23.0 in java project – Neha Oct 04 '21 at 14:38
  • So you aren't using Java for an Android application? – Alex Mamo Oct 04 '21 at 14:53
  • I am using java , but it is not an android application – Neha Oct 04 '21 at 14:59

1 Answers1

0

If you are not using Java for Android, then you should consider using Firebase Admin SDK. In the linked documentation you'll find all the necessary details that can help you interact with Firebase.

Access Google Cloud resources like Cloud Storage buckets and Cloud Firestore databases associated with your Firebase projects.

And yes, the get() method that is called on a Query object doesn't return a Task object, but a ApiFuture.

As far as I can see, there is no method there that can help you combine two queries. You can only addListener() and read the data accordingly.

However, the is a workaround that can help you achieve that, which is by using a CompletableFuture. I personally didn't use it in development, as I'm working mainly with Android, but you can check this out:

I want to perform OR query operation in Firestore using Java.

When it comes to Firestore, you might also take into consideration using the in operator to combine up to 10 equality (==) clauses on the same field with a logical OR.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193