有没有一种方法可以将GeoFireStore查询与普通的Firestore查询结合在一起? [英] Is there a way I can combine a GeoFireStore query with a normal Firestore query?

查看:107
本文介绍了有没有一种方法可以将GeoFireStore查询与普通的Firestore查询结合在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查询存储在名为用户"的集合中的用户.每个用户都有诸如年龄和体重之类的字段.有没有一种方法可以按与使用GeoFire运行搜索的用户之间的距离来查询用户,并将其与FireStore搜索查询结合起来?例如,我是一位用户,正在寻找半径20英里以内的人,但还想找到比他们小2岁或更老的人.

I want to query users which I have stored in a collection named "Users". Each user has fields such as age and weight. Is there a way I can query the users by distance from the user running the search with GeoFire and combine it with FireStore search queries? For instance, I am a user who is looking for someone who is in a 20 mile radius but also wants to find someone who is between 2 years younger or older than them.

推荐答案

Firestore数据库可以对多个字段进行过滤,但是只能对其中一个字段进行范围过滤.因此此查询是可能的:

The Firestore database can filter on multiple fields, but it can only do a range filter on one of those fields. So this query is possible:

collection.where("age", ">", 18).where("age", "<", 20)

但这是不可能的:

// !!!THIS WON'T WORK!!!
collection.where("latitude", ">", 18.001).where("longitude", "<", 21.312)

我所知道的GeoFirestore库都使用geohash(在原始GeoFire库中使用)来完成在基础数据库上似乎不可能完成的工作:基于纬度过滤地理范围内的文档和经度.

The GeoFirestore libraries that I know of, all use geohashes (as used in the original GeoFire libraries) to be able to do something that is seemingly impossible on the underlying database: filtering documents in a geographic range based on latitude and longitude.

Geohashes通过将位置的纬度和经度组合到单个字符串值中来发挥其魔力,该字符串值可以以不同的精度进行过滤.这是可能的,因为您可以(某种程度上)表达一个经度与纬度之间的关系.因此,您需要将lat和lon的数字压缩合并为一个值,该值具有每个数字的最高有效位.

Geohashes work their magic by combining the latitude and longitude of a location into a single string value, that can be filtered with varying degrees of accuracy. This is possible, since you can (sort of) express how one degree of longitude related to a degree of latitude. So you sort of zip-merge the digits of lat and lon into a single value, which has the most significant digit of each first.

现在,这也解释了为什么您不能简单地向地理查询中添加另一个范围过滤器.您将必须找到一种方法来表达距离上的年龄差异.如果可以做到,那么将多余的信息编码为geo-age-hash就可以了.对我来说,这听起来很复杂,但至少有可能.

Now this also explains why you can't simply add another range filter to the geoquery. You'd have to find a way to express the age difference in terms of distance. If you can do that, it's "just" a matter of encoding the extra information into the geo-age-hash. It sounds quite complex to me, but it is at least possible.

第一个问题是,虽然没有通用的方式将其他属性与纬度和经度相关联,这就是为什么您要独自解决这个问题的原因.我的直觉是,大多数开发人员都放弃了这一要求,或者进行了额外的客户端排序/过滤.

The first problem is though that there is no generic way to relate additional properties to latitude and longitude, which is why you'll be on your own to solve this problem. My gut feeling is that most developers give up on this requirement, or do the additional ordering/filtering client-side.

如果您想向geohash添加一个相等过滤器(例如,某个地理范围内的人的文档正好是18),那么在Firestore级别上绝对可以实现,尽管我不知道GeoFirestore库是否公开底层的查询功能可以做到这一点.

If you want to add an equality filter to a geohash (e.g. documents for people in a certain geographic range who are exactly 18), that is definitely possible on a Firestore level, although I don't know if the GeoFirestore libraries expose the underlying query capabilities to do that.

我强烈建议您学习更多有关该主题的知识,因为它非常有趣.一些来源:

I highly recommend learning more on the subject, as it is quite fascinating. Some sources:

  • If you want to learn more on why Firestore can only range filter on a single field, watch this video from the Get to know Cloud Firestore series.
  • If you want to learn more about geohashes, geoqueries, and how you can implement them on Firestore watch my talk about the topic.
  • The wikipedia explanation of geohashes.

我还建议您查看有关该主题的先前问题:

I also recommend checking out these previous questions on the topic:

  • Filtering results with Geofire + Firebase
  • How to do complex queries with geofire?
  • Can I use firebase's GeoFire with Priority in a single query?

其中许多是关于(原始)Firebase实时数据库的GeoFire的,但是相同的原理也适用于Firestore.

Many of these are about GeoFire for the (original) Firebase Realtime Database, but the same principles would apply to Firestore.

这篇关于有没有一种方法可以将GeoFireStore查询与普通的Firestore查询结合在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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