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

查看:116
本文介绍了从最接近的订购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的最后四位数字/字母截断,但这还不够具体.要获取最近的帖子,最好将数据库中的geoHashes设置为我当前的状态,然后在检索内容时,将起始值设为查询的最特定的geohash,然后将end值设为截断版本的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?

从理论上讲,如果有一种方法可以将地质哈希存储为整数,那么我可以使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使用geohashhes能够选择在geohash范围内的文档,该范围与地图上的区域大致相同.仅使用地理查询就无法从该区域内以指定顺序检索那些文档.如果要按距离对文档进行排序,则必须在地理查询之后,在应用程序代码中执行此操作.

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.

如果您想详细了解原因,请看我前一段时间的演讲:

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天全站免登陆