-
Notifications
You must be signed in to change notification settings - Fork 8
DOC-738 | Vector index reference docs #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Simran-B
wants to merge
8
commits into
main
Choose a base branch
from
DOC-738
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a9a3f4a
Initial reference docs about the vector index
Simran-B 604f1a0
HTTP API docs and refinements
Simran-B 1bd63de
Merge branch 'main' of https://github.com/arangodb/docs-hugo into DOC…
Simran-B 4270384
Version remark, OpenAPI minItems/maxItems and fix a type
Simran-B 209675a
inBackground and parallelism are supported
Simran-B 03050a5
Review feedback, address cosine metric issue, reword inBackground, ad…
Simran-B 1aa411b
Remove leftover line
Simran-B 5a017d7
Add internal links to release notes
Simran-B File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,191 @@ | ||
--- | ||
title: Vector search functions in AQL | ||
menuTitle: Vector | ||
weight: 60 | ||
description: >- | ||
The functions for vector search let you quickly find semantically similar | ||
documents utilizing indexed vector embeddings | ||
--- | ||
To use vector search, you need to have vector embeddings stored in documents | ||
and the attribute that stores them needs to be indexed by a | ||
[vector index](../../index-and-search/indexing/working-with-indexes/vector-indexes.md). | ||
|
||
You can calculate vector embeddings using [ArangoDB's GraphML](../../data-science/arangographml/_index.md) | ||
capabilities (available in ArangoGraph) or using external tools. | ||
|
||
{{< warning >}} | ||
The vector index is an experimental feature that you need to enable for the | ||
ArangoDB server with the `--experimental-vector-index` startup option. | ||
Once enabled for a deployment, it cannot be disabled anymore because it | ||
permanently changes how the data is managed by the RocksDB storage engine | ||
(it adds an additional column family). | ||
|
||
To restore a dump that contains vector indexes, the `--experimental-vector-index` | ||
startup option needs to be enabled on the deployment you want to restore to. | ||
{{< /warning >}} | ||
|
||
## Vector similarity functions | ||
|
||
In order to utilize a vector index, you need to do the following in an AQL query: | ||
|
||
- Use one of the following vector similarity functions in a query. | ||
- `SORT` by the similarity so that the most similar documents come first. | ||
- Specify the maximum number of documents to retrieve with a `LIMIT` operation. | ||
|
||
As a result, you get up to the specified number of documents whose vector embeddings | ||
are the most similar to the reference vector embedding you provided in the query, | ||
as approximated by the vector index. | ||
|
||
Example: | ||
|
||
```aql | ||
FOR doc IN coll | ||
SORT APPROX_NEAR_L2(doc.vector, @q) | ||
LIMIT 5 | ||
RETURN doc | ||
``` | ||
|
||
For this query, a vector index over the `vector` attribute and with the `l2` | ||
metric is required. The `@q` bind variable needs to be a vector (array of numbers) | ||
with the dimension as specified in the vector index. It defines the point at | ||
which to look for neighbors (up to `5` in this case). How many neighbors can be | ||
found depends on the data as well as the search effort (see the `nProbe` option). | ||
|
||
{{< info >}} | ||
- If there is more than one suitable vector index over the same attribute, it is | ||
undefined which one is selected. | ||
- You cannot have any `FILTER` operation between `FOR` and `LIMIT` for | ||
pre-filtering. | ||
{{< /info >}} | ||
|
||
### APPROX_NEAR_COSINE() | ||
|
||
`APPROX_NEAR_COSINE(vector1, vector2, options) → similarity` | ||
|
||
Retrieve the approximate angular similarity using the cosine metric, accelerated | ||
by a matching vector index. | ||
|
||
The closer the cosine similarity value is to 1, the more similar the two vectors | ||
are. The closer it is to 0, the more different they are. The value can also | ||
be up to -1, indicating that the vectors are not similar and point in opposite | ||
directions. You need to sort in descending order so that the most similar | ||
documents come first, which is what a vector index using the `cosine` metric | ||
can provide. | ||
|
||
- **vector1** (array of numbers): The first vector. Either this parameter or | ||
`vector2` needs to reference a stored attribute holding the vector embedding. | ||
attribute of a stored document that stores a vector, like `doc.vector` | ||
- **vector2** (array of numbers): The second vector. Either this parameter or | ||
`vector1` needs to reference a stored attribute holding the vector embedding. | ||
- **options** (object, _optional_): | ||
- **nProbe** (number, _optional_): How many neighboring centroids to consider | ||
for the search results. The larger the number, the slower the search but the | ||
better the search results. If not specified, the `defaultNProbe` value of | ||
the vector index is used. | ||
- returns **similarity** (number): The approximate angular similarity between | ||
both vectors. | ||
|
||
**Examples** | ||
|
||
Return the documents of up to `10` neighbors that are the closest to the vector | ||
`@q` according to the cosine metric: | ||
|
||
```aql | ||
FOR doc IN coll | ||
SORT APPROX_NEAR_COSINE(doc.vector, @q) DESC | ||
LIMIT 10 | ||
RETURN doc | ||
``` | ||
|
||
Return the similarity value and the documents of up to `5` close neighbors, | ||
considering `20` neighboring centroids: | ||
|
||
```aql | ||
FOR doc IN coll | ||
LET similarity = APPROX_NEAR_COSINE(doc.vector, @q, { nProbe: 20 }) | ||
SORT similarity DESC | ||
LIMIT 5 | ||
RETURN MERGE( { similarity }, doc) | ||
``` | ||
|
||
Return the similarity and the document keys of up to `3` neighbors for multiple | ||
vectors using a subquery, here taking from ten random documents of the same | ||
collection: | ||
|
||
```aql | ||
FOR docOuter IN coll | ||
LIMIT 10 | ||
LET neighbors = ( | ||
FOR docInner IN coll | ||
LET similarity = APPROX_NEAR_COSINE(docInner.vector, docOuter.vector) | ||
SORT similarity DESC | ||
LIMIT 3 | ||
RETURN { key: docInner._key, similarity } | ||
) | ||
RETURN { key: docOuter._key, neighbors } | ||
``` | ||
|
||
### APPROX_NEAR_L2() | ||
|
||
`APPROX_NEAR_L2(vector1, vector2, options) → similarity` | ||
|
||
Retrieve the approximate distance using the L2 (Euclidean) metric, accelerated | ||
by a matching vector index. | ||
|
||
The closer the distance is to 0, the more similar the two vectors are. The higher | ||
the value, the more different the they are. You need to sort in ascending order | ||
so that the most similar documents come first, which is what a vector index using | ||
the `l2` metric can provide. | ||
|
||
- **vector1** (array of numbers): The first vector. Either this parameter or | ||
`vector2` needs to reference a stored attribute holding the vector embedding. | ||
attribute of a stored document that stores a vector, like `doc.vector` | ||
- **vector2** (array of numbers): The second vector. Either this parameter or | ||
`vector1` needs to reference a stored attribute holding the vector embedding. | ||
- **options** (object, _optional_): | ||
- **nProbe** (number, _optional_): How many neighboring centroids to consider | ||
for the search results. The larger the number, the slower the search but the | ||
better the search results. If not specified, the `defaultNProbe` value of | ||
the vector index is used. | ||
- returns **similarity** (number): The approximate L2 (Euclidean) distance between | ||
both vectors. | ||
|
||
**Examples** | ||
|
||
Return the documents of up to `10` neighbors that are the closest to the vector | ||
`@q` according to the L2 (Euclidean) metric: | ||
|
||
```aql | ||
FOR doc IN coll | ||
SORT APPROX_NEAR_L2(doc.vector, @q) | ||
LIMIT 10 | ||
RETURN doc | ||
``` | ||
|
||
Return the similarity value and the documents of up to `5` close neighbors, | ||
considering `20` neighboring centroids: | ||
Simran-B marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```aql | ||
FOR doc IN coll | ||
LET similarity = APPROX_NEAR_L2(doc.vector, @q, { nProbe: 20 }) | ||
SORT similarity | ||
LIMIT 5 | ||
RETURN MERGE( { similarity }, doc) | ||
``` | ||
|
||
Return the similarity and the document keys of up to `3` close neighbors for | ||
multiple vectors using a subquery, here taking from ten random documents of the | ||
same collection: | ||
|
||
```aql | ||
FOR docOuter IN coll | ||
LIMIT 10 | ||
LET neighbors = ( | ||
FOR docInner IN coll | ||
LET similarity = APPROX_NEAR_L2(docInner.vector, docOuter.vector) | ||
SORT similarity | ||
LIMIT 3 | ||
RETURN { key: docInner._key, similarity } | ||
) | ||
RETURN { key: docOuter._key, neighbors } | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.