Redis Software supports automatic, real-time indexing for both Hash and JSON documents using Redis Search 2.0 and later. Any data written with HSET or JSON.SET is indexed automatically when it matches your schema’s key prefix. This article explains how indexing works in Redis Software clusters and provides CLI and REST API examples, along with common issues and how to resolve them.
Covers: Indexing in Redis Software • CLI and REST Examples • Common Issues
Prerequisites
- Redis Software cluster with Redis Search (and optionally RedisJSON) enabled
- Access to
redis-cli - Module versions ≥ 2.0
How Indexing Works
Redis Software nodes index documents locally and synchronize index segments across shards and replicas. New documents are indexed synchronously, while preexisting data is indexed asynchronously when you create a new index.
Key points
-
FT.ADDis deprecated in Redis Search 2.x and should not be used - Indexes follow defined prefixes across all shards
- Schema changes require dropping and recreating the index
Step-by-Step Setup
1. Create an Index
CLI Example
FT.CREATE idx:employees ON JSON PREFIX 1 emp: SCHEMA $.name AS name TEXT $.dept AS dept TAG $.age AS age NUMERIC
REST API Example
curl -k -u admin:<password> -X POST \
https://<cluster>:9443/v1/redis/commands \
-d '{"cmd":"FT.CREATE idx:employees ON JSON PREFIX 1 emp: SCHEMA $.name AS name TEXT $.dept AS dept TAG $.age AS age NUMERIC"}'2. Add Documents
JSON Example
JSON.SET emp:1 $ '{"name":"Alice","dept":"Engineering","age":30}'
JSON.SET emp:2 $ '{"name":"Bob","dept":"Finance","age":25}'Hash Example
HSET hemp:1 name "Alice" dept "Engineering" age 30 HSET hemp:2 name "Bob" dept "Finance" age 25
3. Query the Index
FT.SEARCH idx:employees "@dept:{Engineering}"
FT.SEARCH idx:employees "@age:[25 35]"Troubleshooting
| Issue | Resolution |
|---|---|
| Documents missing from search | Confirm the key prefix matches the index definition and schema fields. |
| Delayed indexing after creation | Allow background indexing to complete for preexisting keys. |
| Cross-shard mismatch | Verify consistent prefixes and that the index was created successfully. |
0 comments
Please sign in to leave a comment.