簡體   English   中英

Firestore綜合索引排序不修改順序

[英]Firestore Composite Index Sort Not Modifying Order

問題

使用Firestore多屬性復合索引排序,返回的數據順序不會更改。 將其與對一個屬性執行的原始排序進行比較。 當前,僅在使用和不應用復合索引的情況下,按時間戳對返回的數據進行排序。

預期

使用timestampqualityScore的復合索引時,數據應根據qualityScore屬性進行排序。

履行

查詢結果由FirestorePagingOptions.Builder<Content>()方法setQuery()

val options: FirestorePagingOptions<Content> = FirestorePagingOptions.Builder<Content>()
            .setLifecycleOwner(this)
            .setQuery(viewModel.contentFeedQuery, config, Content::class.java).build()

我正在從FirestorePagingAdapteronBindViewHolder中讀取結果。

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int, content: Content) {
            println("QUALITY_SCORE: " + content.timestamp + " " + content.qualityScore)
            viewHolder.bind(content)
        }

原始排序

return FirestoreCollections.contentCollection
.collection(ALL_COLLECTION)
.orderBy(TIMESTAMP, DESCENDING)
.whereGreaterThanOrEqualTo(TIMESTAMP,getTimeframe(WEEK))  

結果

2018-08-26 01:15:08.548 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 15:17:26 PDT 2018 17.0
2018-08-26 01:15:08.574 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 13:30:44 PDT 2018 17.0
2018-08-26 01:15:11.698 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 11:16:47 PDT 2018 16.0
2018-08-26 01:15:11.728 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 08:09:52 PDT 2018 18.0
2018-08-26 01:15:11.777 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 07:38:36 PDT 2018 21.0
2018-08-26 01:15:11.804 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 06:16:23 PDT 2018 20.0
2018-08-26 01:15:11.849 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Thu Aug 23 15:39:59 PDT 2018 15.0
2018-08-26 01:15:11.890 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Thu Aug 23 07:23:51 PDT 2018 1.0
2018-08-26 01:15:11.915 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Wed Aug 22 16:15:02 PDT 2018 5.0
2018-08-26 01:15:11.947 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Wed Aug 22 08:00:12 PDT 2018 22.0
2018-08-26 01:15:12.000 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Tue Aug 21 17:28:03 PDT 2018 19.0
2018-08-26 01:15:12.050 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Tue Aug 21 08:59:06 PDT 2018 6.0
2018-08-26 01:15:12.115 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Tue Aug 21 08:17:53 PDT 2018 7.0
2018-08-26 01:15:12.167 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Mon Aug 20 22:40:56 PDT 2018 9.0
2018-08-26 01:15:12.235 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Mon Aug 20 06:58:18 PDT 2018 10.0
2018-08-26 01:15:12.318 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Mon Aug 20 04:07:27 PDT 2018 12.0
2018-08-26 01:15:12.367 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Sun Aug 19 21:08:31 PDT 2018 8.0
2018-08-26 01:15:12.410 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Sun Aug 19 15:11:37 PDT 2018 14.0
2018-08-26 01:15:12.449 27668-27668/app.carpecoin I/System.out: QUALITY_SCORE: Sun Aug 19 03:35:52 PDT 2018 200.0

綜合指數排序

return FirestoreCollections.contentCollection
.collection(ALL_COLLECTION)
.orderBy(TIMESTAMP, DESCENDING)
.orderBy(QUALITY_SCORE, DESCENDING)
.whereGreaterThanOrEqualTo(TIMESTAMP,getTimeframe(WEEK))  

結果

2018-08-26 01:13:54.549 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 15:17:26 PDT 2018 17.0
2018-08-26 01:13:54.579 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 13:30:44 PDT 2018 17.0
2018-08-26 01:13:58.110 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 11:16:47 PDT 2018 16.0
2018-08-26 01:13:58.205 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 08:09:52 PDT 2018 18.0
2018-08-26 01:13:58.339 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 07:38:36 PDT 2018 21.0
2018-08-26 01:13:58.420 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Fri Aug 24 06:16:23 PDT 2018 20.0
2018-08-26 01:13:58.590 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Thu Aug 23 15:39:59 PDT 2018 15.0
2018-08-26 01:13:58.840 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Thu Aug 23 07:23:51 PDT 2018 1.0
2018-08-26 01:13:58.940 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Wed Aug 22 16:15:02 PDT 2018 5.0
2018-08-26 01:13:59.041 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Wed Aug 22 08:00:12 PDT 2018 22.0
2018-08-26 01:13:59.183 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Tue Aug 21 17:28:03 PDT 2018 19.0
2018-08-26 01:13:59.360 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Tue Aug 21 08:59:06 PDT 2018 6.0
2018-08-26 01:13:59.427 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Tue Aug 21 08:17:53 PDT 2018 7.0
2018-08-26 01:13:59.467 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Mon Aug 20 22:40:56 PDT 2018 9.0
2018-08-26 01:13:59.517 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Mon Aug 20 06:58:18 PDT 2018 10.0
2018-08-26 01:13:59.567 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Mon Aug 20 04:07:27 PDT 2018 12.0
2018-08-26 01:13:59.633 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Sun Aug 19 21:08:31 PDT 2018 8.0
2018-08-26 01:13:59.703 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Sun Aug 19 15:11:37 PDT 2018 14.0
2018-08-26 01:13:59.769 26943-26943/app.carpecoin I/System.out: QUALITY_SCORE: Sun Aug 19 03:35:52 PDT 2018 200.0

綜合指數設置

我在測試各種組合時設置了2個復合索引,以便獲得在特定時間戳后按qualityScore排序返回的數據的結果。 在此處輸入圖片說明

您調用.orderBy(TIMESTAMP, DESCENDING).orderBy(QUALITY_SCORE, DESCENDING) 這意味着文檔首先按降序時間戳排序,而當文檔相同時,按降序質量分數排序。

由於結果集中的所有文檔都有唯一的時間戳,因此這是唯一可見的結果。 只有當多個文檔的第一個排序字段的值相同時,第二個排序字段才有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM