We're talking new releases & fast AI at Redis Released. Join us in your city.

Register now

Building a Fast, Flexible, and Searchable Product Catalog with RedisJSON

November 16, 2021

RedisJSON powered by RediSearch is now out in public preview. In this blog post, we’ll dive into getting you started using RedisJSON’s new JSON indexing, querying, and full-text search capabilities by looking at how it was used to build RedisMart’s product catalog service. In case you missed it, RedisMart is the fully-functional real-time retail store we demoed during the RedisConf 2021 keynote presentation. We also published a blog with a deep-dive into the main requirements and architecture of the RedisMart retail application.

Getting started

RedisJSON is a high-performance document store built on top of Redis open source and available as a source-available license. Its primary goal is to allow you to take the JSON objects you’re likely already using in your application and make them accessible and searchable within Redis. It also offers a more sophisticated data API than the simple hash API, allowing you to expand your data model without having to use multiple keys.

There are a few ways that you can get started using RedisJSON:

  • Try it out on Redis Cloud or in your Redis Enterprise Installation
  • Pull the RedisJSON Docker container, and spin up a copy locally
  • Build RedisJSON from source, and run Redis open source with it

For this post, we’re going to be using the Docker container option:

Once you have that running, you can connect to it using redis-cli:

RedisMart is built in Python, so we’ll connect using the recently simplified developer tools.

Connect to Redis and insert data

At Redis, we tend to prefer using Poetry for dependency management, but the process for adding redis-py to your application is roughly the same for both:

Or, of course:

Once we have redis-py added to our Python environment, we can connect to our Docker container and do a simple “hello world” application:

Now that we have the basics in place, let’s create our data model and index.

Using RedisJSON for document data model

To paraphrase the great Carl Sagan, if you wish to make a RedisJSON product catalog service from scratch, you must first create a search index. For RedisMart, we used a realistic, but straightforward, data model. (See the full Gist here.)

Taking it and creating an index is pretty straightforward:

Notice how, for each field, we’re using the as_name argument to set an alias for the full path. This is helpful for two reasons: one, you don’t have to specify the full path when you’re using that attribute in your query. And two, it allows you to change the underlying path of that attribute without having to change the code that calls it.

Once the index is created, you’ll use the same object to search it. Best practice is to first check to see if the index is created by using the .info() method, and if it isn’t, to do so. Let’s expand our search index creation code to add those cases:

A nice thing about using RedisJSON is that you can easily add new fields to your index with the FT.ALTER command.

Next, we’ll make this search functionality accessible to the rest of the application.

Using RedisJSON for querying the product catalog

One requirement for this project was that we’d be able to query the product catalog using different attributes. Querying by name of the product is an obvious choice, but we also implemented filtering by price, rating, and category as well. In the faceted navigation menu, you can use it to quickly find what you are looking for.

Image

The category also shows up in the autocomplete drop-down powered by the fuzzy search feature of RedisJSON:

Image

Fuzzy search is easy to do using the Suggestions feature, which we can add to any data that we’re adding to the catalog:

Now that we have our index and suggestions setup, let’s build a search query function for our product microservice:

Last, but not least, we’ll need to add the ability to add and modify items from the rest of the application.

RedisJSON for CRUD operations

For RedisMart, we put the product catalog inside of a microservice.

Image

In order to complete the REST API, we’ll need to add our creation, update and delete flows.

Where can I go from here?

By now, you can see how to easily take your JSON-based product data and make it searchable and accessible to any modern application by using a microservice. In addition to indexing, searching, and full-text search features on JSON documents, RedisJSON powered by RediSearch also includes powerful data aggregation capabilities (see tutorial and online course). Here’s some links to get you started

See the full GitHub Gist