Launch price: every exam for $29.99 a year, for the first 300 buyers. See the price

CertStreak
Menu

Implement vector indexing to enable similarity search

A worked example

Put in order

You maintain a support chatbot that uses Azure Managed Redis for vector search. You work in Python with the redis-py client.

The index idx:documents already exists. It was created with IndexDefinition(prefix=["doc:"], index_type=IndexType.HASH) and a schema containing TextField("title"), TextField("content") and VectorField("embedding", "HNSW", {"TYPE": "FLOAT32", "DIM": 1536, "DISTANCE_METRIC": "COSINE"}). All existing documents were embedded with a text embedding model that outputs 1536 values.

You must add a new article with the key doc:2001.

Requirements:

  • Store the article as a Redis hash, with its title, content and vector together in that single key.
  • Use the existing embedding model for the new article and the user question.
  • Do not drop or recreate idx:documents.
  • After storing the article and allowing automatic indexing to complete, execute the application's 5-nearest-neighbour query for a user question. The new article must be eligible to appear in its results.

Which actions should you perform, and in which order?

Your order

You didn't answer this one.

Correct order
1Use the existing embedding model to generate a 1536-dimensional embedding for each input: the new article's text and the user question.
2Serialize both embeddings as FLOAT32 bytes using np.array(vector, dtype=np.float32).tobytes() for each vector.
3Call redis_client.hset("doc:2001", mapping={"title": ..., "content": ..., "embedding": article_bytes}), where article_bytes contains the serialized article embedding.
4Once automatic indexing of the new article has completed, run Query("*=>[KNN 5 @embedding $query_vec AS score]").sort_by("score").dialect(2) against ft("idx:documents") with query_params={"query_vec": question_bytes}, where question_bytes contains the serialized question embedding.
Why

The article's embedding must contain 1536 FLOAT32 values to match the existing vector field. Convert it to bytes and use hset to store it alongside the title and content. The matching doc: prefix causes Redis to index the hash automatically without recreating the index. After indexing completes, execute the required KNN search using the question embedding as bytes and dialect 2.

  • Step 1: Generate the article and question embeddings. Use the existing model so both vectors have the 1536 dimensions required by the index and share the same embedding space.
  • Step 2: Serialize both embeddings as FLOAT32 bytes. The article vector goes into the Redis hash, and the question vector becomes the KNN query parameter.
  • Step 3: Store the article hash with hset. Put the serialized article embedding in the embedding field. Because doc:2001 matches the doc: prefix, Redis indexes the vector automatically.
  • Step 4: Run the KNN query with the serialized question embedding. Waiting for automatic indexing to finish makes the article eligible for retrieval.
  • Not needed: Recreate idx:documents so that it picks up doc:2001. The existing index automatically indexes keys with the doc: prefix, and the scenario requires keeping that index.
  • Not needed: Store the embedding field in the hash as embedding.tolist() instead of as bytes. tolist() produces the numeric array format used for Redis JSON documents. Vectors in a Redis hash are stored as binary bytes created with tobytes().
  • Not needed: Store the article under the key article:2001 instead of doc:2001. The index monitors only keys with the doc: prefix. A key with a different prefix is not indexed and could not appear in the KNN results.

Practise this topic until it's automatic.

Ten questions a day, every answer explained, and misses that come back until you beat them.

Start free