Example - Index and query JSON documents

Learn how to use the Redis query engine with JSON

This example shows how to create a search index for JSON data and run queries against the index.

Make sure that you have Redis Stack and Jedis installed.

Start by importing dependencies:

Connect to the database:

Create some test data to add to the database:

Create an index. In this example, only JSON documents with the key prefix user: are indexed. For more information, see Query syntax.

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:

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.

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

RATE THIS PAGE
Back to top ↑