为了按距离对地理查询结果进行排序,我必须阅读整个数据集吗? [英] In order to sort geoquery results by distance must I read the entire dataset?

查看:103
本文介绍了为了按距离对地理查询结果进行排序,我必须阅读整个数据集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在Firebase或Firestore中执行地理位置查询,可以使用GeoFire和GeoFirestore之类的库.但是要按距离对那个地理查询的结果进行排序,必须读取整个数据集,对吗?如果地理查询产生大量结果,那么按距离排序时,无法分页这些结果(在后端,而不是对用户)?

To perform geoqueries in Firebase or Firestore, there are libraries like GeoFire and GeoFirestore. But to sort the results of that geoquery by distance, the entire dataset must be read, correct? If a geoquery produces a large number of results, there is no way to paginate those results (on the backend, not to the user) when sorting by distance, is there?

推荐答案

是的,为了按距离排序,您必须读取所有落在Geoquery范围内的结果.

Yes, in order to sort by distance you must read all results that fall into the Geoquery range.

这样做的原因是这样的查询如何工作:它们返回一组在一定的geohash值范围内的文档,该文档的顺序不一定与它们到查询中心的距离相同.

The reason for this is how such queries work: they return a set of documents that are within a range of geohash values, which is not necessarily the same order as by their distance to the center of the query.

这还意味着,无法对按其距离排序的文档列表进行有意义的分页,因为无论如何您都需要读取所有结果.我能想到的最好的办法是在Cloud Functions中实现Geoquery,以便您可以在那里进行排序/过滤,并且仅将结果页面的全部返回返回给客户端.虽然这并不能节省您的费用(因为您仍在读取该范围内的所有文档),但可以节省发送文档给用户的带宽.

This also means that there is no way to do meaningful pagination in a list of documents that are ordered by their distance, since you need to read all results anyway. The best I can think of is implementing the Geoquery in Cloud Functions, so that you can do the sort/filter there, and only return the page-full of results to the client. While this doesn't save on your cost (as you're still reading all documents in the range), it will save bandwidth in sending documents to the user.

要了解有关此类地理查询如何工作的更多信息(这解释了为什么无法按您希望的方式对其进行优化),请查看我的演讲视频这篇文章和Jeff Delaney网站上的简短视频.

To learn more about how such geoqueries work, which explains why they can't be optimized the way you're looking to do, have a look at the video of my talk here or this article+shorter video on Jeff Delaney's site.

这篇关于为了按距离对地理查询结果进行排序,我必须阅读整个数据集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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