You can now explore advanced data modeling, with support for secondary indexing, JSON documents, and the ability to search your data with the go-redis client library.
With the latest addition, go-redis is getting closer to supporting all the capabilities offered by Redis Community Edition (Redis Stack and Redis 8), Redis Software, and Redis Cloud. JSON data modeling does not need many presentations: the most popular exchange format, supported by all Redis versions, allows for flexible data management to resolve many of the modern problems of storing hierarchical data in the database. However, only with the search features can you get the most out of your data. The Redis query engine supports indexing hash and JSON documents and simplifies the retrieval of any single piece of data.
Let’s look at some examples of recent additions. To test the samples proposed in this post, create a free Redis Cloud account on AWS, GCP, or Azure. If you’d rather run Redis on your laptop, look at Redis 8 (the M02 milestone is available) on Docker Hub, or install Redis Stack using the preferred installation method.
Create a connection to Redis as usual (connect with the credentials from your Redis Cloud account. Docker images have an empty default password).
Try to create simple JSON documents and execute standard commands as follows:
Let’s try something advanced. Imagine you’d like to model a shopping cart as a JSON document:
You can create the document with the following command:
Apart from reading or writing different items in your shopping cart selectively, you can search within the cart using the JSONPath syntax. So, if you want to check what items are cheaper than 2500 dollars in your cart, the solution is served:
The ability to model your data using the JSON format evolves the simple yet efficient hash data structure. Only the JSON format supports hierarchical data and internal search with JSONPath. But things get even more interesting because you can search across different JSON documents.
The go-redis client library now supports all the features in the Redis query engine. Back to our shopping cart example, if you want to check what carts contain a specific item or maybe refine the search further to include the desired criteria, this is possible using the API available in go-redis. Consider the following example, where we’ll create an index.
The previous code does the following.
- It cleans up an eventual index named json_session_idx together with the data indexed
- It defines what data in the cart is required to be indexed. The specified fields are:
- All the items’ product IDs, for which we require an exact match using the TAG field index
- The locations of the users, expressed by the pairs (longitude, latitude), for which we require the GEO geographical indexing
- We require a numeric search provided by the NUMERIC field type for the price of the items in the cart.
- It creates the index, specifying that JSON documents prefixed by “session:” should be indexed.
Once the index is created, all the existing and new documents in the database will be added. Index creation is asynchronous, but indexing happens synchronously whenever a new document is added. If you create a new index over an existing dataset, indexing will take instants. Now, let’s add a couple of sessions.
The first example is an exact match operation that retrieves how many sessions contain a specific item in the cart.
Now, we will put together multiple criteria to search for the price. The following command returns the prices of those items that are present in all the sessions:
- Have the product ID equal to “hp-printer”
- Are in the range of 60km from a specified location
- Are more expensive than 50 dollars
The query syntax documentation explains the query syntax to be used. Searching our Redis documents is indeed powerful, but things become even more interesting when we introduce semantic search in our application.
The go-redis client fully supports vector search, which enables semantic search, one of the building blocks of Redis for AI, the integrated package for developing GenAI applications. Let’s explore the functionality with a simple example: We will store three sentences and test which sentence in the database is the most semantically similar to a query sentence. Vector search is supported for hash and JSON documents; in this example, we’ll model our sentences once more as JSON documents.
Once we have defined our dataset and the query sentence, let’s create the index.
The former code cleans up the index and the indexed documents; then, it creates an index for vectors with the following features.
- Vectors are floats of 64 bytes.
- Vectors contain 1536 elements, determined by the embedding model we’ll introduce shortly.
- The distance metric is cosine
- The indexing method is of type HNSW
- We are indexing JSON documents prefixed by “json:doc:”
- The vector is stored in the JSON document at “$.v”
We will create the vector embedding representation to create the database documents. To achieve this, we resort to OpenAI embedding models. This helper function transforms a sentence into an array of 1536 float64 elements. The chosen OpenAI embedding model is text-embedding-ada-002.
Note that you can install the official Go library for the OpenAI library as follows:
We can now store our sentences with their semantic representation in the database.
We are ready to search for sentences similar to the query sentence!
As expected, the query sentence (about the “Adventures of Pinocchio”) is more semantically similar to “The Little Prince” rather than the rest of the documents.
You can test the examples in this article using a free Redis Cloud database or the Redis 8 M02 Docker image available from Docker Hub. In addition:
- Refer to the Go guide page to learn more about the go-redis client library
- Find additional examples from the GitHub repository
- Visualize your data, profile the execution of queries, or ask the Copilot all your questions about Redis or your data. Copilot can help you build queries starting from the data in your database with AI. Download Redis Insight; it’s free.
What are you going to build with Redis?