Skip to content

Instantly share code, notes, and snippets.

@Spartee
Created August 11, 2022 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spartee/3ca231066d4e6862b2271fe3ba259f61 to your computer and use it in GitHub Desktop.
Save Spartee/3ca231066d4e6862b2271fe3ba259f61 to your computer and use it in GitHub Desktop.
Example of creating a flat index in Redis with redis-py
from redis import Redis
from redis.commands.search.field import VectorField, TagField
# Function to create a flat (brute-force) search index with Redis/RediSearch
# Could also be a HNSW index
def create_flat_index(redis_conn: Redis, number_of_vectors: int, distance_metric: str='COSINE'):
image_field = VectorField("img_vector",
"FLAT", {"TYPE": "FLOAT32",
"DIM": 512,
"DISTANCE_METRIC": distance_metric,
"INITIAL_CAP": number_of_vectors,
"BLOCK_SIZE": number_of_vectors})
redis_conn.ft().create_index([image_field])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment