This repository provides scripts and instructions for generating test data for Redis, importing it into Redis using RedisInsight, and creating a Redis index using RediSearch.
- Node.js installed on your machine
- Redis server running locally or accessible over the network
- RedisInsight installed (optional, for data import)
Track searches in a web site for car brands, store data into Redis db and display most popular brands for the last 30 days.
Execute the following command to generate Redis test data:
node.exe index.js > test.redis
This command will run the index.js script and output the generated test data to a file named test.redis.
Each record of test data generated is imported with following commands:
HSET brandRankMonth:1712420300717 timestamp 1712420300717 brand "Nissan"
EXPIRE brandRankMonth:1712420300717 86400 NX
The first command will set the timestamp field to 1712420300717 and the brand field to "Nissan" within the hash named brandRankMonth:1712420300717.
The second command (EXPIRE) will set the timeout for this hash key to 86400 seconds (1 month).
Import the generated test data into Redis using RedisInsight or any other preferred method.|
To import data using RedisInsight:
- Open RedisInsight and connect to your Redis server.
- Navigate to the "Bulk Actions" tab then "Upload Data".
- Select the test.redis file generated in the previous step.
- Follow the prompts to complete the import process.
Once the test data is imported into Redis, create a Redis index using RediSearch (FT.CREATE). You can use Redis CLI or any Redis client that supports RediSearch commands. For example, to create a simple index:
FT.CREATE rankCarsIdx ON HASH PREFIX 1 brandRankMonth: SCHEMA timestamp NUMERIC brand TEXT
Run a search query on an index and perform aggregate transformations on the results:
FT.AGGREGATE rankCarsIdx "*" GROUPBY 1 @brand REDUCE COUNT 0 AS viewCount SORTBY 4 @viewCount DESC @brand ASC LIMIT 0 10
This will generate following output:
1) "20"
2) 1) "brand"
2) "Seat"
3) "viewCount"
4) "15"
3) 1) "brand"
2) "Hyundai"
3) "viewCount"
4) "14"
4) 1) "brand"
2) "Kia"
3) "viewCount"
4) "13"
5) 1) "brand"
2) "Skoda"
3) "viewCount"
4) "13"
6) 1) "brand"
2) "Volkswagen"
3) "viewCount"
4) "13"
7) 1) "brand"
2) "Audi"
3) "viewCount"
4) "12"
8) 1) "brand"
2) "Ford"
3) "viewCount"
4) "12"
9) 1) "brand"
2) "Honda"
3) "viewCount"
4) "12"
10) 1) "brand"
2) "Mercedes"
3) "viewCount"
4) "11"
11) 1) "brand"
2) "Renault"
3) "viewCount"
4) "11"
List all indexes
FT._LIST
Display info about the index
FT.INFO rankCarsIdx
Delete index:
FT.DROPINDEX rankCarsIdx
# or
FT.DROPINDEX rankCarsIdx DD
- https://redis.io/commands/hset
- https://redis.io/commands/expire
- https://redis.io/commands/ft.create/
- https://redis.io/commands/ft.aggregate/
- https://redis.io/commands/ft._list/
- https://redis.io/commands/ft.info/
- https://redis.io/commands/ft.dropindex/
- https://redis.com/redis-enterprise/redis-insight/
- https://nodejs.org/en/download/