Skip to content

Commit 55f745f

Browse files
committed
city_neighbors: allow setting count #31
1 parent 5a35bcd commit 55f745f

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

example/vectorsearch-cities/__main__.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,33 @@ def do_ls(self, name: str = ""):
4545
query = qb.build()
4646
list_cities(query.find())
4747

48-
def do_city_neighbors(self, city: str):
49-
"""find five next neighbors to city <name>\nusage: city_neighbors <name>"""
50-
qb = self._box.query()
51-
qb.equals_string(self._name_prop, city)
52-
query = qb.build()
53-
cities = query.find()
54-
if len(cities) == 1:
55-
location = cities[0].location
48+
def do_city_neighbors(self, args: str):
49+
"""find <count> (default: 5) next neighbors to city <name>\nusage: city_neighbors <name>, [,<count>]"""
50+
try:
51+
args = args.split(',')
52+
if len(args) > 2:
53+
raise ValueError()
54+
city = args[0]
55+
if len(city) == 0:
56+
raise ValueError()
57+
count = 5
58+
if len(args) == 2:
59+
count = int(args[1])
5660
qb = self._box.query()
57-
qb.nearest_neighbors_f32(self._location_prop, location, 6)
58-
qb.not_equals_string(self._name_prop, city)
59-
neighbors = qb.build().find_with_scores()
60-
list_cities_with_scores(neighbors)
61-
else:
62-
print(f"no city found named '{city}'")
61+
qb.equals_string(self._name_prop, city)
62+
query = qb.build()
63+
cities = query.find()
64+
if len(cities) == 1:
65+
location = cities[0].location
66+
qb = self._box.query()
67+
qb.nearest_neighbors_f32(self._location_prop, location, count+1) # +1 for the city
68+
qb.not_equals_string(self._name_prop, city)
69+
neighbors = qb.build().find_with_scores()
70+
list_cities_with_scores(neighbors)
71+
else:
72+
print(f"no city found named '{city}'")
73+
except ValueError:
74+
print("usage: city_neighbors <name>[,<count>]")
6375

6476
def do_neighbors(self, args):
6577
"""find <count> neighbors to geo-coord <lat> <long>.\nusage: neighbors <count>,<latitude>,<longitude>"""

0 commit comments

Comments
 (0)