Index and query documents

Learn how to use the Redis query engine with JSON and hash documents.

This example shows how to create a search index for JSON documents and run queries against the index. It then goes on to show the slight differences in the equivalent code for hash documents.

Initialize

Make sure that you have Redis Community Edition or another Redis server available. Also install the redis-py client library if you haven't already done so.

Add the following dependencies. All of them are applicable to both JSON and hash, except for the Path class, which is specific to JSON (see Path for a description of the JSON path syntax).

Create data

Create some test data to add to your database. The example data shown below is compatible with both JSON and hash objects.

Add the index

Connect to your Redis database. The code below shows the most basic connection but see Connect to the server to learn more about the available connection options.

Create an index for the JSON data. The code below specifies that only JSON documents with the key prefix user: are indexed. For more information, see Query syntax.

Add the data

Add the three sets of user data to the database as JSON objects. If you use keys with the user: prefix then Redis will index the objects automatically as you add them:

Query the data

You can now use the index to search the JSON objects. The query below searches for objects that have the text "Paul" in any field and have an age value in the range 30 to 40:

Specify query options to return only the city field:

Use an aggregation query to count all users in each city.

Differences with hash documents

Indexing for hash documents is very similar to JSON indexing but you need to specify some slightly different options.

When you create the schema for a hash index, you don't need to add aliases for the fields, since you use the basic names to access the fields anyway. Also, you must use HASH for the IndexType when you create the index. The code below shows these changes with a new index called hash-idx:users, which is otherwise the same as the idx:users index used for JSON documents in the previous examples.

You use hset() to add the hash documents instead of json().set(), but the same flat userX dictionaries work equally well with either hash or JSON:

The query commands work the same here for hash as they do for JSON (but the name of the hash index is different). The format of the result is almost the same except that the fields are returned directly in the result Document object instead of in an enclosing json dictionary:

More information

See the Redis query engine docs for a full description of all query features with examples.

RATE THIS PAGE
Back to top ↑