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?
You didn't answer this one.
np.array(vector, dtype=np.float32).tobytes() for each vector.redis_client.hset("doc:2001", mapping={"title": ..., "content": ..., "embedding": article_bytes}), where article_bytes contains the serialized article embedding.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.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 theembeddingfield. Becausedoc:2001matches thedoc: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:documentsso that it picks updoc:2001. The existing index automatically indexes keys with thedoc: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 withtobytes(). - Not needed: Store the article under the key
article:2001instead ofdoc:2001. The index monitors only keys with thedoc:prefix. A key with a different prefix is not indexed and could not appear in the KNN results.