You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/develop/get-started/vector-database.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -76,13 +76,13 @@ Create a Python virtual environment and install the following dependencies using
76
76
77
77
You will also need the following imports in your Python code:
78
78
79
-
{{< clients-example search_vssimports />}}
79
+
{{< clients-example set="search_vss" step="imports" description="Foundational: Import required Python packages for vector database operations including redis, pandas, and sentence-transformers" difficulty="beginner" />}}
80
80
81
81
## Connect
82
82
83
83
Connect to Redis. By default, Redis returns binary responses. To decode them, you pass the `decode_responses` parameter set to `True`:
84
84
85
-
{{< clients-example search_vssconnect />}}
85
+
{{< clients-example set="search_vss" step="connect" description="Foundational: Connect to a Redis server with decode_responses enabled to receive decoded strings instead of bytes" difficulty="beginner" />}}
86
86
<br/>
87
87
{{% alert title="Tip" color="warning" %}}
88
88
Instead of using a local Redis server, you can copy and paste the connection details from the Redis Cloud database configuration page. Here is an example connection string of a Cloud database that is hosted in the AWS region `us-east-1` and listens on port 16379: `redis-16379.c283.us-east-1-4.ec2.cloud.redislabs.com:16379`. The connection string has the format `host:port`. You must also copy and paste the username and password of your Cloud database. The line of code for connecting with the default user changes then to `client = redis.Redis(host="redis-16379.c283.us-east-1-4.ec2.cloud.redislabs.com", port=16379, password="your_password_here", decode_responses=True)`.
@@ -113,20 +113,20 @@ The `description` field contains free-form text descriptions of bikes and will b
113
113
### 1. Fetch the demo data
114
114
You need to first fetch the demo dataset as a JSON array:
115
115
116
-
{{< clients-example search_vssget_data />}}
116
+
{{< clients-example set="search_vss" step="get_data" description="Foundational: Fetch the demo dataset as a JSON array to prepare data for vector database operations" difficulty="beginner" />}}
117
117
118
118
Inspect the structure of one of the bike JSON documents:
119
119
120
-
{{< clients-example search_vssdump_data />}}
120
+
{{< clients-example set="search_vss" step="dump_data" description="Foundational: Inspect the structure of a single document from the demo dataset to understand the data format" difficulty="beginner" />}}
121
121
122
122
### 2. Store the demo data in Redis
123
123
Now iterate over the `bikes` array to store the data as [JSON]({{< relref "/develop/data-types/json/" >}}) documents in Redis by using the [JSON.SET]({{< relref "commands/json.set/" >}}) command. The below code uses a [pipeline]({{< relref "/develop/using-commands/pipelining" >}}) to minimize the network round-trip times:
124
124
125
-
{{< clients-example search_vssload_data />}}
125
+
{{< clients-example set="search_vss" step="load_data" description="Practical pattern: Store demo data as JSON documents in Redis using JSON.SET with pipelining to minimize network round-trips" difficulty="beginner" />}}
126
126
127
127
Once loaded, you can retrieve a specific attribute from one of the JSON documents in Redis using a [JSONPath](https://goessner.net/articles/JsonPath/) expression:
128
128
129
-
{{< clients-example search_vssget />}}
129
+
{{< clients-example set="search_vss" step="get" description="Foundational: Retrieve a specific attribute from a JSON document in Redis using JSONPath expressions" difficulty="beginner" />}}
Iterate over all the Redis keys with the prefix `bikes:`:
143
143
144
-
{{< clients-example search_vssget_keys />}}
144
+
{{< clients-example set="search_vss" step="get_keys" description="Foundational: Retrieve all Redis keys with a specific prefix to identify documents for processing" difficulty="beginner" />}}
145
145
146
146
Use the keys as input to the [JSON.MGET]({{< relref "commands/json.mget/" >}}) command, along with the `$.description` field, to collect the descriptions in a list. Then, pass the list of descriptions to the `.encode()` method:
{{< clients-example set="search_vss" step="generate_embeddings" description="Create embeddings: Generate text embeddings from document descriptions using SentenceTransformers model to create vector representations" difficulty="intermediate" />}}
149
149
150
150
Insert the vectorized descriptions to the bike documents in Redis using the [JSON.SET]({{< relref "commands/json.set" >}}) command. The following command inserts a new field into each of the documents under the JSONPath `$.description_embeddings`. Once again, do this using a pipeline to avoid unnecessary network round-trips:
{{< clients-example set="search_vss" step="load_embeddings" description="Practical pattern: Insert generated vector embeddings into JSON documents using JSON.SET with pipelining for efficient batch updates" difficulty="intermediate" />}}
153
153
154
154
Inspect one of the updated bike documents using the [JSON.GET]({{< relref "commands/json.get" >}}) command:
155
155
156
-
{{< clients-example search_vssdump_example />}}
156
+
{{< clients-example set="search_vss" step="dump_example" description="Foundational: Inspect an updated document containing both original data and generated vector embeddings using JSON.GET" difficulty="intermediate" />}}
157
157
158
158
{{% alert title="Note" color="warning" %}}
159
159
When storing a vector embedding within a JSON document, the embedding is stored as a JSON array. In the example above, the array was shortened considerably for the sake of readability.
@@ -166,7 +166,7 @@ When storing a vector embedding within a JSON document, the embedding is stored
166
166
167
167
You must create an index to query document metadata or to perform vector searches. Use the [FT.CREATE]({{< relref "commands/ft.create" >}}) command:
168
168
169
-
{{< clients-example search_vsscreate_index >}}
169
+
{{< clients-example set="search_vss" step="create_index" description="Foundational: Create an index on JSON documents with vector field using FT.CREATE with FLAT indexing and COSINE distance metric" difficulty="intermediate" >}}
170
170
FT.CREATE idx:bikes_vss ON JSON
171
171
PREFIX 1 bikes: SCORE 1.0
172
172
SCHEMA
@@ -192,7 +192,7 @@ You can find further details about all these options in the [vector reference do
192
192
193
193
As soon as you execute the [FT.CREATE]({{< relref "commands/ft.create" >}}) command, the indexing process runs in the background. In a short time, all JSON documents should be indexed and ready to be queried. To validate that, you can use the [FT.INFO]({{< relref "commands/ft.info" >}}) command, which provides details and statistics about the index. Of particular interest are the number of documents successfully indexed and the number of failures:
194
194
195
-
{{< clients-example search_vssvalidate_index >}}
195
+
{{< clients-example set="search_vss" step="validate_index" description="Practical pattern: Check the state and statistics of an index using FT.INFO to verify successful indexing of documents" difficulty="intermediate" >}}
196
196
FT.INFO idx:bikes_vss
197
197
{{< /clients-example >}}
198
198
@@ -204,11 +204,11 @@ This quick start guide focuses on vector search. However, you can learn more abo
204
204
205
205
The following code snippet shows a list of text queries you will use to perform vector search in Redis:
{{< clients-example set="search_vss" step="def_bulk_queries" description="Foundational: Define a list of text queries to use for vector search operations" difficulty="beginner" />}}
208
208
209
209
First, encode each input query as a vector embedding using the same SentenceTransformers model:
{{< clients-example set="search_vss" step="enc_bulk_queries" description="Create query embeddings: Encode text queries as vector embeddings using the same SentenceTransformers model used for document embeddings" difficulty="intermediate" />}}
212
212
213
213
<br/>
214
214
{{% alert title="Tip" color="warning" %}}
@@ -260,7 +260,7 @@ With the template for the query in place, you can execute all queries in a loop.
260
260
261
261
Then, loop over the matched documents and create a list of results that can be converted into a Pandas table to visualize the results:
{{< clients-example set="search_vss" step="define_bulk_query" description="Query data: Execute multiple vector search queries and format results into a Pandas table for visualization and analysis" difficulty="advanced" />}}
264
264
265
265
The query results show the individual queries' top three matches (our K parameter) along with the bike's id, brand, and model for each query.
266
266
@@ -270,7 +270,7 @@ For example, for the query "Best Mountain bikes for kids", the highest similarit
270
270
271
271
From the description, this bike is an excellent match for younger children, and the embeddings accurately captured the semantics of the description.
272
272
273
-
{{< clients-example search_vssrun_knn_query />}}
273
+
{{< clients-example set="search_vss" step="run_knn_query" description="Semantic search: Execute K-nearest neighbors vector search using FT.SEARCH with KNN algorithm to find semantically similar documents" difficulty="advanced" />}}
274
274
275
275
| query | score | id | brand | model | description |
0 commit comments