Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore: FAILED_PRECONDITION: The query requires an index

I made a query in Cloud Firestore,

CollectionReference questionRef = db.collection("collectionName");
        Query query = questionRef.whereEqualTo("field1", "content1")
                .whereEqualTo("field2",content2)
                .orderBy("field3")
                .limit(LIMIT);
        query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>()
        {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task)
            {
                if (task.isSuccessful())
                {
                    for (DocumentSnapshot document : task.getResult())
                    {
                        Log.d(TAG, document.getId() + " => " + document.getData());
                    }
                }
                else
                {
                    Log.w(TAG, "Error getting documents.", task.getException());
                }
            }
        });

and I received errors, but I had an index.

Error getting documents. com.google.firebase.firestore.FirebaseFirestoreException: FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/project/exam-package/database/firestore/indexes?create_index=EglxYmFua2xpc3QaCQoFdmFsaWQQAhoNCgl0aW1lc3RhbXAQAxoMCghfX25hbWVfXxAD at com.google.firebase.firestore.g.zzs.zza(SourceFile:100) at com.google.firebase.firestore.b.zzd.zza(SourceFile:122) at com.google.firebase.firestore.b.zzab.zza(SourceFile:333) at com.google.firebase.firestore.b.zzf.zza(SourceFile:236) at com.google.firebase.firestore.f.zzo.zza(SourceFile:6529) at com.google.firebase.firestore.f.zzv.zzb(SourceFile:2089) at com.google.firebase.firestore.f.zza$zzb.zza(SourceFile:73) at com.google.firebase.firestore.g.zzm$1.onMessage(SourceFile:77) at io.grpc.ForwardingClientCallListener.onMessage(ForwardingClientCallListener.java:36) at io.grpc.ForwardingClientCallListener.onMessage(ForwardingClientCallListener.java:36) at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1MessagesAvailable.runInContext(ClientCallImpl.java:498) at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37) at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at com.google.firebase.firestore.g.zza$zza.run(SourceFile:190) at java.lang.Thread.run(Thread.java:764) Caused by: io.grpc.StatusException: FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/project/exam-package/database/firestore/indexes?create_index=EglxYmFua2xpc3QaCQoFdmFsaWQQAhoNCgl0aW1lc3RhbXAQAxoMCghfX25hbWVfXxAD at io.grpc.Status.asException(Status.java:534) at com.google.firebase.firestore.g.zzs.zza(SourceFile:98) at com.google.firebase.firestore.b.zzd.zza(SourceFile:122)  at com.google.firebase.firestore.b.zzab.zza(SourceFile:333)  at com.google.firebase.firestore.b.zzf.zza(SourceFile:236)  at com.google.firebase.firestore.f.zzo.zza(SourceFile:6529)  at com.google.firebase.firestore.f.zzv.zzb(SourceFile:2089)  at com.google.firebase.firestore.f.zza$zzb.zza(SourceFile:73)  at com.google.firebase.firestore.g.zzm$1.onMessage(SourceFile:77)  at io.grpc.ForwardingClientCallListener.onMessage(ForwardingClientCallListener.java:36)  at io.grpc.ForwardingClientCallListener.onMessage(ForwardingClientCallListener.java:36)  at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1MessagesAvailable.runInContext(ClientCallImpl.java:498)  at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)  at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)  at java.util.concurrent.FutureTask.run(FutureTask.java:266)  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)  at com.google.firebase.firestore.g.zza$zza.run(SourceFile:190)  at java.lang.Thread.run(Thread.java:764)

like image 656
Jack Wilson Avatar asked May 07 '18 05:05

Jack Wilson


People also ask

What types of indexes are automatically created in cloud firestore?

By default, Cloud Firestore automatically maintains single-field indexes for each field in a document and each subfield in a map.

How many data types does cloud firestore support?

Note: Cloud Firestore supports a variety of data types for values: boolean, number, string, geo point, binary blob, and timestamp. You can also use arrays or nested objects, called maps, to structure data within a document.

How do you make a field unique in firestore?

You can use Firestore queries to get the document ID which corresponds to the field you want to keep unique. If the query returns null , that would mean that the database doesn't contain that value., ergo, the username , in this case, is available or vice versa.


1 Answers

as firebase recommends:

Instead of defining a composite index manually, run your query in your app code to get 
a link for generating the required index.

the exception message has a link to create a specific index which exactly required by your failed query

like image 70
Mohammad Desouky Avatar answered Oct 02 '22 22:10

Mohammad Desouky