Skip to content

Commit fa07d52

Browse files
authored
Merge pull request #1617 from appwrite/codex/add-vector-query-helpers
Add vector query helpers
2 parents c8b7cb9 + ce51f05 commit fa07d52

34 files changed

Lines changed: 345 additions & 9 deletions

File tree

src/SDK/Language.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected function toCamelCase($str): string
113113
$str = Normalizer::normalize($str, Normalizer::FORM_D);
114114

115115
// Remove accents and other residual non-ASCII characters
116-
$str = \preg_replace('/\p{M}/u', '', $str);
116+
$str = \preg_replace('/\p{M}/u', '', (string) $str);
117117

118118
$str = \preg_replace('/[^a-zA-Z0-9]+/', ' ', (string) $str);
119119
$str = \trim((string) $str);
@@ -129,7 +129,7 @@ protected function toSnakeCase($str): string
129129
$str = Normalizer::normalize($str, Normalizer::FORM_D);
130130

131131
// Remove accents and other residual non-ASCII characters
132-
$str = \preg_replace('/\p{M}/u', '', $str);
132+
$str = \preg_replace('/\p{M}/u', '', (string) $str);
133133

134134
// Remove apostrophes before replacing non-word characters with underscores
135135
$str = \str_replace("'", '', $str);

