RedisSearch - Release - 2.0.0-M1

Hi,

Let me give you a small example about the new automatic indexing of hashes in RedisSearch 2.0

1- Start RediSearch docker container

> docker run -it --rm --name redis-search-2 \
     -p 6379:6379 \
     redislabs/redisearch:1.99.1

2- Add some data
For example let’s create few “movies”:


> HSET movies:1002 title "Star Wars: Episode V - The Empire Strikes Back" plot "After the Rebels are brutally overpowered by the Empire on the ice planet Hoth, Luke Skywalker begins Jedi training with Yoda, while his friends are pursued by Darth Vader and a bounty hunter named Boba Fett all over the galaxy."  release_year 1980 genre "Action" rating 8.7 nb_of_votes 1127635 imdb_id tt0080684

> HSET movies:1003 title "The Godfather" plot "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son."  release_year 1972 genre "Drama" rating 9.2 nb_of_votes 1563839 imdb_id tt0068646

> HSET movies:1004 title "Heat" plot "A group of professional bank robbers start to feel the heat from police when they unknowingly leave a clue at their latest heist."  release_year 1995 genre "Thriller" rating 8.2 nb_of_votes 559490 imdb_id tt0113277

The key and fields of the hashes are:

  • movie_id : The unique ID of the movie, internal to this database
  • title : The title of the movie.
  • plot : A summary of the movie.
  • genre : The genre of the movie, for now a movie will only h ave one single genre.
  • release_year : The year the movie has been released as a numerical value.
  • rating : The ratings from the public numerical value.
  • nb_of_votes : Number of votes.
  • imdb_id : Id in IMDn id of the movie.

3- Create an Index

So you have 3 movies in your database, and you want to be able to search on title, release_year, rating and genre so you can create an index with the following command:

> FT.CREATE idx:movies ON hash PREFIX 1  "movies:" SCHEMA title TEXT SORTABLE release_year NUMERIC SORTABLE rating NUMERIC SORTABLE genre TAG SORTABLE

  • FT.CREATE : the command that allows you to create a new index

  • idx:movies : the name of the index

  • ON hash : the type of structure to be indexed. Note that in RediSearch 2.0 only hash structure are supported, this is parameter will allow RediSearch to index other structure in the future

  • PREFIX 1 "movies:" : the prefix of the keys that should be index. This is a list, so since we want to only index movies:* keys the number is 1. Suppose you want to index movies and tv_show that have the same fields, you can use: PREFIX 2 "movies:" "tv_shows:"

  • SCHEMA ...: define the schema (field and their type) to index, as you can see in the command, we are using TEXT, NUMERIC and TAG, and SORTABLE parameter; let’s explain the detail later when you run queries.

You can use the following commands to list the indices and look into it:

> FT._LIST 

> FT.INFO "idx:movies"

If you look at the information returned by the info command you can see that the “movies:*” have been indexes. ( num_docs = 3 ).

4- Querying/Searching the data.

You can find some query examples below:

> FT.SEARCH idx:movies "star war" RETURN 2 title release_year

> FT.SEARCH idx:movies * FILTER release_year 1970 1980  RETURN 2 title release_year

5- Add a new movie:

> HSET movies:1005 title "The Exorcist" plot "When a 12 year-old girl is possessed by a mysterious entity, her mother seeks the help of two priests to save her."  release_year 1973 genre "Horror" rating 8.0 nb_of_votes 352898 imdb_id tt0070047

Then query it:

> FT.SEARCH idx:movies * FILTER release_year 1970 1980  RETURN 2 title release_year SORTBY release_year

The new movie is automatically indexed.

6- Delete a movie:

Let’s delete “The Godfather” movie

> DEL movies:1003

> FT.SEARCH idx:movies * FILTER release_year 1970 1980  RETURN 2 title release_year SORTBY release_year

When deleting the hash, the index is updated automatically

6- Delete the index.

Let’s now delete the index without deleting the hashes using the new command FT.DELETE

> FT.DELETE idx:movies

> FT._LIST

This is it! for this quick intro to RediSearch 2.0, you can look at the documentation and select the version in the right menu to have more information about this new release and how to use it

Regards
Tug

3 Likes