I have a table called users in firebase, the table looks like this:
The structure of this table is:
users
|_
uid1
|_
latitude: ...
longitude: ...
name: ...
uid2
|_
latitude: ...
longitude: ...
name: ...
I want to write a Cloud Function called getUserByLocation, the sample request url would be https://someurl/getUserByLocation?latitude=40.45654&longitude=-86.68468, which takes a pair of latitude and longitude as an original point, then go to the users table to find all users within 10 miles then return them back. It doesn't seem very hard and in fact, I have something that works:
- Calculate
latLower,latUpper,longLowerandlongUpperand make a circle like this:
- Then use query
...orderByChild('latitude').startAt(latLower).endAt(latUpper).once('value')...to select users whoselatitudesare in range, I cannot considerlongitudeat this point because apparently I can only order the results by one key - I check all results selected from step 2, if the
longitudeis in range too (betweenlongLowerandlongUpper), then this user should be returned
This approach works for sure, however, I am thinking, what if there are millions of users? This approach is just not efficient. As a matter of fact, I wrote a script to create more than 1 million users and then used this approach to select users, usually, it'll take more than 10 secs to see the results and this is not acceptable.
My question is, is there any way I can improve the search efficiency in this situation? I've googled ElasticSearch but currently have no knowledge of what that is, is that something that can be used, in this case, to improve search?
