Examples of simple query operations with RediSearch
Search is an essential function to derive the value of data. Redis provides inherent, high-speed search capabilities for JSON and Hash Set data.
using StackExchange.Redis;
using NRedisStack;
using NRedisStack.RedisStackCommands;
using NRedisStack.Search;
using NRedisStack.Search.Literals.Enums;
[
{
"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"
}
]
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"
});
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")));
Find all documents for a given index.
foreach (var doc in ft.Search("idx1", new Query("*")).ToJson())
{
Console.WriteLine(doc);
}
{"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"}
Find all documents with a given word in a text field.
foreach (var doc in ft.Search("idx1", new Query("@description:Slippers"))
.ToJson())
{
Console.WriteLine(doc);
}
{"id":46885,"gender":"Boys","season":["Fall"],"description":"Ben 10 Boys Navy Blue Slippers","price":45.99,"city":"Denver","coords":"-104.991531, 39.742043"}
Find all documents with a given phrase in a text field.
foreach (var doc in ft.Search("idx1", new Query("@description:(\"Blue Shirt\")"))
.ToJson())
{
Console.WriteLine(doc);
}
{"id":15970,"gender":"Men","season":["Fall","Winter"],"description":"Turtle Check Men Navy Blue Shirt","price":34.95,"city":"Boston","coords":"-71.057083, 42.361145"}
Find all documents with a numeric field in a given range.
foreach (var doc in ft.Search("idx1", new Query("@price:[40,130]"))
.ToJson())
{
Console.WriteLine(doc);
}
{"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"}
Find all documents that contain a given value in an array field (tag).
foreach (var doc in ft.Search("idx1", new Query("@season:{Spring}"))
.ToJson())
{
Console.WriteLine(doc);
}
{"id":59263,"gender":"Women","season":["Fall","Winter","Spring","Summer"],"description":"Titan Women Silver Watch","price":129.99,"city":"Dallas","coords":"-96.808891, 32.779167"}
Find all documents contain both a numeric field in a range and a word in a text field.
foreach (var doc in ft.Search("idx1", new Query("@price:[40, 100] @description:Blue"))
.ToJson())
{
Console.WriteLine(doc);
}
{"id":46885,"gender":"Boys","season":["Fall"],"description":"Ben 10 Boys Navy Blue Slippers","price":45.99,"city":"Denver","coords":"-104.991531, 39.742043"}
Find all documents that either match tag value or text value.
foreach (var doc in ft.Search("idx1", new Query("(@gender:{Women})|(@city:Boston)"))
.ToJson())
{
Console.WriteLine(doc);
}
{"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"}
Find all documents that do not contain a given word in a text field.
foreach (var doc in ft.Search("idx1", new Query("-(@description:Shirt)"))
.ToJson())
{
Console.WriteLine(doc);
}
{"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"}
Find all documents that have a word that begins with a given prefix value.
foreach (var doc in ft.Search("idx1", new Query("@description:Nav*"))
.ToJson())
{
Console.WriteLine(doc);
}
{"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"}
Find all documents that contain a word that ends with a given suffix value.
foreach (var doc in ft.Search("idx1", new Query("@description:*Watch"))
.ToJson())
{
Console.WriteLine(doc);
}
{"id":59263,"gender":"Women","season":["Fall","Winter","Spring","Summer"],"description":"Titan Women Silver Watch","price":129.99,"city":"Dallas","coords":"-96.808891, 32.779167"}
Find all documents that contain a word that is within 1 Levenshtein distance of a given word.
foreach (var doc in ft.Search("idx1", new Query("@description:%wavy%"))
.ToJson())
{
Console.WriteLine(doc);
}
{"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"}
Find all documents that have geographic coordinates within a given range of a given coordinate. Colorado Springs coords (long, lat) = -104.800644, 38.846127
foreach (var doc in ft.Search("idx1", new Query("@coords:[-104.800644 38.846127 100 mi]"))
.ToJson())
{
Console.WriteLine(doc);
}
{"id":46885,"gender":"Boys","season":["Fall"],"description":"Ben 10 Boys Navy Blue Slippers","price":45.99,"city":"Denver","coords":"-104.991531, 39.742043"}