Firestore“无效查询" -我使用索引错误吗? [英] Firestore "Invalid query" - Am I using Indexing wrong?

查看:65
本文介绍了Firestore“无效查询" -我使用索引错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有以下结构的Firebase Cloud Firestore datadabse构建应用程序:

I am building an app with a firebase cloud firestore datadabse with the following structure:

我正在查询用户书籍,其中"StartDate"字段大于0,而"EndDate"等于0,则应按标题对查询进行排序.

I am doing a query of a user´s books where the field "StartDate" is greater than 0 and the "EndDate" is equal to 0, that query should then be ordered by Title.

CollectionReference booksCollection = mFirebaseDatabase.collection("users").document(user.getUid()).collection("books");

Query databaseQuery = booksCollection.whereGreaterThan("StartDate", 0).whereEqualTo("EndDate", 0).orderBy("Title");

databaseQuery.get()

为此,我读到您需要使用索引编制(尽管我没有得到指向错误消息索引的链接,因为它表明我应该这样做).我的索引如下:

To do this i read that you need to use indexing (although i did not get a link to indexing in errormessages as it stated that I should). my indexing looks like this:

但是我收到错误消息:

java.lang.IllegalArgumentException:无效的查询.您在字段'Sta​​rtDate'上有一个不等式的where过滤器(whereLessThan(),whereGreaterThan()等),因此您还必须将'StartDate'作为第一个orderBy()字段,但是您的第一个orderBy()当前在字段上改为标题".

java.lang.IllegalArgumentException: Invalid query. You have an inequality where filter (whereLessThan(), whereGreaterThan(), etc.) on field 'StartDate' and so you must also have 'StartDate' as your first orderBy() field, but your first orderBy() is currently on field 'Title' instead.

推荐答案

Firestore查询只能在单个字段上对范围进行排序或过滤.您正在尝试在StartDate上进行过滤,然后在Title上进行排序,这是不可能的.

Firestore queries can only sort or filter range on a single field. You're trying to filter on StartDate and then order on Title, which isn't possible.

有关查询的Firestore文档包含以下内容:不允许的类似查询示例:

The Firestore documentation on queries contains this similar example of a query that isn't allowed:

无效:范围过滤器和一阶按不同字段

Invalid: Range filter and first orderBy on different fields

citiesRef.whereGreaterThan("population", 100000).orderBy("country");

Firestore仅允许查询能够满足其性能保证的查询,并且不允许(当前)对多个字段进行排序/范围过滤.请参阅Todd的有关Firestore查询的视频 a>了解有关其工作原理的更多信息.

Firestore only allows queries where it can meet its performance guarantees, and for ordering/range-filtering on multiple fields that is not (currently) possible. See Todd's video on Firestore queries to learn more on how this works.

典型的解决方法是让Firestore处理过滤器(在您的情况下为StartDate,然后在Android应用中对结果重新排序.

The typical workaround is to let Firestore handle the filter (on StartDate in your case, and then reorder the results in the Android app.

这篇关于Firestore“无效查询" -我使用索引错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