Redis as a document database quick start guide

Understand how to use Redis as a document database

This quick start guide shows you how to:

  1. Create a secondary index
  2. Add JSON documents
  3. Search and query your data

The examples in this article refer to a simple bicycle inventory that contains JSON documents with the following structure:

{
  "brand": "brand name",
  "condition": "new | used | refurbished",
  "description": "description",
  "model": "model",
  "price": 0
}

Setup

The easiest way to get started with Redis Stack is to use Redis Cloud:

  1. Create a free account.

  2. Follow the instructions to create a free database.

This free Redis Cloud database comes out of the box with all the Redis Stack features.

You can alternatively use the installation guides to install Redis Stack on your local machine.

Connect

The first step is to connect to your Redis Stack database. You can find further details about the connection options in this documentation site's connection section. The following example shows how to connect to a Redis Stack server that runs on localhost (-h 127.0.0.1) and listens on the default port (-p 6379):


Tip:
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 your Cloud database's username and password and then pass the credentials to your client or use the AUTH command after the connection is established.

Create an index

As explained in the in-memory data store quick start guide, Redis allows you to access an item directly via its key. You also learned how to scan the keyspace. Whereby you can use other data structures (e.g., hashes and sorted sets) as secondary indexes, your application would need to maintain those indexes manually. Redis Stack turns Redis into a document database by allowing you to declare which fields are auto-indexed. Redis Stack currently supports secondary index creation on the hashes and JSON documents.

The following example shows an FT.CREATE command that creates an index with some text fields, a numeric field (price), and a tag field (condition). The text fields have a weight of 1.0, meaning they have the same relevancy in the context of full-text searches. The field names follow the JSONPath notion. Each such index field maps to a property within the JSON document.

Any pre-existing JSON documents with a key prefix bicycle: are automatically added to the index. Additionally, any JSON documents with that prefix created or modified after index creation are added or re-added to the index.

Add JSON documents

The example below shows you how to use the JSON.SET command to create new JSON documents:

Search and query using the Redis Query Engine

Wildcard query

You can retrieve all indexed documents using the FT.SEARCH command. Note the LIMIT clause below, which allows result pagination.

Single-term full-text query

The following command shows a simple single-term query for finding all bicycles with a specific model: