【问题标题】:Firestore query to sort posts by time and scoreFirestore 查询按时间和分数对帖子进行排序
【发布时间】:2018-07-07 03:47:26
【问题描述】:

我正在尝试通过使用以下代码按时间和业力对热门帖子进行排序来对热门帖子进行排序:

FirebaseFirestore.getInstance().collection("topics")
    .orderBy("time", com.google.firebase.firestore.Query.Direction.DESCENDING)
    .whereGreaterThanOrEqualTo("time",(System.currentTimeMillis() % 1000)-1000*60*60*24*7)
    .orderBy("karma", com.google.firebase.firestore.Query.Direction.DESCENDING).startAfter(currentDoc).limit(10).get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot documentSnapshots) {
                        for (DocumentSnapshot d : documentSnapshots.getDocuments()){
                            Topic topic = new Topic();
                            topic.setId(d.getId());
                            topic.setUsername(d.getString("username"));
                            topic.setCaption(d.getString("caption"));
                            topic.setPic(d.getString("pic"));
                            topic.setTime(d.getLong("time"));
                            topic.setType(d.getString("type"));
                            topic.setImage(d.getString("image"));
                            topics.add(topic);
                            if (topics.size()==documentSnapshots.size()){
                                currentDoc = d;
                                adapter.updateList(topics);
                                loading.setVisibility(View.GONE);
                            }
                        }

                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                e.printStackTrace();
            }
        });

我在 Firestore 中添加了索引。但它不起作用。

我的意思是它显示 7 天前的帖子。但它不是按业力排序的。它显示按时间排序的 7 天前的帖子。如果我从查询中删除 orderBy("time") 。崩溃了。

需要帮助:(

【问题讨论】:

  • 如果发生崩溃,总会有崩溃报告。您可以分享日志以供其他人了解。

标签: java android firebase google-cloud-firestore


【解决方案1】:

查询词的顺序很重要。要解决这个问题,请像这样更改顺序:

FirebaseFirestore.getInstance().collection("topics")
    .orderBy("karma", com.google.firebase.firestore.Query.Direction.DESCENDING)
    .orderBy("time", com.google.firebase.firestore.Query.Direction.DESCENDING)
    .startAfter(currentDoc).limit(10).get().addOnSuccessListener(/* ... */);

只要您使用 index,这将起作用。

如您所见,我从查询中删除了以下代码行:

.whereGreaterThanOrEqualTo("time",(System.currentTimeMillis() % 1000)-1000*60*60*24*7)

因为根据有关Order and limit data with Cloud Firestore 的官方文档,您不能:

范围过滤器和不同字段上的一阶排序。

另一种方法是使用:

FirebaseFirestore.getInstance().collection("topics")
    .orderBy("time", com.google.firebase.firestore.Query.Direction.DESCENDING)
    .whereGreaterThanOrEqualTo("time",(System.currentTimeMillis() % 1000)-1000*60*60*24*7)
    .startAfter(currentDoc).limit(10).get().addOnSuccessListener(/* ... */);

如您所见,orderBywhereGreaterThanOrEqualTo 都用于同一个属性。

【讨论】:

  • 怀疑这不起作用。我尝试了以下操作:return FirestoreCollections.contentCollection .collection(ALL_COLLECTION) .orderBy(QUALITY_SCORE, DESCENDING) .orderBy(TIMESTAMP, DESCENDING) .whereGreaterThanOrEqualTo(TIMESTAMP, getTimeframe(WEEK))。错误:查询无效。您在字段“timestamp”上有一个不等式过滤器(whereLessThan()、whereGreaterThan() 等),因此您还必须将“timestamp”作为您的第一个 orderBy() 字段,但您的第一个 orderBy() 目前在字段上'qualityScore' 代替。
  • @AdamHurwitz 感谢您对我的回答发表评论。你是对的。当我回答这个问题时,我错过了你在评论中提到的事实。刚刚更新了我的答案。
  • @AdamHurwitz 感谢您尝试纠正问题,并希望您通过删除反对票来重新考虑我的答案,如果您认为我的更新答案有帮助,请投赞成票。我会很感激的。谢谢!
  • @AdamHurwitz 嗨亚当!你看到我更新的答案了吗?
  • 不幸的是,我的解决方案被标记为重复。但是,您可以查看我在此处概述的解决方案:stackoverflow.com/questions/51859652/…
猜你喜欢
  • 1970-01-01
  • 2017-08-10
  • 2012-09-19
  • 2014-08-15
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多