Skip to content

Commit 7917865

Browse files
committed
example: vectorsearch-capital #31
1 parent 9e1d31f commit 7917865

5 files changed

Lines changed: 383 additions & 0 deletions

File tree

example/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ The following examples are available from this repository.
44

55
## Application Example: Tasks
66

7+
This is our classic Tasks application using a CLI.
8+
79
```
810
cd example
911
python -m tasks
@@ -28,3 +30,47 @@ Welcome to the ObjectBox tasks-list app example. Type help or ? for a list of co
2830
> exit
2931
```
3032

33+
## Vector-Search Example: Capitals
34+
35+
This example application starts with a pre-defined set of capitals and their geo coordinates.
36+
It allows to search for nearest neighbors by capital (`capital_neighbors`) or by coordinates (`neighbors`) as well as adding more locations (`add`).
37+
38+
```
39+
python -m vectorsearch-capitals
40+
Welcome to the ObjectBox vectorsearch-capitals example. Type help or ? for a list of commands.
41+
> ls
42+
ID Name Latitude Longitude
43+
1 Abuja 9.08 7.40
44+
2 Accra 5.60 -0.19
45+
[..]
46+
212 Yerevan 40.19 44.52
47+
213 Zagreb 45.81 15.98
48+
> ls Ber
49+
ID Name Latitude Longitude
50+
28 Berlin 52.52 13.40
51+
29 Bern 46.95 7.45
52+
> city_neighbors Berlin
53+
> city_neighbors Berlin
54+
ID Name Latitude Longitude Score
55+
147 Prague 50.08 14.44 7.04
56+
49 Copenhagen 55.68 12.57 10.66
57+
200 Vienna 48.21 16.37 27.41
58+
34 Bratislava 48.15 17.11 32.82
59+
89 Ljubljana 46.06 14.51 42.98
60+
> neighbors 6,52.52,13.405
61+
ID Name Latitude Longitude Score
62+
28 Berlin 52.52 13.40 0.00
63+
147 Prague 50.08 14.44 7.04
64+
49 Copenhagen 55.68 12.57 10.66
65+
200 Vienna 48.21 16.37 27.41
66+
34 Bratislava 48.15 17.11 32.82
67+
89 Ljubljana 46.06 14.51 42.98
68+
> add Area51, 37.23, -115.81
69+
> city_neighbors Area51
70+
ID Name Latitude Longitude Score
71+
107 Mexico City 19.43 -99.13 594.86
72+
27 Belmopan 17.25 -88.76 1130.92
73+
64 Guatemala City 14.63 -90.51 1150.79
74+
164 San Salvador 13.69 -89.22 1261.12
75+
67 Havana 23.11 -82.37 1317.73
76+
```

example/vectorsearch-capitals/__init__.py

