Skip to content

Commit 5ccb704

Browse files
shacharPashchayimJeevananthan-23
authored
Support Pipeline & Transaction + Examples (#68)
Co-authored-by: Chayim <[email protected]> Co-authored-by: shacharPash <[email protected]> Co-authored-by: Jeevananthan <[email protected]> Co-authored-by: Chayim I. Kirshen <[email protected]>
1 parent 1c26a8c commit 5ccb704

File tree

12 files changed

+716
-17
lines changed

12 files changed

+716
-17
lines changed

Examples/AsyncExample.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
# Async Example
3+
4+
## All methods have sync and async implementations. The async methods end with the suffix Async(...), and are fully await-able. See the example below:
5+
6+
Connect to a Redis server, and retrieve an instance that can run JSON commands:
7+
8+
```csharp
9+
var redis = await ConnectionMultiplexer.ConnectAsync("localhost");
10+
var db = redis.GetDatabase();
11+
var json = db.JSON();
12+
```
13+
14+
Store and retrieve data, async:
15+
16+
```csharp
17+
await json.SetAsync("key", "$", new { name = "John", age = 30, city = "New York" });
18+
var john = await json.GetAsync("key");
19+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Combination modules Pipeline
2+
3+
## An example of pipelines mixing a pipeline with a combination of module commands with JSON & Search
4+
5+
Connect to the Redis server:
6+
7+
```csharp
8+
var redis = ConnectionMultiplexer.Connect("localhost");
9+
```
10+
11+
Setup pipeline connection
12+
13+
```csharp
14+
var db = redis.GetDatabase();
15+
var pipeline = new Pipeline(db);
16+
```
17+
18+
## JSON
19+
20+
Add JSON data to the pipeline.
21+
22+
```csharp
23+
pipeline.Json.SetAsync("person:01", "$", new { name = "John", age = 30, city = "New York" });
24+
pipeline.Json.SetAsync("person:02", "$", new { name = "Joy", age = 25, city = "Los Angeles" });
25+
pipeline.Json.SetAsync("person:03", "$", new { name = "Mark", age = 21, city = "Chicago" });
26+
pipeline.Json.SetAsync("person:04", "$", new { name = "Steve", age = 24, city = "Phoenix" });
27+
pipeline.Json.SetAsync("person:05", "$", new { name = "Michael", age = 55, city = "San Antonio" });
28+
```
29+
30+
## Search
31+
32+
Create the schema to index name as text field, age as a numeric field and city as tag field.
33+
34+
```csharp
35+
var schema = new Schema().AddTextField("name").AddNumericField("age", true).AddTagField("city");
36+
```
37+
38+
Create a search index, that only retrieves JSON objects from keys prefixed *person*.
39+
40+
```csharp
41+
var parameters = FTCreateParams.CreateParams().On(IndexDataType.JSON).Prefix("person:");
42+
```
43+
44+
Create a search index, on our stored data:
45+
46+
```csharp
47+
pipeline.Ft.CreateAsync("person-idx", parameters, schema);
48+
```
49+
50+
Execute the pipeline
51+
52+
```csharp
53+
pipeline.Execute();
54+
```
55+
56+
Search for all indexed person records
57+
58+
```csharp
59+
var getAllPersons = db.FT().SearchAsync("person-idx", new Query());
60+
```

Examples/HsetAndSearch.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# HSET and Search
2+
## An example of mixing Redis open source command (HSET) with Redis Stack Redis commands (FT.CREATE & FT.SEARCH)
3+
4+
Connect to the Redis server:
5+
```csharp
6+
var redis = ConnectionMultiplexer.Connect("localhost");
7+
```
8+
Get a reference to the database and for search commands:
9+
```csharp
10+
var db = redis.GetDatabase();
11+
var ft = db.FT();
12+
```
13+
Use HSET to add a field-value pair to a hash:
14+
```csharp
15+
db.HashSet("profesor:5555", new HashEntry[] { new("first", "Albert"), new("last", "Blue"), new("age", "55") });
16+
db.HashSet("student:1111", new HashEntry[] { new("first", "Joe"), new("last", "Dod"), new("age", "18") });
17+
db.HashSet("pupil:2222", new HashEntry[] { new("first", "Jen"), new("last", "Rod"), new("age", "14") });
18+
db.HashSet("student:3333", new HashEntry[] { new("first", "El"), new("last", "Mark"), new("age", "17") });
19+
db.HashSet("pupil:4444", new HashEntry[] { new("first", "Pat"), new("last", "Shu"), new("age", "21") });
20+
db.HashSet("student:5555", new HashEntry[] { new("first", "Joen"), new("last", "Ko"), new("age", "20") });
21+
db.HashSet("teacher:6666", new HashEntry[] { new("first", "Pat"), new("last", "Rod"), new("age", "20") });
22+
```
23+
24+
Create the schema indexing the text fields ```first``` and ```last```, and ```age``` as a numeric field:
25+
```csharp
26+
var schema = new Schema().AddTextField("first").AddTextField("last").AddNumericField("age");
27+
```
28+
Filter the index to only include hashes with an age greater than 16, and prefix of 'student:' or 'pupil:'
29+
```csharp
30+
var parameters = FTCreateParams.CreateParams().Filter("@age>16").Prefix("student:", "pupil:");
31+
```
32+
Create the index:
33+
```csharp
34+
ft.Create("example_index", parameters, schema);
35+
```
36+
## Search Examples:
37+
38+
Search all hashes in the index:
39+
```csharp
40+
var noFilters = ft.Search("example_index", new Query());
41+
```
42+
_noFilters_ now contains: _student:1111_, _student:5555_, _pupil:4444_, _student:3333_.<br /><br />
43+
44+
Search for hashes with a first name starting with Jo
45+
```csharp
46+
var startWithJo = ft.Search("example_index", new Query("@first:Jo*"));
47+
```
48+
_startWithJo_ now contains: _student:1111_ (Joe), _student:5555_ (Joen).<br /><br />
49+
50+
Search for hashes with first name of Pat
51+
```csharp
52+
var namedPat = ft.Search("example_index", new Query("@first:Pat"));
53+
```
54+
_namedPat_ now contains _pupil:4444_ (Pat). _teacher:6666_ (Pat) is not included because it does not have a prefix of 'student:' or 'pupil:'<br /><br />
55+
56+
Search for hashes with last name of Rod
57+
```csharp
58+
var lastNameRod = ft.Search("example_index", new Query("@last:Rod"));
59+
```
60+
_lastNameRod_ is empty because there are no hashes with a last name of Rod that match the index definition.

Examples/PipelineExample.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Pipeline
2+
## An example of pipelines Redis Stack Redis commands (JSON.SET & JSON.CLEAR & JSON.GET)
3+
4+
Connect to the Redis server and Setup new Pipeline
5+
```csharp
6+
IDatabase db = redisFixture.Redis.GetDatabase();
7+
var pipeline = new Pipeline(db);
8+
```
9+
10+
11+
Add JSON data to pipeline
12+
```csharp
13+
pipeline.Json.SetAsync("person", "$", new { name = "John", age = 30, city = "New York", nicknames = new[] { "John", "Johny", "Jo" } });
14+
```
15+
16+
Increase age by 2
17+
```csharp
18+
pipeline.Json.NumIncrbyAsync("person", "$.age", 2);
19+
```
20+
21+
Remove the ```nicknames``` field from the JSON object
22+
```csharp
23+
pipeline.Json.ClearAsync("person", "$.nicknames");
24+
```
25+
26+
Delete the nicknames
27+
```csharp
28+
pipeline.Json.DelAsync("person", "$.nicknames");
29+
```
30+
31+
Retrieve the JSON response
32+
```csharp
33+
var getResponse = pipeline.Json.GetAsync("person");
34+
```
35+
36+
Execute pipeline
37+
```csharp
38+
pipeline.Execute();
39+
```
40+
41+
Access the result of the JSON response
42+
```csharp
43+
var result = getResponse.Result;
44+
```
45+
now result is:
46+
```json
47+
{
48+
"name": "John",
49+
"age": 32,
50+
"city": "New York"
51+
}
52+
```

Examples/PipelineWithAsync.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Pipeline With Async
2+
## An example of pipelines Redis Stack Redis commands (JSON.SET & JSON.CLEAR & JSON.GET)
3+
4+
Connect to the Redis server
5+
```csharp
6+
var redis = ConnectionMultiplexer.Connect("localhost");
7+
```
8+
9+
Get a reference to the database
10+
```csharp
11+
var db = redis.GetDatabase();
12+
```
13+
14+
Setup pipeline connection
15+
```csharp
16+
var pipeline = new Pipeline(db);
17+
```
18+
19+
Create metadata labels for a TimeSeries object:
20+
21+
```csharp
22+
TimeSeriesLabel label1 = new TimeSeriesLabel("temp", "TLV");
23+
TimeSeriesLabel label2 = new TimeSeriesLabel("temp", "JLM");
24+
var labels1 = new List<TimeSeriesLabel> { label1 };
25+
var labels2 = new List<TimeSeriesLabel> { label2 };
26+
```
27+
28+
Create a new time-series object:
29+
30+
```csharp
31+
pipeline.Ts.CreateAsync("temp:TLV", labels: labels1);
32+
pipeline.Ts.CreateAsync("temp:JLM", labels: labels2);
33+
```
34+
35+
Create the TimeSeries objects, and store them in Redis:
36+
37+
```csharp
38+
List<(string, TimeStamp, double)> sequence1 = new List<(string, TimeStamp, double)>()
39+
{
40+
("temp:TLV",1000,30),
41+
("temp:TLV", 1010 ,35),
42+
("temp:TLV", 1020, 9999),
43+
("temp:TLV", 1030, 40)
44+
};
45+
List<(string, TimeStamp, double)> sequence2 = new List<(string, TimeStamp, double)>()
46+
{
47+
("temp:JLM",1005,30),
48+
("temp:JLM", 1015 ,35),
49+
("temp:JLM", 1025, 9999),
50+
("temp:JLM", 1035, 40)
51+
};
52+
53+
pipeline.Ts.MAddAsync(sequence1);
54+
pipeline.Ts.MAddAsync(sequence2);
55+
56+
```
57+
58+
Execute the pipeline:
59+
60+
```csharp
61+
pipeline.Execute();
62+
```
63+
64+
Get a reference to the database and for TimeSeries commands:
65+
```csharp
66+
var ts = db.TS();
67+
```
68+
69+
Get only the location label for each last sample, use SELECTED_LABELS.
70+
```csharp
71+
var respons = await ts.MGetAsync(new List<string> { "temp=JLM" }, selectedLabels: new List<string> { "location" });
72+
```

Examples/TransactionsExample.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Transaction
2+
3+
## An example of transaction with Redis modules (JSON.SET, JSON.GET & JSON.NUMINCRBY)
4+
5+
Connect to the Redis server:
6+
7+
```cs
8+
var redis = await ConnectionMultiplexer.ConnectAsync("localhost");
9+
var db = redis.GetDatabase();
10+
```
11+
12+
Create the transaction:
13+
14+
```cs
15+
var tran = new Transaction(db);
16+
```
17+
18+
Store the account details as JSON:
19+
20+
```csharp
21+
tran.Json.SetAsync("accdetails:Jeeva", "$", new { name = "Jeeva", totalAmount= 1000, bankName = "City" });
22+
tran.Json.SetAsync("accdetails:Shachar", "$", new { name = "Shachar", totalAmount = 1000, bankName = "City" });
23+
```
24+
25+
Retrieve the responses
26+
27+
```csharp
28+
var getShachar = tran.Json.GetAsync("accdetails:Shachar");
29+
var getJeeva = tran.Json.GetAsync("accdetails:Jeeva");
30+
```
31+
32+
Debit 200 from Jeeva
33+
34+
```cs
35+
tran.Json.NumIncrbyAsync("accdetails:Jeeva", "$.totalAmount", -200);
36+
```
37+
38+
Credit 200 from Shachar
39+
40+
```cs
41+
tran.Json.NumIncrbyAsync("accdetails:Shachar", "$.totalAmount", 200);
42+
```
43+
44+
Get total amount for both Jeeva = 800 & Shachar = 1200
45+
46+
```cs
47+
var totalAmtOfJeeva = tran.Json.GetAsync("accdetails:Jeeva", path:"$.totalAmount");
48+
var totalAmtOfShachar = tran.Json.GetAsync("accdetails:Shachar", path:"$.totalAmount");
49+
```
50+
51+
Execute the transaction
52+
53+
```cs
54+
var condition = tran.ExecuteAsync();
55+
```

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ IJsonCommands json = db.JSON();
6565
ITimeSeriesCommands ts = db.TS();
6666
```
6767
Then, that variable will allow you to call all the commands of that module.
68+
6869
## Examples
69-
### Set JSON object to Redis
70-
Set a json object to Redis:
70+
71+
### Store a JSON object in Redis
72+
73+
To store a json object in Redis:
74+
7175
```csharp
7276
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
7377
IDatabase db = redis.GetDatabase();
@@ -76,17 +80,20 @@ IJsonCommands json = db.JSON();
7680
var key = "myKey";
7781
json.Set(key, "$", new Person() { Age = 35, Name = "Alice" });
7882
```
83+
7984
### Index and search
80-
We will see an example that shows how you can create an index, add a document to it and search it using NRedisStack.
85+
Now, to execute a search for objects, we need to index them on the server, and run a query:
8186

8287
Setup:
88+
8389
```csharp
8490
using NRedisStack;
8591
...
8692
IDatabase db = redisFixture.Redis.GetDatabase();
8793
ISearchCommands ft = db.FT();
8894
IJsonCommands json = db.JSON();
8995
```
96+
9097
Create an index with fields and weights:
9198
```csharp
9299
// FT.CREATE myIdx ON HASH PREFIX 1 doc: SCHEMA title TEXT WEIGHT 5.0 body TEXT url TEXT
@@ -97,8 +104,7 @@ ft.Create("myIndex", new FTCreateParams().On(IndexDataType.Hash)
97104
.AddTextField("url"));
98105
```
99106

100-
After you create the index, any new hash documents with the doc: prefix are automatically indexed upon creation.
101-
107+
After creating the index, future documents with the ```doc:``` prefix will be automatically indexed when created or modified.
102108

103109
To create a new hash document and add it to the index, use the HSET command:
104110
```csharp
@@ -117,6 +123,9 @@ Drop the index:
117123
// FT.DROPINDEX myIndex
118124
ft.DropIndex("myIndex");
119125
```
126+
127+
More examples can be found in the [examples folder](/blob/master/Examples).
128+
120129
------
121130

122131
### Author

0 commit comments

Comments
 (0)