【问题标题】:Firestore Composite Index Sort Not Modifying OrderFirestore 复合索引排序不修改顺序
【发布时间】:2019-02-01 12:13:11
【问题描述】:

问题

使用 Firestore 多属性复合索引排序不会更改返回数据的顺序。这与对一个属性执行的原始排序进行比较。目前,返回的数据仅按 timestamp 排序,应用和不应用复合索引。

预期

当使用 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 排序返回的数据的结果。

【问题讨论】:

  • 请编辑您的问题以包含您如何读取查询结果并打印它们的代码。
  • 感谢@FrankvanPuffelen,我已将推荐信息添加到我上面的原始帖子中。

标签: android firebase google-cloud-firestore


【解决方案1】:

您致电.orderBy(TIMESTAMP, DESCENDING).orderBy(QUALITY_SCORE, DESCENDING)。这意味着文档首先按时间戳降序排列,当它们相同时,它们按质量得分降序排列。

由于结果集中的所有文档都有唯一的时间戳,这是唯一可见的结果。只有当多个文档的第一个排序字段的值相同时,第二个排序字段才会变得相关。

【讨论】:

  • 感谢@Frank van Puffelen。我的目标是按QUALITY_SCORE 排序,过滤掉指定TIMESTAMP 之后的所有内容。有没有办法可以命令查询来完成这个?如果没有,我可以先通过TIMESTAMP查询,然后在客户端按qualityScore排序。但是,我需要将排序集合的最终结果(例如 List 或 Array)转换回 Query 以传递给 FirestorePagingOptions.BuildersetQuery() 方法。如果第 1 部分不可能,关于如何将集合转换为查询的任何想法?
  • 那不是return FirestoreCollections.contentCollection .collection(ALL_COLLECTION) .orderBy(QUALITY_SCORE, DESCENDING) .whereGreaterThanOrEqualTo(TIMESTAMP,getTimeframe(WEEK)),所以没有时间戳的排序?
  • 很遗憾,无法进行查询。收到的错误指出:查询无效。您在字段“timestamp”上有一个不等式过滤器(whereLessThan()、whereGreaterThan() 等),因此您还必须将“timestamp”作为您的第一个 orderBy() 字段,但您的第一个 orderBy() 目前在字段上'qualityScore' 代替。
  • 啊...在这种情况下,确实似乎无法更改顺序。像往常一样,这将是一个性能考虑因素:由于时间戳是您过滤的主要标准,因此(显然)需要首先这样做才能保证性能。您必须在客户端重新排序。
  • 您必须为此创建自己的适配器。通常附加一个观察者,将文档填充到List,然后将其传递给ArrayAdapter 我想。
猜你喜欢
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
相关资源
最近更新 更多