英文:
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>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<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>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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论