templates/android/library/src/main/java/io/package/Query.kt.twig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,12 @@ class Query(
413413
*/
414414
fun distanceLessThan(attribute: String, values: List<Any>, distance: Number, meters: Boolean = true) = Query("distanceLessThan", attribute, listOf(listOf(values, distance, meters))).toJson()
415415

416+
fun vectorDot(attribute: String, vector: List<Number>) = Query("vectorDot", attribute, listOf(vector)).toJson()
417+
418+
fun vectorCosine(attribute: String, vector: List<Number>) = Query("vectorCosine", attribute, listOf(vector)).toJson()
419+
420+
fun vectorEuclidean(attribute: String, vector: List<Number>) = Query("vectorEuclidean", attribute, listOf(vector)).toJson()
421+
416422
/**
417423
* Filter resources where attribute intersects with the given geometry.
418424
*
@@ -498,4 +504,4 @@ class Query(
498504
}
499505
}
500506
}
501-
}
507+
}

templates/dart/lib/query.dart.twig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,18 @@ class Query {
241241
static String distanceLessThan(String attribute, List<dynamic> values, num distance, [bool meters = true]) =>
242242
Query._('distanceLessThan', attribute, [[values, distance, meters]]).toString();
243243

244+
/// Filter resources using vector dot product similarity.
245+
static String vectorDot(String attribute, List<num> vector) =>
246+
Query._('vectorDot', attribute, [vector]).toString();
247+
248+
/// Filter resources using vector cosine similarity.
249+
static String vectorCosine(String attribute, List<num> vector) =>
250+
Query._('vectorCosine', attribute, [vector]).toString();
251+
252+
/// Filter resources using vector Euclidean distance.
253+
static String vectorEuclidean(String attribute, List<num> vector) =>
254+
Query._('vectorEuclidean', attribute, [vector]).toString();
255+
244256
/// Filter resources where [attribute] intersects with the given geometry.
245257
static String intersects(String attribute, List<dynamic> values) =>
246258
Query._('intersects', attribute, [values]).toString();
@@ -272,4 +284,4 @@ class Query {
272284
/// Filter resources where [attribute] does not touch the given geometry.
273285
static String notTouches(String attribute, List<dynamic> values) =>
274286
Query._('notTouches', attribute, [values]).toString();
275-
}
287+
}

templates/deno/src/query.ts.twig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,36 @@ export class Query {
336336
static distanceLessThan = (attribute: string, values: any[], distance: number, meters: boolean = true): string =>
337337
new Query("distanceLessThan", attribute, [[values, distance, meters]]).toString();
338338

339+
/**
340+
* Filter resources using vector dot product similarity.
341+
*
342+
* @param {string} attribute
343+
* @param {number[]} vector
344+
* @returns {string}
345+
*/
346+
static vectorDot = (attribute: string, vector: number[]): string =>
347+
new Query("vectorDot", attribute, [vector]).toString();
348+
349+
/**
350+
* Filter resources using vector cosine similarity.
351+
*
352+
* @param {string} attribute
353+
* @param {number[]} vector
354+
* @returns {string}
355+
*/
356+
static vectorCosine = (attribute: string, vector: number[]): string =>
357+
new Query("vectorCosine", attribute, [vector]).toString();
358+
359+
/**
360+
* Filter resources using vector Euclidean distance.
361+
*
362+
* @param {string} attribute
363+
* @param {number[]} vector
364+
* @returns {string}
365+
*/
366+
static vectorEuclidean = (attribute: string, vector: number[]): string =>
367+
new Query("vectorEuclidean", attribute, [vector]).toString();
368+
339369
/**
340370
* Filter resources where attribute intersects with the given geometry.
341371
*

templates/dotnet/Package/Query.cs.twig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,21 @@ namespace {{ spec.title | caseUcfirst }}
300300
return new Query("distanceLessThan", attribute, new List<object> { new List<object> { values, distance, meters } }).ToString();
301301
}
302302

303+
public static string VectorDot(string attribute, List<double> vector)
304+
{
305+
return new Query("vectorDot", attribute, new List<object> { vector }).ToString();
306+
}
307+
308+
public static string VectorCosine(string attribute, List<double> vector)
309+
{
310+
return new Query("vectorCosine", attribute, new List<object> { vector }).ToString();
311+
}
312+
313+
public static string VectorEuclidean(string attribute, List<double> vector)
314+
{
315+
return new Query("vectorEuclidean", attribute, new List<object> { vector }).ToString();
316+
}
317+
303318
public static string Intersects(string attribute, object values)
304319
{
305320
return new Query("intersects", attribute, new List<object> { values }).ToString();

templates/go/query.go.twig

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,30 @@ func DistanceLessThan(attribute string, values []interface{}, distance float64,
375375
})
376376
}
377377

378+
func VectorDot(attribute string, vector []float64) string {
379+
return parseQuery(queryOptions{
380+
Method: "vectorDot",
381+
Attribute: &attribute,
382+
Values: &[]interface{}{vector},
383+
})
384+
}
385+
386+
func VectorCosine(attribute string, vector []float64) string {
387+
return parseQuery(queryOptions{
388+
Method: "vectorCosine",
389+
Attribute: &attribute,
390+
Values: &[]interface{}{vector},
391+
})
392+
}
393+
394+
func VectorEuclidean(attribute string, vector []float64) string {
395+
return parseQuery(queryOptions{
396+
Method: "vectorEuclidean",
397+
Attribute: &attribute,
398+
Values: &[]interface{}{vector},
399+
})
400+
}
401+
378402
func Intersects(attribute string, values []interface{}) string {
379403
return parseQuery(queryOptions{
380404
Method: "intersects",
@@ -495,4 +519,4 @@ func ElemMatch(attribute string, queries []string) string {
495519
Attribute: &attribute,
496520
Values: &parsedQueries,
497521
})
498-
}
522+
}

templates/kotlin/src/main/kotlin/io/appwrite/Query.kt.twig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ class Query(
177177
*/
178178
fun distanceLessThan(attribute: String, values: List<Any>, distance: Number, meters: Boolean = true) = Query("distanceLessThan", attribute, listOf(listOf(values, distance, meters))).toJson()
179179

180+
fun vectorDot(attribute: String, vector: List<Number>) = Query("vectorDot", attribute, listOf(vector)).toJson()
181+
182+
fun vectorCosine(attribute: String, vector: List<Number>) = Query("vectorCosine", attribute, listOf(vector)).toJson()
183+
184+
fun vectorEuclidean(attribute: String, vector: List<Number>) = Query("vectorEuclidean", attribute, listOf(vector)).toJson()
185+
180186
fun intersects(attribute: String, values: List<Any>) = Query("intersects", attribute, listOf(values)).toJson()
181187

182188
fun notIntersects(attribute: String, values: List<Any>) = Query("notIntersects", attribute, listOf(values)).toJson()
@@ -200,4 +206,4 @@ class Query(
200206
}
201207
}
202208
}
203-
}
209+
}

