Oihana PHP Arango

approxNearCosine.php

Table of Contents

Functions

approxNearCosine()  : string
Return the approximate cosine similarity between two vectors, accelerated by a vector index.

Functions

approxNearCosine()

Return the approximate cosine similarity between two vectors, accelerated by a vector index.

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

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

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

Requires ArangoDB started with the experimental vector index feature.

Example AQL usage:

FOR doc IN items
  SORT APPROX_NEAR_COSINE(doc.embedding, @query) DESC
  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\approxNearCosine;

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

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

For the L2-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