Skip to content

Latest commit

 

History

History
380 lines (289 loc) · 10.4 KB

BasicQueryOperations.md

File metadata and controls

380 lines (289 loc) · 10.4 KB

Basic Query Operations

Examples of simple query operations with RediSearch

Contents

  1. Business Value Statement
  2. Modules Needed
  3. Data Set
  4. Data Loading
  5. Index Creation
  6. Search Examples
    1. Retrieve All
    2. Single Term Text
    3. Exact Phrase Text
    4. Numeric Range
    5. Tag Array
    6. Logical AND
    7. Logical OR
    8. Negation
    9. Prefix
    10. Suffix
    11. Fuzzy
    12. Geo

Business Value Statement

Search is an essential function to derive the value of data. Redis provides inherent, high-speed search capabilities for JSON and Hash Set data.

Modules Needed

using StackExchange.Redis;
using NRedisStack;
using NRedisStack.RedisStackCommands;
using NRedisStack.Search;
using NRedisStack.Search.Literals.Enums;

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

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

Index Creation

Syntax

FT.CREATE

Command

SearchCommands ft = db.FT();
try {ft.DropIndex("idx1");} catch {};
ft.Create("idx1",   new FTCreateParams().On(IndexDataType.JSON)
                                        .Prefix("product:"),
                                new Schema().AddNumericField(new FieldName("$.id", "id"))
                                            .AddTagField(new FieldName("$.gender", "gender"))
                                            .AddTagField(new FieldName("$.season.*", "season"))
                                            .AddTextField(new FieldName("$.description", "description"))
                                            .AddNumericField(new FieldName("$.price", "price"))
                                            .AddTextField(new FieldName("$.city", "city"))
                                            .AddGeoField(new FieldName("$.coords", "coords")));

Search Examples

Syntax

FT.SEARCH

Retrieve All

Find all documents for a given index.

Command

foreach (var doc in ft.Search("idx1", new Query("*")).ToJson())
{
    Console.WriteLine(doc);
}

Result

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

Single Term Text

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

Command

foreach (var doc in ft.Search("idx1", new Query("@description:Slippers"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

{"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

foreach (var doc in ft.Search("idx1", new Query("@description:(\"Blue Shirt\")"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

{"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

foreach (var doc in ft.Search("idx1", new Query("@price:[40,130]"))
                .ToJson())
{
    Console.WriteLine(doc);
}

Result

{"id":46885,"gender":"Boys","season":["Fall"],"description":"Ben 10 Boys Navy Blue Slippers","price":45.99,"city":"Denver","coords":"-104.991531, 39.742043"}
{"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

foreach (var doc in ft.Search("idx1", new Query("@season:{Spring}"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

{"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

foreach (var doc in ft.Search("idx1", new Query("@price:[40, 100] @description:Blue"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

{"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

foreach (var doc in ft.Search("idx1", new Query("(@gender:{Women})|(@city:Boston)"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

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

Negation

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

Command

foreach (var doc in ft.Search("idx1", new Query("-(@description:Shirt)"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

{"id":46885,"gender":"Boys","season":["Fall"],"description":"Ben 10 Boys Navy Blue Slippers","price":45.99,"city":"Denver","coords":"-104.991531, 39.742043"}
{"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

foreach (var doc in ft.Search("idx1", new Query("@description:Nav*"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

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

Suffix

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

Command

foreach (var doc in ft.Search("idx1", new Query("@description:*Watch"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

{"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

foreach (var doc in ft.Search("idx1", new Query("@description:%wavy%"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

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

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

foreach (var doc in ft.Search("idx1", new Query("@coords:[-104.800644 38.846127 100 mi]"))
                    .ToJson())
{
    Console.WriteLine(doc);
}

Result

{"id":46885,"gender":"Boys","season":["Fall"],"description":"Ben 10 Boys Navy Blue Slippers","price":45.99,"city":"Denver","coords":"-104.991531, 39.742043"}