Follow these steps to set up, build, and run the Valkey server with vector search capabilities. This guide will walk you through creating a vector index, inserting vectors, and issuing queries.
- Build Valkey from source by following the instructions here.
- Build ValkeySearch module from source by following the instructions here.
Once ValkeySearch is built, run the Valkey server with the ValkeySearch module loaded:
./valkey-server "--loadmodule libvalkeysearch.so --reader-threads 64 --writer-threads 64"
You should see the Valkey server start, and it will be ready to accept commands.
To enable vector search functionality, you need to create an index for storing vector data. Start a Valkey CLI session:
valkey-cli
Create a vector field using the FT.CREATE command. For example:
FT.CREATE myIndex SCHEMA vector VECTOR HNSW 6 TYPE FLOAT32 DIM 3 DISTANCE_METRIC COSINE
vector
is the vector field for storing the vectors.VECTOR HNSW
specifies the use of the HNSW (Hierarchical Navigable Small World) algorithm for vector search.DIM 3
sets the vector dimensionality to 3.DISTANCE_METRIC COSINE
sets the distance metric to cosine similarity.
To insert vectors, use the HSET
command:
HSET my_hash_key_1 vector "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80?"
HSET my_hash_key_2 vector "\x00\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x80?"
Replace the \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80?
and \x00\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x80?
with actual vectors.
Now that you've created an index and inserted vectors, you can perform a vector search. Use the FT.SEARCH
command to find similar vectors:
FT.SEARCH myIndex "*=>[KNN 5 @vector $query_vector]" PARAMS 2 query_vector "\xcd\xccL?\x00\x00\x00\x00\x00\x00\x00\x00"
This command performs a K-nearest neighbors search and returns the top 5 closest vectors to the provided query vector.