Skip to content

Commit 564b3dc

Browse files
Merge branch '106-vector-search-example' into 'main'
Add basic Vector Search example using Cities See merge request objectbox/objectbox-dart!86
2 parents 61a04a7 + aceaa52 commit 564b3dc

File tree

14 files changed

+233
-1
lines changed

14 files changed

+233
-1
lines changed

.gitlab-ci.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,21 @@ analyze:
3434
- dart pub get --directory=generator
3535
- dart pub get --directory=objectbox
3636
- dart pub get --directory=objectbox_test
37+
- dart pub get --directory=objectbox/example/dart-native/vectorsearch_cities
3738
# Generate code
3839
- cd benchmark
3940
- dart run build_runner build
4041
- cd ../objectbox_test
4142
- dart run build_runner build
42-
- cd ..
43+
- cd ../objectbox/example/dart-native/vectorsearch_cities
44+
- dart run build_runner build
45+
- cd ../../../.. # back to repo root
4346
# Analyze Dart packages
4447
- dart analyze benchmark
4548
- dart analyze generator
4649
- dart analyze objectbox
4750
- dart analyze objectbox_test
51+
- dart analyze objectbox/example/dart-native/vectorsearch_cities
4852
# Check formatting of all packages
4953
- dart format --set-exit-if-changed --fix .
5054

@@ -120,3 +124,18 @@ coverage:
120124
artifacts:
121125
paths:
122126
- objectbox/coverage/html/
127+
128+
# For the Dart Native example compiles and runs the executable (analysis and code formatting is
129+
# checked by analyze job).
130+
dart-examples-test:
131+
extends: .cache
132+
stage: test
133+
tags: [ x64, linux, docker ]
134+
image: dart:$DART_VERSION
135+
script:
136+
- cd objectbox/example/dart-native/vectorsearch_cities
137+
- dart pub get
138+
- dart run build_runner build
139+
- ../../../../install.sh
140+
- dart compile exe bin/vectorsearch_cities.dart
141+
- ./bin/vectorsearch_cities.exe

objectbox/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## latest
22