templates/php/src/Query.php.twig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,42 @@ class Query implements \JsonSerializable
573573
return (new Query('distanceLessThan', $attribute, [[$values, $distance, $meters]]))->__toString();
574574
}
575575
576+
/**
577+
* Vector Dot
578+
*
579+
* @param string $attribute
580+
* @param array<float> $vector
581+
* @return string
582+
*/
583+
public static function vectorDot(string $attribute, array $vector): string
584+
{
585+
return (new Query('vectorDot', $attribute, [$vector]))->__toString();
586+
}
587+
588+
/**
589+
* Vector Cosine
590+
*
591+
* @param string $attribute
592+
* @param array<float> $vector
593+
* @return string
594+
*/
595+
public static function vectorCosine(string $attribute, array $vector): string
596+
{
597+
return (new Query('vectorCosine', $attribute, [$vector]))->__toString();
598+
}
599+
600+
/**
601+
* Vector Euclidean
602+
*
603+
* @param string $attribute
604+
* @param array<float> $vector
605+
* @return string
606+
*/
607+
public static function vectorEuclidean(string $attribute, array $vector): string
608+
{
609+
return (new Query('vectorEuclidean', $attribute, [$vector]))->__toString();
610+
}
611+
576612
/**
577613
* Intersects
578614
*

templates/python/package/query.py.twig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ class Query():
248248
def distance_less_than(attribute, values, distance, meters=True):
249249
return str(Query("distanceLessThan", attribute, [[values, distance, meters]]))
250250

251+
@staticmethod
252+
def vector_dot(attribute, vector):
253+
return str(Query("vectorDot", attribute, [vector]))
254+
255+
@staticmethod
256+
def vector_cosine(attribute, vector):
257+
return str(Query("vectorCosine", attribute, [vector]))
258+
259+
@staticmethod
260+
def vector_euclidean(attribute, vector):
261+
return str(Query("vectorEuclidean", attribute, [vector]))
262+
251263
@staticmethod
252264
def intersects(attribute, values):
253265
return str(Query("intersects", attribute, [values]))

templates/react-native/src/query.ts.twig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,36 @@ export class Query {
336336
static distanceLessThan = (attribute: string, values: any[], distance: number, meters: boolean = true): string =>
337337
new Query("distanceLessThan", attribute, [[values, distance, meters]] as QueryTypesList).toString();
338338

339+
/**
340+
* Filter resources using vector dot product similarity.
341+
*
342+
* @param {string} attribute
343+
* @param {number[]} vector
344+
* @returns {string}
345+
*/
346+
static vectorDot = (attribute: string, vector: number[]): string =>
347+
new Query("vectorDot", attribute, [vector] as QueryTypesList).toString();
348+
349+
/**
350+
* Filter resources using vector cosine similarity.
351+
*
352+
* @param {string} attribute
353+
* @param {number[]} vector
354+
* @returns {string}
355+
*/
356+
static vectorCosine = (attribute: string, vector: number[]): string =>
357+
new Query("vectorCosine", attribute, [vector] as QueryTypesList).toString();
358+
359+
/**
360+
* Filter resources using vector Euclidean distance.
361+
*
362+
* @param {string} attribute
363+
* @param {number[]} vector
364+
* @returns {string}
365+
*/
366+
static vectorEuclidean = (attribute: string, vector: number[]): string =>
367+
new Query("vectorEuclidean", attribute, [vector] as QueryTypesList).toString();
368+
339369
/**
340370
* Filter resources where attribute intersects with the given geometry.
341371
*

0 commit comments

Comments
 (0)