【问题标题】:Firestore, how to merge indexes for queries containing multiple range comparisons?Firestore,如何合并包含多个范围比较的查询的索引?
【发布时间】:2019-05-14 13:32:01
【问题描述】:

所以我收藏的每个文档都包含五个不同的字段,即:

mode
type
category
price
size

我希望我的用户根据上述字段过滤文档,即用户可以通过尝试任何可能的字段组合来过滤结果,例如:

mode=='foo' && type=='bar'
category=='foo' && price>500 && size<50
category=='bar'
...
...

这将导致许多不同的组合可能性。但是对于字段modecategorytype,用户只能根据 相等条件 进行过滤,而对于字段pricesize,他可以使用范围比较.

此外,我还需要orderBy 以升序和降序方式使用价格的功能。

因此,对于应用的任何过滤器,firebase 查询最多可以包含 3 个相等子句和最多 2 个范围比较。这显然意味着我必须在 firebase 控制台中创建适当的复合索引才能以这种方式查询数据。

一些查询示例如下:

db.where('price', '<', 2000).where('category','==','foo').where('size', '>', 80).orderBy('price', 'desc')
db.where('price', '>', 300).where('type','==','foo').where('mode', '==', 'bar').where('category', '==', 'foo').where('size', '<', 500).orderBy('price')

由于这会导致大量索引占用存储空间,因此 Firebase 文档建议 merging indexes for equality clauses

现在我的问题是为上述情况创建复合索引的最佳方法是什么,以便我可以使用上述任何组合查询数据,同时通过利用firestore的“合并技术”。

我想知道我是否必须创建类似这样的索引:

mode (ascending), price (ascending)
type (ascending), price (ascending)
category (ascending), price (ascending)
size (ascending), price (ascending)

mode (ascending), price (descending)
type (ascending), price (descending)
category (ascending), price (descending)
size (ascending), price (descending)

这里的问题是测试,因为为创建的索引测试每个可能的查询非常忙碌。 因此,我一直在寻找能够保证上述所有可能查询集的索引组合。

【问题讨论】:

    标签: firebase react-native indexing filter google-cloud-firestore


    【解决方案1】:

    Firestore 查询只能包含单个范围子句,因此这种类型的查询是不可能的:

    category=='foo' && price>500 && size<50
    

    【讨论】:

    • 即使在创建索引之后?
    猜你喜欢
    • 2019-11-06
    • 2021-09-01
    • 1970-01-01
    • 2011-08-21
    • 2022-12-12
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    相关资源
    最近更新 更多