33
* Export `ObjectWithScore` and `IdWithScore` used by the new find with score `Query` methods. [#637]
4+
* Add simple `vectorsearch_cities` Dart Native example application.
45

56
## 4.0.0 (2024-05-15)
67

objectbox/example/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ ObjectBox Examples
33

44
There are several example apps and tutorials available:
55

6+
* [vectorsearch_cities](dart-native/vectorsearch_cities): a Dart Native example app using Vector
7+
Search to find neighbors of cities.
68
* [objectbox_demo_relations](flutter/objectbox_demo_relations): our main Flutter example app showing
79
all basics and how to use relations.
810
* [event_management_tutorial](flutter/event_management_tutorial): an event management Flutter app
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Vector-Search Example: Cities
2+
3+
This is a simple example application to demonstrate the Vector Search feature of ObjectBox.
4+
It searches for nearest neighbors of a city.
5+
6+
## Docs
7+
- [Getting started with ObjectBox](https://docs.objectbox.io/getting-started)
8+
- [ANN Vector Search](https://docs.objectbox.io/ann-vector-search)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:vectorsearch_cities/objectbox.dart';
2+
3+
void main(List<String> arguments) {
4+
final objectBox = ObjectBox.create();
5+
6+
// Retrieve IDs
7+
final resultIds = objectBox.findTwoClosestNeighborsIds();
8+
for (final result in resultIds) {
9+
print("City ID: ${result.id}, distance: ${result.score}");
10+
}
11+
12+
// Retrieve objects
13+
final results = objectBox.findTwoClosestNeighbors();
14+
for (final result in results) {
15+
print("City: ${result.object.name}, distance: ${result.score}");
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:objectbox/objectbox.dart';
2+
3+
@Entity()
4+
class City {
5+
@Id()
6+
int id = 0;
7+
8+
String? name;
9+
10+
@HnswIndex(dimensions: 2)
11+
@Property(type: PropertyType.floatVector)
12+
List<double>? location;
13+
14+
City(this.name, this.location);
15+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"_note1": "KEEP THIS FILE! Check it into a version control system (VCS) like git.",
3+
"_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.",
4+
"_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.",
5+
"entities": [
6+
{
7+
"id": "1:4473667649417503248",
8+
"lastPropertyId": "3:6086893156295683893",
9+
"name": "City",
10+
"properties": [
11+
{
12+
"id": "1:7234981322630746044",
13+
"name": "id",
14+
"type": 6,
15+
"flags": 1
16+
},
17+
{
18+
"id": "2:2260839727248414810",
19+
"name": "name",
20+
"type": 9
21+
},
22+
{
23+
"id": "3:6086893156295683893",
24+
"name": "location",
25+
"type": 28,
26+
"flags": 8,
27+
"indexId": "1:145739237546482940"
28+
}
29+
],
30+
"relations": []
31+
}
32+
],
33+
"lastEntityId": "1:4473667649417503248",
34+
"lastIndexId": "1:145739237546482940",
35+
"lastRelationId": "0:0",
36+
"lastSequenceId": "0:0",
37+
"modelVersion": 5,
38+
"modelVersionParserMinimum": 5,
39+
"retiredEntityUids": [],
40+
"retiredIndexUids": [],
41+
"retiredPropertyUids": [],
42+
"retiredRelationUids": [],
43+
"version": 1
44+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import 'model.dart';
2+
import 'objectbox.g.dart';
3+
4+
/// Provides access to the ObjectBox Store throughout the app.
5+
class ObjectBox {
6+
/// The Store of this app.
7+
late final Store _store;
8+
9+
late final Box<City> _cityBox;
10+
11+
ObjectBox._create(this._store) {
12+
_cityBox = Box<City>(_store);
13+
14+
if (_cityBox.isEmpty()) {
15+
_putCityData();
16+
}
17+
}
18+
19+
/// Create an instance of ObjectBox to use throughout the app.
20+
static ObjectBox create() {
21+
// Note: set macosApplicationGroup for sandboxed macOS applications, see the
22+
// Store documentation for details.
23+
24+
// Store openStore() {...} is defined in the generated objectbox.g.dart
25+
final store = openStore(
26+
directory: "obx-demo-vectorsearch-cities",
27+
macosApplicationGroup: "objectbox.demo");
28+
return ObjectBox._create(store);
29+
}
30+
31+
_putCityData() {
32+
_cityBox.putMany([
33+
City("Barcelona", [41.385063, 2.173404]),
34+
City("Nairobi", [-1.292066, 36.821945]),
35+
City("Salzburg", [47.809490, 13.055010]),
36+
]);
37+
}
38+
39+
Query<City> _queryTwoClosestNeighbors() {
40+
final madrid = [40.416775, -3.703790]; // query vector
41+
// Prepare a Query object to search for the 2 closest neighbors:
42+
final query =
43+
_cityBox.query(City_.location.nearestNeighborsF32(madrid, 2)).build();
44+
45+
// Combine with other conditions as usual
46+
// ignore: unused_local_variable
47+
final queryCombined = _cityBox
48+
.query(City_.location
49+
.nearestNeighborsF32(madrid, 2)
50+
.and(City_.name.startsWith("B")))
51+
.build();
52+
53+
return query;
54+
}
55+
56+
List<IdWithScore> findTwoClosestNeighborsIds() {
57+
final query = _queryTwoClosestNeighbors();
58+
final results = query.findIdsWithScores();
59+
query.close();
60+
return results;
61+
}
62+
63+
List<ObjectWithScore<City>> findTwoClosestNeighbors() {
64+
final query = _queryTwoClosestNeighbors();
65+
final results = query.findWithScores();
66+
query.close();
67+
return results;
68+
}
69+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: vectorsearch_cities
2+
description: Vector Search Example with Cities.
3+
version: 1.0.0
4+
5+
environment:
6+
sdk: ^2.18.6
7+
8+
dependencies:
9+
objectbox: ^4.0.0
10+
11+
dev_dependencies:
12+
build_runner: ^2.4.9
13+
lints: ^3.0.0
14+
objectbox_generator: any
15+
16+
# Note: these overrides are only for ObjectBox internal development, don't use them in your app.
17+
dependency_overrides:
18+
objectbox:
19+
path: ../../../../objectbox
20+
objectbox_generator:
21+
path: ../../../../generator

tool/init.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ initialize dart objectbox
4040
initialize dart generator
4141
initialize dart objectbox_test generate
4242
initialize dart benchmark generate
43+
initialize dart objectbox/example/dart-native/vectorsearch_cities generate
4344
initialize flutter objectbox/example/flutter/objectbox_demo generate
4445
initialize flutter objectbox/example/flutter/objectbox_demo_relations generate
4546
initialize flutter objectbox/example/flutter/objectbox_demo_sync generate

tool/pubdev-links.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ echo "Setting pub.dev specific links"
66
update objectbox/README.md "s|CHANGELOG.md|https://pub.dev/packages/objectbox/changelog|g"
77
update objectbox/README.md "s|../CONTRIBUTING.md|https://github.com/objectbox/objectbox-dart/blob/main/CONTRIBUTING.md|g"
88
# Links generated by pub.dev are missing "objectbox" path, so replace with absolute link.
9+
update objectbox/example/README.md "s|dart-native/vectorsearch_cities|https://github.com/objectbox/objectbox-dart/tree/main/objectbox/example/dart-native/vectorsearch_cities|g"
910
update objectbox/example/README.md "s|flutter/objectbox_demo|https://github.com/objectbox/objectbox-dart/tree/main/objectbox/example/flutter/objectbox_demo|g"
1011
#update objectbox/example/README.md "s|flutter/objectbox_demo_relations|https://github.com/objectbox/objectbox-dart/tree/main/objectbox/example/flutter/objectbox_demo_relations|g"
1112
update objectbox/example/README.md "s|flutter/event_management_tutorial|https://github.com/objectbox/objectbox-dart/tree/main/objectbox/example/flutter/event_management_tutorial|g"

tool/set-version.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ versionDartExpr="s/current = \".*\"/current = \"${version}\"/g"
2121
update generator/lib/src/version.dart "${versionDartExpr}"
2222

2323
dependencyHigherExpr="s/objectbox: \^.*/objectbox: ^${version}/g"
24+
update objectbox/example/dart-native/vectorsearch_cities/pubspec.yaml "${dependencyHigherExpr}"
2425
update objectbox/example/flutter/event_management_tutorial/event_manager/pubspec.yaml "${dependencyHigherExpr}"
2526
update objectbox/example/flutter/event_management_tutorial/many_to_many/pubspec.yaml "${dependencyHigherExpr}"
2627
update objectbox/example/flutter/objectbox_demo/pubspec.yaml "${dependencyHigherExpr}"

0 commit comments

Comments
 (0)