首页 > 解决方案 > 使用 geoflutterfire 和 where 执行查询

问题描述

在我的“办公室”集合中,我正在使用 GeoFlutterFire 包尝试查询在 x 年和 y 年之间建造的所有办公室,提供特定服务并且在我办公室的 z 英里范围内。每个“办公室”文档都有一个“位置”地图,其中包含使用 GeoFirePoint 的 geohash 和地理点,以及“建成年份”和“服务”字段。我喜欢进行如下查询并获取文档并显示它们:

var collRef = firestore.collection('offices')
              .where('service', isEqualTo: 'law')
              .where('built', isLessThanOrEqualTo: y)
              .where('built', isGreaterThanOrEqualTo: x);

geo.collection(collectionRef: collRef.reference())
                .within(center: myOffice, radius: z, field: 'position', strictMode: true);

但这不起作用。我收到此查询无效的错误:

Unhandled Exception: PlatformException(invalid_query, FIRInvalidArgumentException, Invalid query. You have a where filter with an inequality (lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) on field 'built' and so you must also use 'built' as your first queryOrderedBy field, but your first queryOrderedBy is currently on field 'position.geohash' instead.)

我知道我们不能对 Firebase 中的多个字段进行不等式处理,因为这就是索引的工作方式,我猜 GeoFlutterFire 会根据 geohash 进行内部排序,但我试图避免客户端查询,以尽可能避免不必要的文档读取,这就是为什么我需要在这里合并所有查询!

如果我在这里进行位置查询,然后在客户端进行构建年份,我最终会阅读很多不必要的文档。同样适用于其他方式(基于构建年份的查询并在客户端处理位置)。有没有解决的办法?

标签: firebasefluttergoogle-cloud-firestoregeolocation

解决方案


推荐阅读