Skip to content

Commit

Permalink
Vector search #131
Browse files Browse the repository at this point in the history
  • Loading branch information
happy-san committed May 9, 2023
1 parent 163342e commit ee87235
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.3.1

* Added `dimensions` field to support creating a vector field in schema.

# 0.3.0+1

* Update README.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Add `typesense` as a [dependency in your pubspec.yaml file](https://flutter.dev/

```@yaml
dependencies:
typesense: ^0.3.0
typesense: ^0.3.1
```

## Usage
Expand Down
36 changes: 36 additions & 0 deletions example/console-simple/bin/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Future<void> runExample(Client client) async {
await init(client);
await search(client);
await geosearch(client);
await vectorsearch(client);
await multisearch(client);
await collections.delete(client);
}
Expand Down Expand Up @@ -139,6 +140,41 @@ Future<void> geosearch(Client client) async {
}
}

Future<void> vectorsearch(Client client) async {
try {
await collections.create(
client,
Schema(
'docs',
{
Field('title', type: Type.string),
Field('points', type: Type.int32),
Field('vec', type: Type.float, isMultivalued: true, dimensions: 4),
},
defaultSortingField: Field('points', type: Type.int32),
));
await documents.importDocs(client, 'docs', [
{
'title': 'Louvre Museuem',
'points': 1,
'vec': [0.04, 0.234, 0.113, 0.001]
}
]);

logInfoln(log, 'Vector search.');
log.fine(await client.collection('docs').documents.search({
'q': '*',
'vector_query': 'vec:([0.96826, 0.94, 0.39557, 0.306488], k:100)',
}));

await collections.delete(client, 'docs');
} on RequestException catch (e, stackTrace) {
log.severe(e.message, e, stackTrace);
} catch (e, stackTrace) {
log.severe(e, stackTrace);
}
}

Future<void> multisearch(Client client) async {
try {
logInfoln(log, 'Executing multiple searches.');
Expand Down
2 changes: 1 addition & 1 deletion example/console-simple/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ packages:
path: "../.."
relative: true
source: path
version: "0.3.0+1"
version: "0.3.1"
sdks:
dart: ">=2.17.0 <3.0.0"
15 changes: 15 additions & 0 deletions lib/src/models/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Field {
/// [Type.float], [Type.bool] or [Type.geopoint]
final bool isMultivalued;

/// Used in case of a vector field. Represents the number of dimensions
/// (length of the float array) that your embeddings contain.
final int dimensions;

/// If this field can be ommited in a document.
final bool isOptional;

Expand Down Expand Up @@ -43,6 +47,7 @@ class Field {
this.name, {
this.type,
this.isMultivalued = false,
this.dimensions = 0,
this.isOptional = false,
this.isFacetable = false,
this.shouldIndex = true,
Expand All @@ -65,6 +70,7 @@ class Field {
? _Type.fromValue(map['type'], isMultivalued)
: null,
isMultivalued: isMultivalued,
dimensions: map['num_dim'] ?? 0,
isOptional: map['optional'] ?? false,
isFacetable: map['facet'] ?? false,
shouldIndex: map['index'] ?? true,
Expand All @@ -80,6 +86,9 @@ class Field {
if (type != null) {
map['type'] = type!.value(isMultivalued);
}
if (dimensions > 0) {
map['num_dim'] = dimensions;
}
if (isOptional) {
map['optional'] = true;
}
Expand Down Expand Up @@ -111,6 +120,7 @@ class Field {
name.hashCode ^
type.hashCode ^
isMultivalued.hashCode ^
dimensions.hashCode ^
isOptional.hashCode ^
isFacetable.hashCode ^
shouldIndex.hashCode ^
Expand All @@ -125,6 +135,7 @@ class Field {
other.name == name &&
other.type == type &&
other.isMultivalued == isMultivalued &&
other.dimensions == dimensions &&
other.isOptional == isOptional &&
other.isFacetable == isFacetable &&
other.shouldIndex == shouldIndex &&
Expand All @@ -147,6 +158,7 @@ class UpdateField extends Field {
super.name, {
super.type,
super.isMultivalued,
super.dimensions,
super.isOptional,
super.isFacetable,
super.shouldIndex,
Expand All @@ -172,6 +184,7 @@ class UpdateField extends Field {
field.name,
type: field.type,
isMultivalued: field.isMultivalued,
dimensions: field.dimensions,
isOptional: field.isOptional,
isFacetable: field.isFacetable,
shouldIndex: field.shouldIndex,
Expand All @@ -187,6 +200,7 @@ class UpdateField extends Field {
name.hashCode ^
type.hashCode ^
isMultivalued.hashCode ^
dimensions.hashCode ^
isOptional.hashCode ^
isFacetable.hashCode ^
shouldIndex.hashCode ^
Expand All @@ -202,6 +216,7 @@ class UpdateField extends Field {
other.name == name &&
other.type == type &&
other.isMultivalued == isMultivalued &&
other.dimensions == dimensions &&
other.isOptional == isOptional &&
other.isFacetable == isFacetable &&
other.shouldIndex == shouldIndex &&
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: typesense
description: Dart client library for accessing the HTTP API of Typesense search engine.
version: 0.3.0+1
version: 0.3.1
homepage: https://typesense.org/
repository: https://github.com/typesense/typesense-dart

Expand Down

0 comments on commit ee87235

Please sign in to comment.