Skip to content

velizarn/redis-search-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Overview

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.

Prerequisites

  • Node.js installed on your machine
  • Redis server running locally or accessible over the network
  • RedisInsight installed (optional, for data import)

Use case

Track searches in a web site for car brands, store data into Redis db and display most popular brands for the last 30 days.

Steps

1. Generate Redis Test Data

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).

2. Import Test Data into Redis

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.

3. Create Redis Index with RediSearch

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

4. Search for most popupar brands for the last 30 days

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"

Additional commands

List all indexes

FT._LIST

Display info about the index

FT.INFO rankCarsIdx

Delete index:

FT.DROPINDEX rankCarsIdx
# or
FT.DROPINDEX rankCarsIdx DD

Resources

About

RediSearch example with sample test data

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published