Sunday, April 23, 2023

Redis Document/Search - Java Examples

Summary

This post will provide some snippets from Redis Query Workshop available on GitHub.  That workshop covers parallel examples in CLI, Python, Nodejs, Java, and C#.  This post will focus on Java examples.



Basic JSON


no = new JSONObject();
no.put("num1", 1);
no.put("arr2", new JSONArray(Arrays.asList("val1", "val2", "val3")));
jo = new JSONObject();
jo.put("str1", "val1");
jo.put("str2", "val2");
jo.put("arr1", new JSONArray(Arrays.asList(1, 2, 3, 4)));
jo.put("obj1", no);
client.jsonSet("ex2:5", jo);
res = client.jsonGetAsPlainString("ex2:5", new Path("$.obj1.arr2"));
System.out.println(res);
res = client.jsonGetAsPlainString("ex2:5", new Path("$.arr1[1]"));
System.out.println(res);
res = client.jsonGetAsPlainString("ex2:5", new Path("$.obj1.arr2[0:2]"));
System.out.println(res);
res = client.jsonGetAsPlainString("ex2:5", new Path("$.arr1[-2:]"));
System.out.println(res);
[["val1","val2","val3"]]
[2]
["val1","val2"]
[3,4]

Basic Search


Schema schema = new Schema().addNumericField("$.id")
.addTagField("$.gender").as("gender")
.addTagField("$.season.*").as("season")
.addTextField("$.description", 1.0).as("description")
.addNumericField("$.price").as("price")
.addTextField("$.city",1.0).as("city")
.addGeoField("$.coords").as("coords");
IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.JSON)
.setPrefixes(new String[]{"product:"});
client.ftCreate("idx1", IndexOptions.defaultOptions().setDefinition(rule), schema);
private class Product {
private int id;
private String gender;
private String[] season;
private String description;
private double price;
private String city;
private String coords;
public Product(int id, String gender, String[] season,
String description, double price, String city, String coords) {
this.id = id;
this.gender = gender;
this.season = season;
this.description = description;
this.price = price;
this.city = city;
this.coords = coords;
}
}
Product prod15970 = new Product(15970, "Men", new String[]{"Fall, Winter"}, "Turtle Check Men Navy Blue Shirt",
34.95, "Boston", "-71.057083, 42.361145");
Product prod59263 = new Product(59263, "Women", new String[]{"Fall, Winter", "Spring", "Summer"}, "Titan Women Silver Watch",
129.99, "Dallas", "-96.808891, 32.779167");
Product prod46885 = new Product(46885, "Boys", new String[]{"Fall"}, "Ben 10 Boys Navy Blue Slippers",
45.99, "Denver", "-104.991531, 39.742043");
Gson gson = new Gson();
client.jsonSet("product:15970", gson.toJson(prod15970));
client.jsonSet("product:59263", gson.toJson(prod59263));
client.jsonSet("product:46885", gson.toJson(prod46885));
q = new Query("@price:[40, 100] @description:Blue");
res = client.ftSearch("idx1", q);
docs = res.getDocuments();
for (Document doc : docs) {
System.out.println(doc);
}
id:product:46885, score: 1.0, payload:null, properties:[$={"id":59263,"gender":"Boys","season":["Fall"],"description":"Ben 10 Boys Navy Blue Slippers","price":45.99,"city":"Denver","coords":"-104.991531, 39.742043"}]

Advanced JSON


