从最近到最远订购 Firestore GeoHash 查询? [英] Ordering Firestore GeoHash query from closest to furthest?

查看:12
本文介绍了从最近到最远订购 Firestore GeoHash 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用部分 GeoFirebase 库和 Firestore 来进行地理查询.当我设置帖子的 geohash 时,我会这样做 if let geoHash = GFGeoHash(location: location.coordinate).geoHashValue {

Currently, I'm using parts of the GeoFirebase library along with Firestore to allow for geoquerying. When I set the geohash of a post, I do it as such if let geoHash = GFGeoHash(location: location.coordinate).geoHashValue {

但是,为了使 geohash 查询不那么具体,我计划在查询时截断部分 geohash;目前,查询看起来与此类似

However, to make the geohash querying less specific, I'm planning on truncating part of the geohash when I query; currently, the query looks similar to this

 var geoQuerySpecific = GFGeoHashQuery()
        let geoQueryHash =  GFGeoHashQuery.queries(forLocation: (lastLocation?.coordinate)!, radius: (30)) as! Set<GFGeoHashQuery>

        for query in geoQueryHash {
            geoQuerySpecific = query
            print("the key is this um (geoQuerySpecific)")
        }
        print("the starting value is (geoQuerySpecific.startValue)  and the end value is (geoQuerySpecific.endValue)")
        let nearQuery = Firestore.firestore().collection("stuff").order(by: "g").whereField("g", isGreaterThanOrEqualTo: geoQuerySpecific.startValue).whereField("g", isLessThanOrEqualTo: geoQuerySpecific.endValue)

如您所见,这将无法正常工作,因为 geoQueryHash 中有多个项目.当我在firebase中设置geohash时,我考虑过截断geohash的最后四个数字/字母,但是,这还不够具体.要获得最近的帖子,最好像我目前一样在数据库中设置geoHashes,然后,在检索内容时,将起始值设为查询的最具体的geohash,然后将结束值设为geohash 的截断版本,从获取最近的帖子开始,以最广泛的帖子结束?

As you can see, this won't work correctly as there are multiple items in the geoQueryHash. I've thought about truncating the last four digits/letters from the geohash when I am setting it in firebase, however, that won't be specific enough. To get the closest posts, would it be best to set the geoHashes in the database as I currently am, then, when retrieving the stuff, make the start value the most specific geohash for the query and then make the end value the truncated version of the geohash, as to start by getting the closest posts and end with the broadest?

我可以将 Firestore 查询限制为 50,这样我就可以检索从最近到最远的 50 个帖子...我对地理散列的理解是否正确?这可行吗?

I can limit the Firestore query to 50, so then I can retrieve the 50 posts from closest to furthest... is my understand of geo hashing correct? Would this be feasible?

从概念上讲,如果有一种方法可以将 geohash 存储为整数,那么我可以让 firestore 查询从最大整数(即最精确的 Geohash)开始,然后通过降序对查询进行排序,直到它达到最不精确的强度(最广泛的geoHash),然后将其限制为50.

Conceputally, if there were a way to store geohashes as integers than I could make the firestore query start at the largest integer (i.e. most precise Geohash) and then work order the query by descending until it gets to the least precise intenser (broadest geoHash) and then limit it to 50.

推荐答案

GeoFirestore 使用geohashes 能够选择geohashes 范围内的文档,该范围与地图上的区域大致相同.仅通过地理查询无法从该区域内以指定顺序检索这些文档.如果您想按距离对文档进行排序,则必须在地理查询之后的应用程序代码中执行此操作.

GeoFirestore uses geohashes to be able to select documents that are within a range of geohashes, which is roughly the same as an area on the map. There is no way to retrieve those documents within a specified order from within that area with just a geoquery. If you want to have the documents sorted by distance, you will have to do that after the geoquery, in your application code.

如果您想详细了解为什么会这样,请查看我前段时间的演讲:根据地理位置或距离查询 Firebase 和 Firestore.在其中,我解释了 geohashes 的工作原理,它们如何允许您选择特定地理范围内的文档,以及为什么您不能在 Firestore(或 Firebase 的原始实时数据库)上进行更复杂的查询.

If you want to learn more on why that is, have a look at a talk I gave a while ago: Querying Firebase and Firestore based on geographic location or distance. In it I explain how geohashes work, how they allow you to select documents in a certain geographic range, and why you can't do the more complex query on Firestore (or Firebase's original realtime database).

这篇关于从最近到最远订购 Firestore GeoHash 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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