-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '106-vector-search-example' into 'main'
Add basic Vector Search example using Cities See merge request objectbox/objectbox-dart!86
- Loading branch information
Showing
14 changed files
with
233 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# https://dart.dev/guides/libraries/private-files | ||
# Created by `dart pub` | ||
.dart_tool/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Vector-Search Example: Cities | ||
|
||
This is a simple example application to demonstrate the Vector Search feature of ObjectBox. | ||
It searches for nearest neighbors of a city. | ||
|
||
## Docs | ||
- [Getting started with ObjectBox](https://docs.objectbox.io/getting-started) | ||
- [ANN Vector Search](https://docs.objectbox.io/ann-vector-search) |
30 changes: 30 additions & 0 deletions
30
objectbox/example/dart-native/vectorsearch_cities/analysis_options.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file configures the static analysis results for your project (errors, | ||
# warnings, and lints). | ||
# | ||
# This enables the 'recommended' set of lints from `package:lints`. | ||
# This set helps identify many issues that may lead to problems when running | ||
# or consuming Dart code, and enforces writing Dart using a single, idiomatic | ||
# style and format. | ||
# | ||
# If you want a smaller set of lints you can change this to specify | ||
# 'package:lints/core.yaml'. These are just the most critical lints | ||
# (the recommended set includes the core lints). | ||
# The core lints are also what is used by pub.dev for scoring packages. | ||
|
||
include: package:lints/recommended.yaml | ||
|
||
# Uncomment the following section to specify additional rules. | ||
|
||
# linter: | ||
# rules: | ||
# - camel_case_types | ||
|
||
# analyzer: | ||
# exclude: | ||
# - path/to/excluded/files/** | ||
|
||
# For more information about the core and recommended set of lints, see | ||
# https://dart.dev/go/core-lints | ||
|
||
# For additional information about configuring this file, see | ||
# https://dart.dev/guides/language/analysis-options |
17 changes: 17 additions & 0 deletions
17
objectbox/example/dart-native/vectorsearch_cities/bin/vectorsearch_cities.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:vectorsearch_cities/objectbox.dart'; | ||
|
||
void main(List<String> arguments) { | ||
final objectBox = ObjectBox.create(); | ||
|
||
// Retrieve IDs | ||
final resultIds = objectBox.findTwoClosestNeighborsIds(); | ||
for (final result in resultIds) { | ||
print("City ID: ${result.id}, distance: ${result.score}"); | ||
} | ||
|
||
// Retrieve objects | ||
final results = objectBox.findTwoClosestNeighbors(); | ||
for (final result in results) { | ||
print("City: ${result.object.name}, distance: ${result.score}"); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
objectbox/example/dart-native/vectorsearch_cities/lib/model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:objectbox/objectbox.dart'; | ||
|
||
@Entity() | ||
class City { | ||
@Id() | ||
int id = 0; | ||
|
||
String? name; | ||
|
||
@HnswIndex(dimensions: 2) | ||
@Property(type: PropertyType.floatVector) | ||
List<double>? location; | ||
|
||
City(this.name, this.location); | ||
} |
44 changes: 44 additions & 0 deletions
44
objectbox/example/dart-native/vectorsearch_cities/lib/objectbox-model.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"_note1": "KEEP THIS FILE! Check it into a version control system (VCS) like git.", | ||
"_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.", | ||
"_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.", | ||
"entities": [ | ||
{ | ||
"id": "1:4473667649417503248", | ||
"lastPropertyId": "3:6086893156295683893", | ||
"name": "City", | ||
"properties": [ | ||
{ | ||
"id": "1:7234981322630746044", | ||
"name": "id", | ||
"type": 6, | ||
"flags": 1 | ||
}, | ||
{ | ||
"id": "2:2260839727248414810", | ||
"name": "name", | ||
"type": 9 | ||
}, | ||
{ | ||
"id": "3:6086893156295683893", | ||
"name": "location", | ||
"type": 28, | ||
"flags": 8, | ||
"indexId": "1:145739237546482940" | ||
} | ||
], | ||
"relations": [] | ||
} | ||
], | ||
"lastEntityId": "1:4473667649417503248", | ||
"lastIndexId": "1:145739237546482940", | ||
"lastRelationId": "0:0", | ||
"lastSequenceId": "0:0", | ||
"modelVersion": 5, | ||
"modelVersionParserMinimum": 5, | ||
"retiredEntityUids": [], | ||
"retiredIndexUids": [], | ||
"retiredPropertyUids": [], | ||
"retiredRelationUids": [], | ||
"version": 1 | ||
} |
69 changes: 69 additions & 0 deletions
69
objectbox/example/dart-native/vectorsearch_cities/lib/objectbox.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'model.dart'; | ||
import 'objectbox.g.dart'; | ||
|
||
/// Provides access to the ObjectBox Store throughout the app. | ||
class ObjectBox { | ||
/// The Store of this app. | ||
late final Store _store; | ||
|
||
late final Box<City> _cityBox; | ||
|
||
ObjectBox._create(this._store) { | ||
_cityBox = Box<City>(_store); | ||
|
||
if (_cityBox.isEmpty()) { | ||
_putCityData(); | ||
} | ||
} | ||
|
||
/// Create an instance of ObjectBox to use throughout the app. | ||
static ObjectBox create() { | ||
// Note: set macosApplicationGroup for sandboxed macOS applications, see the | ||
// Store documentation for details. | ||
|
||
// Store openStore() {...} is defined in the generated objectbox.g.dart | ||
final store = openStore( | ||
directory: "obx-demo-vectorsearch-cities", | ||
macosApplicationGroup: "objectbox.demo"); | ||
return ObjectBox._create(store); | ||
} | ||
|
||
_putCityData() { | ||
_cityBox.putMany([ | ||
City("Barcelona", [41.385063, 2.173404]), | ||
City("Nairobi", [-1.292066, 36.821945]), | ||
City("Salzburg", [47.809490, 13.055010]), | ||
]); | ||
} | ||
|
||
Query<City> _queryTwoClosestNeighbors() { | ||
final madrid = [40.416775, -3.703790]; // query vector | ||
// Prepare a Query object to search for the 2 closest neighbors: | ||
final query = | ||
_cityBox.query(City_.location.nearestNeighborsF32(madrid, 2)).build(); | ||
|
||
// Combine with other conditions as usual | ||
// ignore: unused_local_variable | ||
final queryCombined = _cityBox | ||
.query(City_.location | ||
.nearestNeighborsF32(madrid, 2) | ||
.and(City_.name.startsWith("B"))) | ||
.build(); | ||
|
||
return query; | ||
} | ||
|
||
List<IdWithScore> findTwoClosestNeighborsIds() { | ||
final query = _queryTwoClosestNeighbors(); | ||
final results = query.findIdsWithScores(); | ||
query.close(); | ||
return results; | ||
} | ||
|
||
List<ObjectWithScore<City>> findTwoClosestNeighbors() { | ||
final query = _queryTwoClosestNeighbors(); | ||
final results = query.findWithScores(); | ||
query.close(); | ||
return results; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
objectbox/example/dart-native/vectorsearch_cities/pubspec.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: vectorsearch_cities | ||
description: Vector Search Example with Cities. | ||
version: 1.0.0 | ||
|
||
environment: | ||
sdk: ^2.18.6 | ||
|
||
dependencies: | ||
objectbox: ^4.0.0 | ||
|
||
dev_dependencies: | ||
build_runner: ^2.4.9 | ||
lints: ^3.0.0 | ||
objectbox_generator: any | ||
|
||
# Note: these overrides are only for ObjectBox internal development, don't use them in your app. | ||
dependency_overrides: | ||
objectbox: | ||
path: ../../../../objectbox | ||
objectbox_generator: | ||
path: ../../../../generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters