如何使用 FirestoreRecyclerAdapter 进行复杂查询?

How make complex query with FirestoreRecyclerAdapter?

我正在开发一个 Android 应用程序,我需要对我的 Firestore 数据库进行复杂查询并创建一个 FirestoreRecyclerAdapter。要创建适配器,我需要一个 FirestoreRecyclerOptions 对象来接收整个查询的输入。阅读文档后,我无法在查询中对不同参数使用方法 whereGreaterThanwhereLessThanoderBy 等。例如,如何从数据库中获取年龄大于 than/less 且体重大于 than/less 的用户?

例如,我的 firestore 数据库中的文档结构是:

Users --->UserID--->userName(String)
            --->userAge(number)
            --->userHeight(number)
            --->userWeight(number)

FirebaseFirestore db;

db = FirebaseFirestore.getInstance();

RecyclerView recyclerView;

recyclerView = (RecyclerView)findViewById(R.id.recyclerViewID);
.
.
.

Query query = db.collection("Users").//the complex query that i need

FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>()
            .setQuery(query, User.class)
            .build();

adapter = new UsersAdapter(options, this);//get in input options and the context

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

编辑:我上一条回答 1 的评论中的一个可能的解决方案

关于 Firestore,有一些 query limitations

Query limitations

Cloud Firestore does not support the following types of queries:

  • Queries with range filters on different fields, as described in the previous section.

因此,您不能在不同属性上使用 whereGreaterThan()whereLessThan() 方法在范围过滤器上查询数据库并将其传递给适配器。

编辑:

此问题的可能解决方案是在客户端过滤您的记录两次。首先使用第一个 属性 查询数据库,然后使用第二个 属性 查询数据库。不幸的是,您无法一次性实现这一目标。

编辑2:

解决方案是使用第一个查询查询数据库,获取相应的元素,使用第二个查询再次查询数据库并获取相应的元素,然后合并结果客户端。现在数据库中的元素被过滤了两次。将这些元素的列表传递给适配器,仅此而已。请注意,当使用此解决方案时,您不能再使用 Firebase-UI 库,但是您可以通过这种方式将您的项目过滤两次。