-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_search.py
53 lines (49 loc) · 1.43 KB
/
example_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
es = Elasticsearch()
index = 'pantry_list'
lat = 40.022079
lon = -82.977878
distance = '5mi'
# TODO: There is probably a nicer way to do this via the dsl
raw_query = {
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": distance,
"location": {
"lat": lat,
"lon": lon,
}
}
}
}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": lat,
"lon": lon
},
# "order": "desc",
"order": "asc",
"unit": "mi",
"distance_type": "plane"
}
}
]
}
search = Search.from_dict(raw_query).using(es)
search.doc_type("pantry")
search.index(index)
res = search.execute()
print("Found {} documents matching located around latitude {} longitude {}".format(len(res.hits), lat, lon))
# The closer meta.sort gets to 0 the closer it is to the lat/lon
for resp in res.hits:
print("score: {} name: {} address1: {} located in {}".format(resp.meta.sort[0], resp.loc_name, resp.address1,
resp.city))