redis-on-azure-workshop

RediSearch in Azure Cache for Redis

To store data in Redis, we first define a key and then set the value. For example, to add a Redis Hash:

hset participant-id:0001 name satya

If we know the key ahead of time, this works great for retrieving a value.

However, what about scenarios where there’s data in the cache that needs to be searched or aggregated? For example, a product, stores or transactions by a particular vendor?

Typically this is where a key/value model starts to show its limitations. To search or carry out processing, large transfers of data may be required by a client application.

RediSearch adds search capabilities to Redis and includes many other powerful features, and it does so right where your data lives. This means you can process much larger volumes of data and at greater speed.

Learning Objective

Prerequisites

Data Set

[
    {   
        "id": 15970,
        "gender": "Men",
        "season":["Fall", "Winter"],
        "description": "Turtle Check Men Navy Blue Shirt",
        "price": 34.95,
        "city": "Boston",
        "location": "42.361145, -71.057083"
    },
    {
        "id": 59263,
        "gender": "Women",
        "season": ["Fall", "Winter", "Spring", "Summer"],
        "description": "Titan Women Silver Watch",
        "price": 129.99,
        "city": "Dallas",
        "location": "32.779167, -96.808891"
    },
    {
        "id": 46885,
        "gender": "Boys",
        "season": ["Fall"],
        "description": "Ben 10 Boys Navy Blue Slippers",
        "price": 45.99,
        "city": "Denver",
        "location": "39.742043, -104.991531"
    }
]

Data Loading

JSON.SET product:15970 $ '{"id": 15970, "gender": "Men", "season":["Fall", "Winter"], "description": "Turtle Check Men Navy Blue Shirt", "price": 34.95, "city": "Boston", "coords": "-71.057083, 42.361145"}'
JSON.SET product:59263 $ '{"id": 59263, "gender": "Women", "season":["Fall", "Winter", "Spring", "Summer"],"description": "Titan Women Silver Watch", "price": 129.99, "city": "Dallas", "coords": "-96.808891, 32.779167"}'
JSON.SET product:46885 $ '{"id": 46885, "gender": "Boys", "season":["Fall"], "description": "Ben 10 Boys Navy Blue Slippers", "price": 45.99, "city": "Denver", "coords": "-104.991531, 39.742043"}'

Index Creation

Syntax

FT.CREATE

Command

FT.CREATE idx1 ON JSON PREFIX 1 product: SCHEMA $.id as id NUMERIC $.gender as gender TAG $.season.* AS season TAG $.description AS description TEXT $.price AS price NUMERIC $.city AS city TEXT $.coords AS coords GEO

Result

"OK"

Search Examples

Syntax

FT.SEARCH

Retrieve All

Find all documents for a given index.

Command

FT.SEARCH idx1 *

Result

1) "3"
2) "product:46885"
3) 1) "$"
   2) "{\"id\":46885,\"gender\":\"Boys\",\"season\":[\"Fall\"],\"description\":\"Ben 10 Boys Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"coords\":\"-104.991531, 39.742043\"}"
4) "product:59263"
5) 1) "$"
   2) "{\"id\":59263,\"gender\":\"Women\",\"season\":[\"Fall\",\"Winter\",\"Spring\",\"Summer\"],\"description\":\"Titan Women Silver Watch\",\"price\":129.99,\"city\":\"Dallas\",\"coords\":\"-96.808891, 32.779167\"}"
6) "product:15970"
7) 1) "$"
   2) "{\"id\":15970,\"gender\":\"Men\",\"season\":[\"Fall\",\"Winter\"],\"description\":\"Turtle Check Men Navy Blue Shirt\",\"price\":34.95,\"city\":\"Boston\",\"coords\":\"-71.057083, 42.361145\"}"

Single Term Text

Find all documents with a given word in a text field.

Command

FT.SEARCH idx1 '@description:Slippers'

Result

1) "1"
2) "product:46885"
3) 1) "$"
   2) "{\"id\":46885,\"gender\":\"Boys\",\"season\":[\"Fall\"],\"description\":\"Ben 10 Boys Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"coords\":\"-104.991531, 39.742043\"}"

Exact Phrase Text

Find all documents with a given phrase in a text field.

Command

FT.SEARCH idx1 '@description:("Blue Shirt")'

Result

1) "1"
2) "product:15970"
3) 1) "$"
   2) "{\"id\":15970,\"gender\":\"Men\",\"season\":[\"Fall\",\"Winter\"],\"description\":\"Turtle Check Men Navy Blue Shirt\",\"price\":34.95,\"city\":\"Boston\",\"coords\":\"-71.057083, 42.361145\"}"

Numeric Range

Find all documents with a numeric field in a given range.

Command

FT.SEARCH idx1 '@price:[40,130]'

Result

1) "2"
2) "product:46885"
3) 1) "$"
   2) "{\"id\":46885,\"gender\":\"Boys\",\"season\":[\"Fall\"],\"description\":\"Ben 10 Boys Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"coords\":\"-104.991531, 39.742043\"}"
4) "product:59263"
5) 1) "$"
   2) "{\"id\":59263,\"gender\":\"Women\",\"season\":[\"Fall\",\"Winter\",\"Spring\",\"Summer\"],\"description\":\"Titan Women Silver Watch\",\"price\":129.99,\"city\":\"Dallas\",\"coords\":\"-96.808891, 32.779167\"}"