Whitespace-only changes.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from cmd import Cmd
2+
import objectbox
3+
import time
4+
from .model import *
5+
import csv
6+
import os
7+
8+
def list_cities(cities):
9+
print("{:3s} {:25s} {:>9s} {:>9s}".format("ID", "Name", "Latitude", "Longitude"))
10+
for city in cities:
11+
print("{:3d} {:25s} {:>9.2f} {:>9.2f}".format(
12+
city.id, city.name, city.location[0], city.location[1]))
13+
14+
def list_cities_with_scores(city_score_tuples):
15+
print("{:3s} {:25s} {:>9s} {:>9s} {:>5s}".format("ID", "Name", "Latitude", "Longitude", "Score"))
16+
for (city,score) in city_score_tuples:
17+
print("{:3d} {:25s} {:>9.2f} {:>9.2f} {:>5.2f}".format(
18+
city.id, city.name, city.location[0], city.location[1], score))
19+
20+
class VectorSearchCitysCmd(Cmd):
21+
prompt = "> "
22+
def __init__(self, *args):
23+
Cmd.__init__(self, *args)
24+
dbdir = "cities-db"
25+
new_db = not os.path.exists(dbdir)
26+
self._ob = objectbox.Builder().model(get_objectbox_model()).directory(dbdir).build()
27+
self._box = objectbox.Box(self._ob, City)
28+
self._name_prop: Property = City.get_property("name")
29+
self._location_prop: Property = City.get_property("location")
30+
if new_db:
31+
with open(os.path.join(os.path.dirname(__file__), 'cities.csv')) as f:
32+
r = csv.reader(f)
33+
cities = []
34+
for row in r:
35+
city = City()
36+
city.name = row[0]
37+
city.location = [ row[1], row[2] ]
38+
cities.append(city)
39+
self._box.put(*cities)
40+
41+
def do_ls(self, name: str = ""):
42+
"""list all cities or starting with <prefix>\nusage: ls [<prefix>]"""
43+
qb = self._box.query()
44+
qb.starts_with_string(self._name_prop, name)
45+
query = qb.build()
46+
list_cities(query.find())
47+
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
56+
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}'")
63+
64+
def do_neighbors(self, args):
65+
"""find <count> neighbors to geo-coord <lat> <long>.\nusage: neighbors <count>,<latitude>,<longitude>"""
66+
try:
67+
args = args.split(',')
68+
if len(args) != 3:
69+
raise ValueError()
70+
count = int(args[0])
71+
geocoord = [ float(args[1]), float(args[2]) ]
72+
qb = self._box.query()
73+
qb.nearest_neighbors_f32(self._location_prop, geocoord, count)
74+
neighbors = qb.build().find_with_scores()
75+
list_cities_with_scores(neighbors)
76+
except ValueError:
77+
print("usage: neighbors <count>,<latitude>,<longitude>")
78+
79+
def do_add(self, args: str):
80+
"""add new location\nusage: add <name>,<lat>,<long>"""
81+
try:
82+
args = args.split(',')
83+
if len(args) != 3:
84+
raise ValueError()
85+
name = str(args[0])
86+
lat = float(args[1])
87+
long = float(args[2])
88+
city = City()
89+
city.name = name
90+
city.location = [lat,long]
91+
self._box.put(city)
92+
except ValueError:
93+
print("usage: add <name>,<latitude>,<longitude>")
94+
95+
def do_exit(self, _):
96+
"""close the program"""
97+
raise SystemExit()
98+
99+
100+
if __name__ == '__main__':
101+
app = VectorSearchCitysCmd()
102+
app.cmdloop('Welcome to the ObjectBox vectorsearch-cities example. Type help or ? for a list of commands.')
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
Abuja, 9.0765, 7.3986
2+
Accra, 5.6037, -0.1870
3+
Addis Ababa, 9.0084, 38.7813
4+
Algiers, 36.7529, 3.0420
5+
Amman, 31.9632, 35.9306
6+
Amsterdam, 52.3667, 4.8945
7+
Ankara, 39.9334, 32.8597
8+
Antananarivo, -18.8792, 47.5079
9+
Apia, -13.8330, -171.7667
10+
Ashgabat, 37.9601, 58.3261
11+
Asmara, 15.3229, 38.9251
12+
Astana, 51.1796, 71.4475
13+
Asunción, -25.2637, -57.5759
14+
Athens, 37.9795, 23.7162
15+
Avarua, -21.2079, -159.7750
16+
Baghdad, 33.3152, 44.3661
17+
Baku, 40.4093, 49.8671
18+
Bamako, 12.6530, -7.9864
19+
Bandar Seri Begawan, 4.9031, 114.9398
20+
Bangkok, 13.7563, 100.5018
21+
Bangui, 4.3947, 18.5582
22+
Banjul, 13.4549, -16.5790
23+
Basseterre, 17.3026, -62.7177
24+
Beijing, 39.9042, 116.4074
25+
Beirut, 33.8889, 35.4944
26+
Belgrade, 44.7866, 20.4489
27+
Belmopan, 17.2510, -88.7590
28+
Berlin, 52.5200, 13.4050
29+
Bern, 46.9480, 7.4474
30+
Bishkek, 42.8746, 74.5698
31+
Bissau, 11.8636, -15.5842
32+
Bogotá, 4.7109, -74.0721
33+
Brasília, -15.8267, -47.9218
34+
Bratislava, 48.1486, 17.1077
35+
Brazzaville, -4.2634, 15.2429
36+
Bridgetown, 13.1132, -59.5988
37+
Brussels, 50.8503, 4.3517
38+
Bucharest, 44.4268, 26.1025
39+
Budapest, 47.4979, 19.0402
40+
Buenos Aires, -34.6037, -58.3816
41+
Bujumbura, -3.3818, 29.3622
42+
Cairo, 30.0444, 31.2357
43+
Canberra, -35.2809, 149.1300
44+
Caracas, 10.4806, -66.9036
45+
Castries, 14.0101, -60.9874
46+
Chisinau, 47.0105, 28.8638
47+
Colombo, 6.9271, 79.8612
48+
Conakry, 9.6412, -13.5784
49+
Copenhagen, 55.6761, 12.5683
50+
Dakar, 14.7167, -17.4677
51+
Damascus, 33.5131, 36.2919
52+
Dhaka, 23.8103, 90.4125
53+
Dili, -8.5569, 125.5603
54+
Djibouti, 11.5890, 43.1456
55+
Dodoma, -6.1748, 35.7469
56+
Doha, 25.2854, 51.5310
57+
Dublin, 53.3498, -6.2603
58+
Dushanbe, 38.5868, 68.7841
59+
Freetown, 8.4840, -13.2299
60+
Funafuti, -8.5210, 179.1962
61+
Gaborone, -24.6282, 25.9231
62+
Georgetown, 6.8013, -58.1550
63+
Gibraltar, 36.1408, -5.3536
64+
Guatemala City, 14.6349, -90.5069
65+
Hanoi, 21.0278, 105.8342
66+
Harare, -17.8252, 31.0335
67+
Havana, 23.1136, -82.3666
68+
Helsinki, 60.1699, 24.9384
69+
Honiara, -9.4376, 159.9720
70+
Islamabad, 33.6844, 73.0479
71+
Jakarta, -6.2088, 106.8456
72+
Juba, 4.8594, 31.5713
73+
Kabul, 34.5553, 69.2075
74+
Kampala, 0.3476, 32.5825
75+
Kathmandu, 27.7172, 85.3240
76+
Khartoum, 15.5007, 32.5599
77+
Kiev, 50.4501, 30.5234
78+
Kigali, -1.9441, 30.0619
79+
Kingston, 17.9710, -76.7924
80+
Kingstown, 13.1467, -61.2121
81+
Kinshasa, -4.4419, 15.2663
82+
Kuala Lumpur, 3.1390, 101.6869
83+
Kuwait City, 29.3759, 47.9774
84+
La Paz, -16.4897, -68.1193
85+
Libreville, 0.4162, 9.4673
86+
Lilongwe, -13.9626, 33.7741
87+
Lima, -12.0464, -77.0428
88+
Lisbon, 38.7223, -9.1393
89+
Ljubljana, 46.0569, 14.5058
90+
Lomé, 6.1319, 1.2228
91+
London, 51.5072, -0.1276
92+
Luanda, -8.8399, 13.2894
93+
Lusaka, -15.3875, 28.3228
94+
Luxembourg City, 49.6116, 6.1319
95+
Madrid, 40.4168, -3.7038
96+
Majuro, 7.1164, 171.1859
97+
Malabo, 3.7508, 8.7839
98+
Male, 4.1755, 73.5093
99+
Mamoudzou, -12.7871, 45.2750
100+
Managua, 12.1364, -86.2514
101+
Manama, 26.2285, 50.5860
102+
Manila, 14.5995, 120.9842
103+
Maputo, -25.8918, 32.6051
104+
Maseru, -29.2976, 27.4854
105+
Mbabane, -26.3054, 31.1367
106+
Melekeok, 7.4874, 134.6265
107+
Mexico City, 19.4326, -99.1332
108+
Minsk, 53.9045, 27.5615
109+
Mogadishu, 2.0469, 45.3182
110+
Monaco, 43.7325, 7.4189
111+
Monrovia, 6.3005, -10.7974
112+
Montevideo, -34.9011, -56.1645
113+
Moroni, -11.7022, 43.2551
114+
Moscow, 55.7558, 37.6173
115+
Muscat, 23.5859, 58.4059
116+
Nairobi, -1.2921, 36.8219
117+
Nassau, 25.0478, -77.3554
118+
Naypyidaw, 19.7633, 96.0785
119+
New Delhi, 28.6139, 77.2090
120+
Ngerulmud, 7.5004, 134.6249
121+
Niamey, 13.5122, 2.1254
122+
Nicosia, 35.1725, 33.365
123+
Nicosia Northern Cyprus, 35.19, 33.363611
124+
Nouakchott, 18.0735, -15.9582
125+
Nuku'alofa, -21.1393, -175.2049
126+
Nuuk, 64.1836, -51.7214
127+
Oranjestad, 12.5092, -70.0086
128+
Oslo, 59.9139, 10.7522
129+
Ottawa, 45.4215, -75.6972
130+
Ouagadougou, 12.3714, -1.5197
131+
Pago Pago, -14.2794, -170.7004
132+
Palikir, 6.9248, 158.1614
133+
Panama City, 8.9824, -79.5199
134+
Papeete, -17.5350, -149.5699
135+
Paramaribo, 5.8520, -55.2038
136+
Paris, 48.8566, 2.3522
137+
Philipsburg, 18.0255, -63.0450
138+
Phnom Penh, 11.5564, 104.9282
139+
Plymouth, 16.7056, -62.2126
140+
Podgorica, 42.4304, 19.2594
141+
Port Louis, -20.1619, 57.4989
142+
Port Moresby, -9.4438, 147.1803
143+
Port Vila, -17.7416, 168.3213
144+
Port-au-Prince, 18.5944, -72.3074
145+
Port of Spain, 10.6596, -61.4789
146+
Porto-Novo, 6.4968, 2.6283
147+
Prague, 50.0755, 14.4378
148+
Praia, 14.9195, -23.5087
149+
Pretoria, -25.7463, 28.1876
150+
Pristina, 42.6629, 21.1655
151+
Pyongyang, 39.0392, 125.7625
152+
Quito, -0.1807, -78.4678
153+
Rabat, 33.9693, -6.9275
154+
Reykjavik, 64.1466, -21.9426
155+
Riga, 56.9496, 24.1052
156+
Riyadh, 24.7136, 46.6753
157+
Road Town, 18.4207, -64.6399
158+
Rome, 41.9028, 12.4964
159+
Roseau, 15.3092, -61.3794
160+
Saipan, 15.1833, 145.7500
161+
San José, 9.9281, -84.0907
162+
San Juan, 18.4655, -66.1057
163+
San Marino, 43.9424, 12.4578
164+
San Salvador, 13.6929, -89.2182
165+
Sana'a, 15.3694, 44.1910
166+
Santiago, -33.4489, -70.6693
167+
Santo Domingo, 18.4861, -69.9312
168+
Sarajevo, 43.8564, 18.4131
169+
Seoul, 37.5665, 126.9780
170+
Singapore, 1.3521, 103.8198
171+
Skopje, 41.9973, 21.4279
172+
Sofia, 42.6975, 23.3241
173+
Sri Jayawardenepura Kotte, 6.8928, 79.9277
174+
St. George's, 12.0561, -61.7485
175+
St. Helier, 49.1839, -2.1064
176+
St. John's, 17.1171, -61.8456
177+
St. Peter Port, 49.4599, -2.5352
178+
Stanley, -51.7020, -57.8517
179+
Stockholm, 59.3293, 18.0686
180+
Sucre, -19.0421, -65.2559
181+
Sukhumi, 43.0004, 41.0234
182+
Suva, -18.1416, 178.4419
183+
Taipei, 25.0330, 121.5654
184+
Tallinn, 59.4370, 24.7536
185+
Tarawa, 1.4170, 173.0000
186+
Tashkent, 41.2995, 69.2401
187+
Tbilisi, 41.7151, 44.8271
188+
Tegucigalpa, 14.0818, -87.2068
189+
Tehran, 35.6892, 51.3890
190+
Thimphu, 27.4728, 89.6390
191+
Tirana, 41.3275, 19.8187
192+
Tokyo, 35.6762, 139.6503
193+
Tripoli, 32.8867, 13.1910
194+
Tunis, 36.8065, 10.1815
195+
Ulaanbaatar, 47.8864, 106.9057
196+
Vaduz, 47.1410, 9.5215
197+
Valletta, 35.9042, 14.5189
198+
Vatican City, 41.9029, 12.4534
199+
Victoria, -4.6182, 55.4515
200+
Vienna, 48.2082, 16.3738
201+
Vientiane, 17.9757, 102.6331
202+
Vilnius, 54.6872, 25.2797
203+
Warsaw, 52.2297, 21.0122
204+
Washington D.C., 38.9072, -77.0369
205+
Wellington, -41.2865, 174.7762
206+
West Island, -12.1880, 96.8292
207+
Willemstad, 12.1091, -68.9319
208+
Windhoek, -22.5749, 17.0805
209+
Yamoussoukro, 6.8276, -5.2893
210+
Yaoundé, 3.8480, 11.5021
211+
Yaren, -0.5467, 166.9209
212+
Yerevan, 40.1872, 44.5152
213+
Zagreb, 45.8150, 15.9819
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from objectbox.model import *
2+
from objectbox.model.properties import *
3+
import objectbox
4+
import numpy as np
5+
6+
7+
@Entity(id=1, uid=1)
8+
class City:
9+
id = Id(id=1, uid=1001)
10+
name = Property(str, id=2, uid=1002)
11+
location = Property(np.ndarray, type=PropertyType.floatVector, id=3, uid=1003, index=HnswIndex(
12+
id=3, uid=10001,
13+
dimensions=2,
14+
distance_type=HnswDistanceType.EUCLIDEAN
15+
))
16+
17+
def get_objectbox_model():
18+
m = Model()
19+
m.entity(City, last_property_id=IdUid(3, 1003))
20+
m.last_entity_id = IdUid(1, 1)
21+
m.last_index_id = IdUid(3,10001)
22+
return m

0 commit comments

Comments
 (0)