问题描述
由于firebase不支持任何空间索引,因此我使用geohash,有人在这里建议.Geohash正在使用角色来寻找附近的人.
Since firebase not support any spatial indexing then I use geohash which someone advice here.Geohash is using characters to find the nearby.
所以可以说我有 w22qt4vhye1c
w99qt4vdf4vc
w22qt4vhzerct
fireh中的geohash存储,我只想查询哪个接近 w22qt4的geohash
.
So let say I have w22qt4vhye1c
w99qt4vdf4vc
w22qt4vhzerct
geohash store in firebase and I want to query only which geohash close to w22qt4
.
该怎么做?
我知道Firebase具有startAt.我尝试了但没用.
I know firebase has startAt. I tried but not work.
更新
将geohash存储到Firebase
Storing geohash to firebase
//Encode lat lng, result w22qt4vhye1c3
var geoHashLatLng = encodeGeoHash(lat,lng);
firebaseRef.child('/geohash/' + geoHashLatLng).set(id);
所以在json
root
- geohash
- w22qt4vhye1c3
- id
- z99qt4vdf4vc
- w22qt4vhzerct
我的问题在这里.我只想查询从字符 w22qt4
开始的geohash.我们可以在firebase中做到这一点吗?
My question here.I Just want to query geohash which start from characters w22qt4
. Can we do that in firebase?
更新2 startAt()
似乎不以字符开头的查询...
UPDATE 2startAt()
seems like not query with characters start with ...
示例:我有下面的gehash
Example: I have following gehash
geohash节点
geohash
- a123
- a333
- b123
- c123
- d123
使用以下代码
var firebaseRef = new Firebase('https://test.firebaseio.com');
var query = firebaseRef.child('/geohash').startAt(null, 'a');
query.on('child_added', function(snapshot) {
console.log(snapshot.name());
});
将获得此结果
startAt(null, 'a') //return a123, a333, b123, c123, d123
startAt(null, 'c123') //return c123, d123
startAt(null, 'd') //return d123
我的预期结果
startAt(null, 'a') //return a123, a333
startAt(null, 'c123') //return c123
startAt(null, 'd') //return d123
我的猜测是,startAt()将按顺序查询26个字母,但不匹配.因此,我可以在firebase中做什么,以便可以在上面获得预期的结果?
My guess, startAt() will query the 26 alphabet letters in sequence but not matching.So, what can I do in firebase so I can get my expected results above?
推荐答案
除了上述安德鲁(Andrew)的答案外,您还想做什么来获得带有地理哈希值的预期效果(即能够执行前缀查询,以便您在中指定精度)例如,查询"(例如,geohash中的每3个字符可让您〜+ -80km)将以更细化和离散的方式存储数据.
In addition to Andrew's answer above, what you want to do to get the desired effect with geohashes (i.e. be able to do prefix queries that let you specify accuracy in the 'query', specifically, for example every 3 characters in the geohash gets you ~ +-80km) is to store the data in a more granular and discretized manner.
因此,与其现在如何存储,不如通过将geohash字符串键切成碎片来保存数据(我已经在3个字符的边界完成了此操作,但是您应该选择适当的标记化为您提供所需准确性的策略),并将每个片段用作子名称,例如:
So, rather than how you're storing it now, you'll want to save the data by chopping the geohash string key into fragments (I've done it at the 3 character boundary, but you should choose an appropriate tokenization strategy that gives you the desired accuracy) and use each fragment as a child name, as such:
root
- geohash
- w22
- qt4
- vhy
- e1c
- w22qt4vhye1c3
- id
- vhz
- erc
- w22qt4vhzerct
- id
- z99
- qt4
- vdf
- z99qt4vdf4vc
- id
然后,如您的问题所述,当您需要获取所有以 w22qt4
开头的地理哈希时,您将针对以下内容进行查询:
Then, as in your question, when you need to get all geohashes that start with w22qt4
you would do a query against:
firebaseRef.child('/geohash/w22/qt4/')
然后会给您 vhy
和 vhz
子级,然后这些子级将具有您感兴趣的所有实际地理哈希.
Which would then give you the vhy
and vhz
children which then have all the actual geohashes that you were interested in.
这篇关于如何在Firebase中查询与字符串前缀匹配的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!