Oihana PHP Arango

approxNearL2.php

Table of Contents

Functions

approxNearL2()  : string
Return the approximate L2 (Euclidean) distance between two vectors, accelerated by a vector index.

Functions

approxNearL2()

Return the approximate L2 (Euclidean) distance between two vectors, accelerated by a vector index.

approxNearL2(string|int $x, string|int $y[, int|null $nProbe = null ]) : string

This helper wraps the ArangoDB AQL function APPROX_NEAR_L2(x, y). One of the two operands must reference a document attribute covered by a VectorIndex created with the "l2" metric; the other is the query vector. The closer the returned value is to 0, the more similar the vectors are.

Because smaller is more similar, you sort in ascending order to get the nearest neighbours first — which is exactly what aqlVectorSearch() does for the l2 metric.

Requires ArangoDB started with the experimental vector index feature.

Example AQL usage:

FOR doc IN items
  SORT APPROX_NEAR_L2(doc.embedding, @query) ASC
  LIMIT 10
  RETURN doc
Parameters
$x : string|int

First operand — the stored attribute or the query vector.

$y : string|int

Second operand — the query vector or the stored attribute.

$nProbe : int|null = null

Optional number of neighbouring centroids to probe (higher = more accurate, slower).

Tags
example
use function oihana\arango\db\functions\numerics\approxNearL2;

$expr = approxNearL2('doc.embedding', '@query');
// Produces: 'APPROX_NEAR_L2(doc.embedding,@query)'

$expr = approxNearL2('doc.embedding', '@query', 20);
// Produces: 'APPROX_NEAR_L2(doc.embedding,@query,{"nProbe":20})'
see
https://docs.arangodb.com/stable/aql/functions/vector/#approx_near_l2
approxNearCosine()

For the cosine-metric counterpart.

aqlVectorSearch()

For the full FOR … SORT … LIMIT query builder.

since
1.1.0
author

Marc Alcaraz

Return values
string

The formatted AQL expression.

On this page

Search results