Using Gears, the script will look something like this:
import math
USER_LON = 19
USER_LAT = 47
def CreateTouple(t):
lon = float(t[1].split(’,’)[0])
lat = float(t[1].split(’,’)[1])
docid = t[0]
return (docid, lon, lat)
‘’’
-
Perform the redisearch query
-
Remove the first results which is the number of results
-
Put the docid and the location inside a touple
-
Create a touple of (docid, lon, lat)
-
Create a touple of (distance, docid)
-
Sorting by distance
-
Run the exection
‘’’
GB().\
map(lambda x: execute(‘FT.SEARCH’, ‘idx’, ‘@test:[19,47,100,km]’)).\
map(lambda x: x[1:]).\
flatmap(lambda x: [(x[i - 1], x[i][1]) for i in range(len(x)) if i % 2 == 1]).\
map(CreateTouple).\
map(lambda x: (math.sqrt((x[1] - USER_LON)**2 + (x[2] - USER_LAT)**2), x[0])).\
sort().\
run(‘idx:idx’)
``
And when running it on this data:
127.0.0.1:6379> FT.CREATE idx SCHEMA test GEO
OK
127.0.0.1:6379> FT.ADD idx doc1 1.0 FIELDS test “19.05,47.497”
OK
127.0.0.1:6379> FT.ADD idx doc2 1.0 FIELDS test “19.06,47.498”
OK
127.0.0.1:6379> FT.ADD idx doc3 1.0 FIELDS test “19.07,47.499”
OK
127.0.0.1:6379> FT.ADD idx doc4 1.0 FIELDS test “19.02,47.492”
OK
``
You will get something like this:
python gears.py ClientExample.py
[["(0.4924063362711708, ‘doc4’)", “(0.4995087586819674, ‘doc1’)”, “(0.5016014354046422, ‘doc2’)”, “(0.5038858997828798, ‘doc3’)”], []]
``
You can see that inside the Geas script I use RediSearch query to extract the data, then I calculating the distance from the “user” and then I sorting the results.