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)
By default, Cloud Firestore automatically maintains single-field indexes for each field in a document and each subfield in a map.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With