Tag Array

Find all documents that contain a given value in an array field (tag).

Command

FT.SEARCH idx1 '@season:{Spring}'

Result

1) "1"
2) "product:59263"
3) 1) "$"
   2) "{\"id\":59263,\"gender\":\"Women\",\"season\":[\"Fall\",\"Winter\",\"Spring\",\"Summer\"],\"description\":\"Titan Women Silver Watch\",\"price\":129.99,\"city\":\"Dallas\",\"coords\":\"-96.808891, 32.779167\"}"

Logical AND

Find all documents contain both a numeric field in a range and a word in a text field.

Command

FT.SEARCH idx1 '@price:[40, 100] @description:Blue'

Result

1) "1"
2) "product:46885"
3) 1) "$"
   2) "{\"id\":46885,\"gender\":\"Boys\",\"season\":[\"Fall\"],\"description\":\"Ben 10 Boys Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"coords\":\"-104.991531, 39.742043\"}"

Logical OR

Find all documents that either match tag value or text value.

Command

FT.SEARCH idx1 '(@gender:{Women})|(@city:Boston)'

Result

1) "2"
2) "product:59263"
3) 1) "$"
   2) "{\"id\":59263,\"gender\":\"Women\",\"season\":[\"Fall\",\"Winter\",\"Spring\",\"Summer\"],\"description\":\"Titan Women Silver Watch\",\"price\":129.99,\"city\":\"Dallas\",\"coords\":\"-96.808891, 32.779167\"}"
4) "product:15970"
5) 1) "$"
   2) "{\"id\":15970,\"gender\":\"Men\",\"season\":[\"Fall\",\"Winter\"],\"description\":\"Turtle Check Men Navy Blue Shirt\",\"price\":34.95,\"city\":\"Boston\",\"coords\":\"-71.057083, 42.361145\"}"

Negation

Find all documents that do not contain a given word in a text field.

Command

FT.SEARCH idx1 '-(@description:Shirt)'

Result

1) "2"
2) "product:46885"
3) 1) "$"
   2) "{\"id\":46885,\"gender\":\"Boys\",\"season\":[\"Fall\"],\"description\":\"Ben 10 Boys Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"coords\":\"-104.991531, 39.742043\"}"
4) "product:59263"
5) 1) "$"
   2) "{\"id\":59263,\"gender\":\"Women\",\"season\":[\"Fall\",\"Winter\",\"Spring\",\"Summer\"],\"description\":\"Titan Women Silver Watch\",\"price\":129.99,\"city\":\"Dallas\",\"coords\":\"-96.808891, 32.779167\"}"

Prefix

Find all documents that have a word that begins with a given prefix value.

Command

FT.SEARCH idx1 '@description:Nav*'

Result

1) "2"
2) "product:46885"
3) 1) "$"
   2) "{\"id\":46885,\"gender\":\"Boys\",\"season\":[\"Fall\"],\"description\":\"Ben 10 Boys Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"coords\":\"-104.991531, 39.742043\"}"
4) "product:15970"
5) 1) "$"
   2) "{\"id\":15970,\"gender\":\"Men\",\"season\":[\"Fall\",\"Winter\"],\"description\":\"Turtle Check Men Navy Blue Shirt\",\"price\":34.95,\"city\":\"Boston\",\"coords\":\"-71.057083, 42.361145\"}"

Suffix

Find all documents that contain a word that ends with a given suffix value.

Command

FT.SEARCH idx1 '@description:*Watch'

Result

1) "1"
2) "product:59263"
3) 1) "$"
   2) "{\"id\":59263,\"gender\":\"Women\",\"season\":[\"Fall\",\"Winter\",\"Spring\",\"Summer\"],\"description\":\"Titan Women Silver Watch\",\"price\":129.99,\"city\":\"Dallas\",\"coords\":\"-96.808891, 32.779167\"}"

Fuzzy

Find all documents that contain a word that is within 1 Levenshtein distance of a given word.

Command

FT.SEARCH idx1 '@description:%wavy%'

Result

1) "2"
2) "product:46885"
3) 1) "$"
   2) "{\"id\":46885,\"gender\":\"Boys\",\"season\":[\"Fall\"],\"description\":\"Ben 10 Boys Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"coords\":\"-104.991531, 39.742043\"}"
4) "product:15970"
5) 1) "$"
   2) "{\"id\":15970,\"gender\":\"Men\",\"season\":[\"Fall\",\"Winter\"],\"description\":\"Turtle Check Men Navy Blue Shirt\",\"price\":34.95,\"city\":\"Boston\",\"coords\":\"-71.057083, 42.361145\"}"

Geo

Find all documents that have geographic coordinates within a given range of a given coordinate. Colorado Springs coords (long, lat) = -104.800644, 38.846127

Command

FT.SEARCH idx1 '@coords:[-104.800644 38.846127 100 mi]'

Result

1) "1"
2) "product:46885"
3) 1) "$"
   2) "{\"id\":46885,\"gender\":\"Boys\",\"season\":[\"Fall\"],\"description\":\"Ben 10 Boys Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"coords\":\"-104.991531, 39.742043\"}"

Additional Resources

  1. RediSearch documentation
  2. RediSearch query syntax
  3. Redis JSON and Search Resources in GitHub