private class Product {
private int id;
private String gender;
private String[] season;
private String description;
private double price;
public Product(int id, String gender, String[] season, String description, double price) {
this.id = id;
this.gender = gender;
this.season = season;
this.description = description;
this.price = price;
}
}
private class Warehouse {
private String city;
private String location;
private Product[] inventory;;
public Warehouse(String city, String location, Product[] inventory) {
this.city = city;
this.location = location;
this.inventory = inventory;
}
}
Product prod15970 = new Product(15970, "Men", new String[]{"Fall", "Winter"}, "Turtle Check Men Navy Blue Shirt",
34.95);
Product prod59263 = new Product(59263, "Women", new String[]{"Fall", "Winter", "Spring", "Summer"}, "Titan Women Silver Watch",
129.99);
Product prod46885 = new Product(46885, "Boys", new String[]{"Fall"}, "Ben 10 Boys Navy Blue Slippers",
45.99);
Warehouse wh1 = new Warehouse("Boston", "42.361145, -71.057083",
new Product[]{prod15970, prod59263, prod46885});
Gson gson = new Gson();
client.jsonSet("warehouse:1", gson.toJson(wh1));
res = client.jsonGetAsPlainString("warehouse:1",
new Path("$.inventory[?(@.description==\"Turtle Check Men Navy Blue Shirt\")]"));
System.out.println(res);
[{"id":15970,"gender":"Men","season":["Fall","Winter"],"description":"Turtle Check Men Navy Blue Shirt","price":34.95}]

Advanced Search


HashMap<String, Object> attr = new HashMap<String, Object>();
attr.put("TYPE", "FLOAT32");
attr.put("DIM", "4");
attr.put("DISTANCE_METRIC", "L2");
Schema schema = new Schema().addVectorField("$.vector", Schema.VectorField.VectorAlgo.FLAT, attr).as("vector");
IndexDefinition rule = new IndexDefinition(IndexDefinition.Type.JSON)
.setPrefixes(new String[]{"vec:"});
client.ftCreate("vss_idx", IndexOptions.defaultOptions().setDefinition(rule), schema);
client.jsonSet("vec:1", new JSONObject("{\"vector\": [1,1,1,1]}"));
client.jsonSet("vec:2", new JSONObject("{\"vector\": [2,2,2,2]}"));
client.jsonSet("vec:3", new JSONObject("{\"vector\": [3,3,3,3]}"));
client.jsonSet("vec:4", new JSONObject("{\"vector\": [4,4,4,4]}"));
float[] vec = new float[]{2,2,3,3};
ByteBuffer buffer = ByteBuffer.allocate(vec.length * Float.BYTES);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.asFloatBuffer().put(vec);
Query q = new Query("*=>[KNN 3 @vector $query_vec]")
.addParam("query_vec", buffer.array())
.setSortBy("__vector_score", true)
.dialect(2);
SearchResult res = client.ftSearch("vss_idx", q);
List<Document> docs = res.getDocuments();
for (Document doc : docs) {
System.out.println(doc);
}
id:vec:2, score: 1.0, payload:null, properties:[$={"vector":[2,2,2,2]}, __vector_score=2]
id:vec:3, score: 1.0, payload:null, properties:[$={"vector":[3,3,3,3]}, __vector_score=2]
id:vec:1, score: 1.0, payload:null, properties:[$={"vector":[1,1,1,1]}, __vector_score=10]

Source


Copyright ©1993-2024 Joey E Whelan, All rights reserved.

Redis Document/Search - C# Examples

Summary

This post will provide some snippets from Redis Query Workshop available on GitHub.  That workshop covers parallel examples in CLI, Python, Nodejs, Java, and C#.  This post will focus on C# examples.



Basic JSON


json.Set("ex3:1", "$", new {field1 = "val1"});
json.Set("ex3:1", "$", new {foo = "bar"});
Console.WriteLine(json.Get(key: "ex3:1",
indent: "\t",
newLine: "\n"
));
{
"foo":"bar"
}

Basic Search


