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.
- Follow the instructions to build Valkey from source. Make sure to use Valkey version 7.2.6 or later as the previous versions have Valkey module API bugs.
- Follow the instructions to build the valkey-search module from source.
Once valkey-search is built, run the Valkey server with the valkey-search module loaded:
./valkey-server "--loadmodule libsearch.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-cliCreate a vector field using the FT.CREATE command. For example:
FT.CREATE myIndex SCHEMA vector VECTOR HNSW 6 TYPE FLOAT32 DIM 3 DISTANCE_METRIC COSINEvectoris the vector field for storing the vectors.VECTOR HNSWspecifies the use of the HNSW (Hierarchical Navigable Small World) algorithm for vector search.DIM 3sets the vector dimensionality to 3.DISTANCE_METRIC COSINEsets 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.