为什么我的Firestore复合索引不起作用?

huangapple go评论76阅读模式
英文:

Why doesnt my Firestore composite index work?

问题

I'm relatively new to using Firestore. 我对使用Firestore相对较新。
I want to filter my collection using composite indexes but it doesn't seem to work? 我想使用复合索引来过滤我的集合,但似乎不起作用?
I'm not sure what went wrong. 我不确定出了什么问题。
Here's my code: 这是我的代码:

Stream<List<Listing>> readListings() {
    DateTime currentDateTime = DateTime.now();
    return FirebaseFirestore.instance
        .collection('Listings')
        .where("expiryDate", isGreaterThan: currentDateTime)
        .where("isAvailable", isEqualTo: true)
        .snapshots()
        .map((snapshot) =>
            snapshot.docs.map((doc) => Listing.fromJson(doc.data())).toList());
}

The error message is: 错误消息是:

W/Firestore( 9339): (24.6.0) [Firestore]: Listen for Query(target=Query(Listings where expiryDate&gt;time(1686292045,716000000) and isAvailable==true order by expiryDate, __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/foodbridge-b1332/firestore/indexes?create_composite=ClFwcm9qZWN0cy9mb29kYnJpZGdlLWIxMzMyL2RhdGFiYXNlcy8oZGVmYXVsdCkvY29sbGVjdGlvbkdyb3Vwcy9MaXN0aW5ncy9pbmRleGVzL18QARoPCgtpc0F2YWlsYWJsZRABGg4KCmV4cGlyeURhdGUQARoMCghfX25hbWVfXxAB, cause=null}   
D/EGL_emulation( 9339): app_time_stats: avg=402.96ms min=14.32ms max=1517.38ms count=4
W/Firestore( 9339): (24.6.0) [WatchStream]: (aef561b) Stream closed with status: Status{code=CANCELLED, description=Disconnecting idle stream. Timed out waiting for new targets., cause=null}.
D/TrafficStats( 9339): tagSocket(136) with statsTag=0xffffffff, statsUid=-1

However, I already created the index in Firestore? 但是,我已经在Firestore中创建了索引?
screenshot of index in Firestore 我已经在Firestore中创建了索引?
I tried adding sortby too but it doesn't work 我也尝试添加排序,但不起作用。

英文:

I'm relatively new to using firestore. I want to filter my collection using composite indexes but it doesn't seem to work? I'm not sure what went wrong. Here's my code:

Stream&lt;List&lt;Listing&gt;&gt; readListings() {
    DateTime currentDateTime = DateTime.now();
    return FirebaseFirestore.instance
        .collection(&#39;Listings&#39;)
        .where(&quot;expiryDate&quot;, isGreaterThan: currentDateTime)
        .where(&quot;isAvailable&quot;, isEqualTo: true)
        .snapshots()
        .map((snapshot) =&gt;
            snapshot.docs.map((doc) =&gt; Listing.fromJson(doc.data())).toList());
  }

The error message is:

W/Firestore( 9339): (24.6.0) [Firestore]: Listen for Query(target=Query(Listings where expiryDate&gt;time(1686292045,716000000) and isAvailable==true order by expiryDate, __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/foodbridge-b1332/firestore/indexes?create_composite=ClFwcm9qZWN0cy9mb29kYnJpZGdlLWIxMzMyL2RhdGFiYXNlcy8oZGVmYXVsdCkvY29sbGVjdGlvbkdyb3Vwcy9MaXN0aW5ncy9pbmRleGVzL18QARoPCgtpc0F2YWlsYWJsZRABGg4KCmV4cGlyeURhdGUQARoMCghfX25hbWVfXxAB, cause=null}   
D/EGL_emulation( 9339): app_time_stats: avg=402.96ms min=14.32ms max=1517.38ms count=4
W/Firestore( 9339): (24.6.0) [WatchStream]: (aef561b) Stream closed with status: Status{code=CANCELLED, description=Disconnecting idle stream. Timed out waiting for new targets., cause=null}.
D/TrafficStats( 9339): tagSocket(136) with statsTag=0xffffffff, statsUid=-1

However, I already created the index in firestore?
screenshot of index in firestore

I tried adding sortby too but it doesn't work

答案1

得分: 2

你在HomePageAllAvailableListings集合上执行了一个查询,但你需要为listings集合创建索引。请打开错误信息中提供的链接创建索引。

英文:

You execute a query on the listings collection while your index is on the HomePageAllAvailableListings collection. You need to create the index for the listings collection by opening the URL provided in the error message.

huangapple
  • 本文由 发表于 2023年6月9日 14:42:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定