ISearchCommands 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")));
IJsonCommands 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"
});
foreach (var doc in ft.Search("idx1", new Query("@season:{Spring}"))
.Documents.Select(x => x["json"]))
{
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"}

Advanced JSON


IJsonCommands json = db.JSON();
json.Set("warehouse:1", "$", new {
city = "Boston",
location = "42.361145, -71.057083",
inventory = new[] {
new {
id = 15970,
gender = "Men",
season = new[] {"Fall", "Winter"},
description = "Turtle Check Men Navy Blue Shirt",
price = 34.95
},
new {
id = 59263,
gender = "Women",
season = new[] {"Fall", "Winter", "Spring", "Summer"},
description = "Titan Women Silver Watch",
price = 129.99
},
new {
id = 46885,
gender = "Boys",
season = new[] {"Fall"},
description = "Ben 10 Boys Navy Blue Slippers",
price = 45.99
}
}
});
Console.WriteLine(json.Get(key: "warehouse:1",
path: "$.inventory[?(@.price<100)]",
indent: "\t",
newLine: "\n"
));
[
{
"id":15970,
"gender":"Men",
"season":[
"Fall",
"Winter"
],
"description":"Turtle Check Men Navy Blue Shirt",
"price":34.95
},
{
"id":46885,
"gender":"Boys",
"season":[
"Fall"
],
"description":"Ben 10 Boys Navy Blue Slippers",
"price":45.99
}
]

Advanced Search


ISearchCommands ft = db.FT();
try {ft.DropIndex("wh_idx");} catch {};
ft.Create("wh_idx", new FTCreateParams()
.On(IndexDataType.JSON)
.Prefix("warehouse:"),
new Schema().AddTextField(new FieldName("$.city", "city")));
IJsonCommands json = db.JSON();
json.Set("warehouse:1", "$", new {
city = "Boston",
location = "-71.057083, 42.361145",
inventory = new[] {
new {
id = 15970,
gender = "Men",
season = new[] {"Fall", "Winter"},
description = "Turtle Check Men Navy Blue Shirt",
price = 34.95
},
new {
id = 59263,
gender = "Women",
season = new[] {"Fall", "Winter", "Spring", "Summer"},
description = "Titan Women Silver Watch",
price = 129.99
},
new {
id = 46885,
gender = "Boys",
season = new[] {"Fall"},
description = "Ben 10 Boys Navy Blue Slippers",
price = 45.99
}
}
});
json.Set("warehouse:2", "$", new {
city = "Dallas",
location = "-96.808891, 32.779167",
inventory = new[] {
new {
id = 51919,
gender = "Women",
season = new[] {"Summer"},
description = "Nyk Black Horado Handbag",
price = 52.49
},
new {
id = 4602,
gender = "Unisex",
season = new[] {"Fall", "Winter"},
description = "Wildcraft Red Trailblazer Backpack",
price = 50.99
},
new {
id = 37561,
gender = "Girls",
season = new[] {"Spring", "Summer"},
description = "Madagascar3 Infant Pink Snapsuit Romper",
price = 23.95
}
}
});
foreach (var doc in ft.Search("wh_idx",
new Query("@city:(Dallas)")
.ReturnFields(new FieldName("$.inventory[?(@.gender==\"Women\" || @.gender==\"Girls\")]", "result"))
.Dialect(3))
.Documents.Select(x => x["result"]))
{
Console.WriteLine(doc);
}
[{"id":51919,"gender":"Women","season":["Summer"],"description":"Nyk Black Horado Handbag","price":52.49},{"id":37561,"gender":"Girls","season":["Spring","Summer"],"description":"Madagascar3 Infant Pink Snapsuit Romper","price":23.95}]

Source


Copyright ©1993-2024 Joey E Whelan, All rights reserved.

Redis Document/Search - Nodejs Examples

Summary

This post will provide some snippets from Redis Query Workshop available on GitHub.  That workshop covers parallel examples in CLI, Python, Nodejs, Java, and C#.  This post will focus on Nodejs examples.



Basic JSON


await client.json.set('ex3:3', '$', {"obj1": {"str1": "val1", "num2": 2}});
await client.json.set('ex3:3', '$.obj1.num2', 3);
result = await client.json.get('ex3:3');
console.log(result);
{ obj1: { str1: 'val1', num2: 3 } }

Basic Search


await client.ft.create('idx1', {
'$.id': {
type: SchemaFieldTypes.NUMERIC,
AS: 'id'
},
'$.gender': {
type: SchemaFieldTypes.TAG,
AS: 'gender'
},
'$.season.*': {
type: SchemaFieldTypes.TAG,
AS: 'season'
},
'$.description': {
type: SchemaFieldTypes.TEXT,
AS: 'description'
},
'$.price': {
type: SchemaFieldTypes.NUMERIC,
AS: 'price'
},
'$.city': {
type: SchemaFieldTypes.TEXT,
AS: 'city'
},
'$.coords': {
type: SchemaFieldTypes.GEO,
AS: 'coords'
}
}, { ON: 'JSON', PREFIX: 'product:'});
await client.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"});
await client.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"});
await client.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"});
result = await client.ft.search('idx1', '@price:[40,130]');
console.log(JSON.stringify(result, null, 4));
{
"total": 2,
"documents": [
{
"id": "product:46885",
"value": {
"id": 46885,
"gender": "Boys",
"season": [
"Fall"
],
"description": "Ben 10 Boys Navy Blue Slippers",
"price": 45.99,
"city": "Denver",
"coords": "-104.991531, 39.742043"
}
},
{
"id": "product:59263",
"value": {
"id": 59263,
"gender": "Women",
"season": [
"Fall",
"Winter",
"Spring",
"Summer"
],
"description": "Titan Women Silver Watch",
"price": 129.99,
"city": "Dallas",
"coords": "-96.808891, 32.779167"
}
}
]
}

Advanced JSON


await client.json.set('warehouse:1', '$', {
"city": "Boston",
"location": "42.361145, -71.057083",
"inventory":[{
"id": 15970,
"gender": "Men",
"season":["Fall", "Winter"],
"description": "Turtle Check Men Navy Blue Shirt",
"price": 34.95
},{
"id": 59263,
"gender": "Women",
"season": ["Fall", "Winter", "Spring", "Summer"],
"description": "Titan Women Silver Watch",
"price": 129.99
},{
"id": 46885,
"gender": "Boys",
"season": ["Fall"],
"description":
"Ben 10 Boys Navy Blue Slippers",
"price": 45.99
}]});
result = await client.json.get('warehouse:1', { path: '$.inventory[?(@.description=="Turtle Check Men Navy Blue Shirt")]' });
console.log(JSON.stringify(result, null, 4));
[
{
"id": 15970,
"gender": "Men",
"season": [
"Fall",
"Winter"
],
"description": "Turtle Check Men Navy Blue Shirt",
"price": 34.95
}
]

Advanced Search


await client.ft.create('wh_idx', {
'$.city': {
type: SchemaFieldTypes.TEXT,
AS: 'city'
}
}, { ON: 'JSON', PREFIX: 'warehouse:'});
await client.json.set('warehouse:1', '$', {
"city": "Boston",
"location": "-71.057083, 42.361145",
"inventory":[
{
"id": 15970,
"gender": "Men",
"season":["Fall", "Winter"],
"description": "Turtle Check Men Navy Blue Shirt",
"price": 34.95
},
{
"id": 59263,
"gender": "Women",
"season": ["Fall", "Winter", "Spring", "Summer"],
"description": "Titan Women Silver Watch",
"price": 129.99
},
{
"id": 46885,
"gender": "Boys",
"season": ["Fall"],
"description": "Ben 10 Boys Navy Blue Slippers",
"price": 45.99
}
]});
await client.json.set('warehouse:2', '$', {
"city": "Dallas",
"location": "-96.808891, 32.779167",
"inventory": [
{
"id": 51919,
"gender": "Women",
"season":["Summer"],
"description": "Nyk Black Horado Handbag",
"price": 52.49
},
{
"id": 4602,
"gender": "Unisex",
"season": ["Fall", "Winter"],
"description": "Wildcraft Red Trailblazer Backpack",
"price": 50.99
},
{
"id": 37561,
"gender": "Girls",
"season": ["Spring", "Summer"],
"description": "Madagascar3 Infant Pink Snapsuit Romper",
"price": 23.95
}
]});
result = await client.ft.search('wh_idx', '@city:(Dallas)', {
RETURN: ['$.inventory[?(@.gender=="Women" || @.gender=="Girls")]'],
DIALECT: 3
});
console.log(JSON.stringify(result, null, 4));
{
"total": 1,
"documents": [
{
"id": "warehouse:2",
"value": {
"$.inventory[?(@.gender==\"Women\" || @.gender==\"Girls\")]": "[{\"id\":51919,\"gender\":\"Women\",\"season\":[\"Summer\"],\"description\":\"Nyk Black Horado Handbag\",\"price\":52.49},{\"id\":37561,\"gender\":\"Girls\",\"season\":[\"Spring\",\"Summer\"],\"description\":\"Madagascar3 Infant Pink Snapsuit Romper\",\"price\":23.95}]"
}
}
]
}

Source


Copyright ©1993-2024 Joey E Whelan, All rights reserved.

Redis Document/Search - Python Examples

Summary

This post will provide some snippets from Redis Query Workshop available on GitHub.  That workshop covers parallel examples in CLI, Python, Nodejs, Java, and C#.  This post will focus on Python examples.



Basic JSON


client.json().set('ex2:5', '$', {"str1": "val1", "str2": "val2", "arr1":[1,2,3,4], "obj1": {"num1": 1,"arr2":["val1","val2", "val3"]}})
result = client.json().get('ex2:5', Path('$.obj1.arr2'))
print(result)
result = client.json().get('ex2:5', Path('$.arr1[1]' ))
print(result)
result = client.json().get('ex2:5', Path('$.obj1.arr2[0:2]'))
print(result)
result = client.json().get('ex2:5', Path('$.arr1[-2:]'))
print(result)
[['val1', 'val2', 'val3']]
[2]
['val1', 'val2']
[3, 4]

Basic Search


idx_def = IndexDefinition(index_type=IndexType.JSON, prefix=['product:'])
schema = [
NumericField('$.id', as_name='id'),
TagField('$.gender', as_name='gender'),
TagField('$.season.*', as_name='season'),
TextField('$.description', as_name='description'),
NumericField('$.price', as_name='price'),
TextField('$.city', as_name='city'),
GeoField('$.coords', as_name='coords')
]
result = client.ft('idx1').create_index(schema, definition=idx_def)
client.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"})
client.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"})
client.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"})
query = Query('@description:("Blue Shirt")')
result = client.ft('idx1').search(query)
print(result)
Result{1 total, docs: [Document {'id': 'product:15970', 'payload': None, 'json': '{"id":15970,"gender":"Men","season":["Fall","Winter"],"description":"Turtle Check Men Navy Blue Shirt","price":34.95,"city":"Boston","coords":"-71.057083, 42.361145"}'}]}

Advanced JSON


client.json().set('warehouse:1', '$', {
"city": "Boston",
"location": "42.361145, -71.057083",
"inventory":[{
"id": 15970,
"gender": "Men",
"season":["Fall", "Winter"],
"description": "Turtle Check Men Navy Blue Shirt",
"price": 34.95
},{
"id": 59263,
"gender": "Women",
"season": ["Fall", "Winter", "Spring", "Summer"],
"description": "Titan Women Silver Watch",
"price": 129.99
},{
"id": 46885,
"gender": "Boys",
"season": ["Fall"],
"description":
"Ben 10 Boys Navy Blue Slippers",
"price": 45.99
}]})
result = client.json().get('warehouse:1', Path('$.inventory[?(@.description=="Turtle Check Men Navy Blue Shirt")]'))
print(json.dumps(result, indent=4))
[
{
"id": 15970,
"gender": "Men",
"season": [
"Fall",
"Winter"
],
"description": "Turtle Check Men Navy Blue Shirt",
"price": 34.95
}
]

Advanced Search


schema = [VectorField('$.vector', 'FLAT', { "TYPE": 'FLOAT32', "DIM": 4, "DISTANCE_METRIC": 'L2'}, as_name='vector')]
idx_def: IndexDefinition = IndexDefinition(index_type=IndexType.JSON, prefix=['vec:'])
result = client.ft('vss_idx').create_index(schema, definition=idx_def)
client.json().set('vec:1', '$', {'vector': [1,1,1,1]})
client.json().set('vec:2', '$', {'vector': [2,2,2,2]})
client.json().set('vec:3', '$', {'vector': [3,3,3,3]})
client.json().set('vec:4', '$', {'vector': [4,4,4,4]})
vec = [2,2,3,3]
query_vector = np.array(vec, dtype=np.float32).tobytes()
q_str = '*=>[KNN 3 @vector $query_vec]'
q = Query(q_str)\
.sort_by('__vector_score')\
.dialect(2)
params_dict = {"query_vec": query_vector}
results = client.ft('vss_idx').search(q, query_params=params_dict)
print(results)
Result{3 total, docs: [Document {'id': 'vec:2', 'payload': None, '__vector_score': '2', 'json': '{"vector":[2,2,2,2]}'}, Document {'id': 'vec:3', 'payload': None, '__vector_score': '2', 'json': '{"vector":[3,3,3,3]}'}, Document {'id': 'vec:1', 'payload': None, '__vector_score': '10', 'json': '{"vector":[1,1,1,1]}'}]}

Source


Copyright ©1993-2024 Joey E Whelan, All rights reserved.

Redis Document/Search - CLI Examples

Summary

This post will provide some snippets from Redis Query Workshop available on Github.  That workshop covers parallel examples in CLI, Python, Nodejs, Java, and C#.  This post will focus on the CLI examples.



Basic JSON


JSON.SET ex2:5 $ '{"str1": "val1", "str2": "val2", "arr1":[1,2,3,4], "obj1": {"num1": 1,"arr2":["val1","val2", "val3"]}}'
JSON.GET ex2:5 $.obj1.arr2
JSON.GET ex2:5 $.arr1[1]
JSON.GET ex2:5 $.obj1.arr2[0:2]
JSON.GET ex2:5 $.arr1[-2:]
"[[\"val1\",\"val2\",\"val3\"]]"
"[2]"
"[\"val1\",\"val2\"]"
"[3,4]"

Basic Search


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
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"}'
FT.SEARCH idx1 '@description:Slippers'
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\"}"

Advanced JSON


JSON.SET warehouse:1 $ '{"city": "Boston","location": "42.361145, -71.057083","inventory":[{"id": 15970,"gender": "Men","season":["Fall", "Winter"],"description": "Turtle Check Men Navy Blue Shirt","price": 34.95},{"id": 59263,"gender": "Women","season": ["Fall", "Winter", "Spring", "Summer"],"description": "Titan Women Silver Watch","price": 129.99},{"id": 46885,"gender": "Boys","season": ["Fall"],"description": "Ben 10 Boys Navy Blue Slippers","price": 45.99}]}'
JSON.GET warehouse:1 '$.inventory[*].price'
"[34.95,129.99,45.99]"

Advanced Search


FT.CREATE wh_idx ON JSON PREFIX 1 warehouse: SCHEMA $.city as city TEXT
JSON.SET warehouse:1 $ '{"city": "Boston","location": "-71.057083, 42.361145","inventory":[{"id": 15970,"gender": "Men","season":["Fall", "Winter"],"description": "Turtle Check Men Navy Blue Shirt","price": 34.95},{"id": 59263,"gender": "Women","season": ["Fall", "Winter", "Spring", "Summer"],"description": "Titan Women Silver Watch","price": 129.99},{"id": 46885,"gender": "Boys","season": ["Fall"],"description": "Ben 10 Boys Navy Blue Slippers","price": 45.99}]}'
JSON.SET warehouse:2 $ '{"city": "Dallas","location": "-96.808891, 32.779167","inventory": [{"id": 51919,"gender": "Women","season":["Summer"],"description": "Nyk Black Horado Handbag","price": 52.49},{"id": 4602,"gender": "Unisex","season": ["Fall", "Winter"],"description": "Wildcraft Red Trailblazer Backpack","price": 50.99},{"id": 37561,"gender": "Girls","season": ["Spring", "Summer"],"description": "Madagascar3 Infant Pink Snapsuit Romper","price": 23.95}]}'
FT.SEARCH wh_idx '@city:(Boston)' RETURN 1 '$.inventory[?(@.price>50)].id' DIALECT 3
1) "1"
2) "warehouse:1"
3) 1) "$.inventory[?(@.price>50)].id"
2) "[59263]"

Source

Redis Search - Card Transactions Example

Summary

I'll be demonstrating Redis Search capabilities in a credit card transaction domain.  All the data will be synthetically generated from the Faker module.  Data will be stored as Hash sets in Redis.  Subsequently, Redis Search will be leveraged to generate analytics on the data.

Architecture


Code Snippets

Data Generation


merchants_provider = DynamicProvider(
provider_name='merchants',
elements=['Walmart', 'Nordstrom', 'Amazon', 'Exxon', 'Kroger', 'Safeway', 'United Airlines', 'Office Depot', 'Ford', 'Taco Bell']
)
categories_provider = DynamicProvider(
provider_name='categories',
elements= ['AUTO', 'FOOD', 'GASS', 'GIFT', 'TRAV', 'GROC', 'HOME', 'PERS', 'HEAL', 'MISC']
)
def generate_data(client, count):
Faker.seed(0)
random.seed(0)
fake = Faker()
fake.add_provider(merchants_provider)
fake.add_provider(categories_provider)
for i in range(count):
tdate = fake.date_time_between(start_date='-3y', end_date='now')
txn_record = {
'acct_id': int(fake.ean(length=13)),
'txn_id': int(fake.ean(length=13)),
'txn_date': re.escape(tdate.isoformat()),
'txn_timestamp': time.mktime(tdate.timetuple()),
'card_last_4': fake.credit_card_number()[-4:],
'txn_amt': round(random.uniform(1, 1000), 2),
'txn_currency': 'USD',
'expense_category': fake.categories(),
'merchant_name': fake.merchants(),
'merchant_address': re.escape(fake.address())
}
client.hset(f'{PREFIX}{txn_record["txn_id"]}', mapping=txn_record)

Index Creation


try:
client.ft(IDX_NAME).dropindex()
except:
pass
idx_def = IndexDefinition(index_type=IndexType.HASH, prefix=[PREFIX])
schema = [
TagField('txn_id', sortable=True),
TextField('txn_date'),
NumericField('txn_timestamp', sortable=True),
NumericField('txn_amt'),
TagField('txn_currency'),
TagField('expense_category'),
TextField('merchant_name'),
TextField('merchant_address')
]
client.ft(IDX_NAME).create_index(schema, definition=idx_def)
view raw cctxn_index.py hosted with ❤ by GitHub

Sample Query

The query below aggregates total spend by category for those transactions with a dollar value >$500 in Dec 2021.

request = AggregateRequest('(@txn_date:2021\-12* @txn_currency:{USD} @txn_amt:[(500, inf])')\
.group_by('@expense_category', reducers.sum('@txn_amt').alias('total_spend'))\
.sort_by(Desc('@total_spend'))
result = client.ft(IDX_NAME).aggregate(request)
view raw cctxn_search.py hosted with ❤ by GitHub

Source


Copyright ©1993-2024 Joey E Whelan, All rights reserved.