Skip to content

Commit e783070

Browse files
DOC-5840 add C#-sync index/query notebook
1 parent 338f735 commit e783070

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed

content/develop/clients/dotnet/queryjson.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ to learn more about the available connection options.
6060
{{< clients-example cs_home_json connect >}}
6161
{{< /clients-example >}}
6262

63+
Delete any existing index called `idx:users` and any keys that start with `user:`.
64+
65+
{{< clients-example cs_home_json cleanup_json >}}
66+
{{< /clients-example >}}
67+
6368
Create an index. In this example, only JSON documents with the key prefix `user:` are indexed. For more information, see [Query syntax]({{< relref "/develop/ai/search-and-query/query/" >}}).
6469

6570
{{< clients-example cs_home_json make_index >}}
@@ -109,6 +114,13 @@ in the `FTCreateParams` object when you create the index. The code below shows
109114
these changes with a new index called `hash-idx:users`, which is otherwise the
110115
same as the `idx:users` index used for JSON documents in the previous examples.
111116

117+
First, delete any existing index called `hash-idx:users` and any keys that start with `huser:`.
118+
119+
{{< clients-example cs_home_json cleanup_hash >}}
120+
{{< /clients-example >}}
121+
122+
Now create the new index:
123+
112124
{{< clients-example cs_home_json make_hash_index >}}
113125
{{< /clients-example >}}
114126

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
// EXAMPLE: cs_home_json
2+
// BINDER_ID netsync-cs_home_json
3+
// STEP_START import
4+
using NRedisStack.RedisStackCommands;
5+
using NRedisStack.Search;
6+
using NRedisStack.Search.Aggregation;
7+
using NRedisStack.Search.Literals.Enums;
8+
using StackExchange.Redis;
9+
// STEP_END
10+
11+
// REMOVE_START
12+
using NRedisStack.Tests;
13+
14+
namespace Doc;
15+
[Collection("DocsTests")]
16+
// REMOVE_END
17+
18+
public class HomeJsonExample
19+
// REMOVE_START
20+
: AbstractNRedisStackTest, IDisposable
21+
// REMOVE_END
22+
{
23+
// REMOVE_START
24+
public HomeJsonExample(EndpointsFixture fixture) : base(fixture) { }
25+
26+
[SkippableFact]
27+
// REMOVE_END
28+
public void Run()
29+
{
30+
//REMOVE_START
31+
// This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection
32+
SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone);
33+
var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone);
34+
//REMOVE_END
35+
36+
// STEP_START connect
37+
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
38+
var db = muxer.GetDatabase();
39+
// STEP_END
40+
41+
// STEP_START cleanup_json
42+
db.KeyDelete(["user:1", "user:2", "user:3"]);
43+
try { db.FT().DropIndex("idx:users"); } catch { }
44+
// STEP_END
45+
46+
// STEP_START create_data
47+
var user1 = new
48+
{
49+
name = "Paul John",
50+
email = "[email protected]",
51+
age = 42,
52+
city = "London"
53+
};
54+
55+
var user2 = new
56+
{
57+
name = "Eden Zamir",
58+
email = "[email protected]",
59+
age = 29,
60+
city = "Tel Aviv"
61+
};
62+
63+
var user3 = new
64+
{
65+
name = "Paul Zamir",
66+
email = "[email protected]",
67+
age = 35,
68+
city = "Tel Aviv"
69+
};
70+
// STEP_END
71+
72+
// STEP_START make_index
73+
var schema = new Schema()
74+
.AddTextField(new FieldName("$.name", "name"))
75+
.AddTagField(new FieldName("$.city", "city"))
76+
.AddNumericField(new FieldName("$.age", "age"));
77+
78+
bool indexCreated = db.FT().Create(
79+
"idx:users",
80+
new FTCreateParams()
81+
.On(IndexDataType.JSON)
82+
.Prefix("user:"),
83+
schema
84+
);
85+
// STEP_END
86+
// REMOVE_START
87+
Assert.True(indexCreated);
88+
// REMOVE_END
89+
90+
91+
// STEP_START add_data
92+
bool user1Set = db.JSON().Set("user:1", "$", user1);
93+
bool user2Set = db.JSON().Set("user:2", "$", user2);
94+
bool user3Set = db.JSON().Set("user:3", "$", user3);
95+
// STEP_END
96+
// REMOVE_START
97+
Assert.True(user1Set);
98+
Assert.True(user2Set);
99+
Assert.True(user3Set);
100+
// REMOVE_END
101+
102+
103+
// STEP_START query1
104+
SearchResult findPaulResult = db.FT().Search(
105+
"idx:users",
106+
new("Paul @age:[30 40]")
107+
);
108+
Console.WriteLine(string.Join(
109+
", ",
110+
findPaulResult.Documents.Select(x => x["json"])
111+
));
112+
// >>> {"name":"Paul Zamir","email":"[email protected]", ...
113+
// STEP_END
114+
// REMOVE_START
115+
Assert.Equal(
116+
"{\"name\":\"Paul Zamir\",\"email\":\"[email protected]\",\"age\":35,\"city\":\"Tel Aviv\"}",
117+
string.Join(", ", findPaulResult.Documents.Select(x => x["json"]))
118+
);
119+
// REMOVE_END
120+
121+
122+
// STEP_START query2
123+
var citiesResult = db.FT().Search(
124+
"idx:users",
125+
new Query("Paul")
126+
.ReturnFields(new FieldName("$.city", "city"))
127+
);
128+
Console.WriteLine(string.Join(
129+
", ",
130+
citiesResult.Documents.Select(x => x["city"]).OrderBy(x => x)
131+
));
132+
// >>> London, Tel Aviv
133+
// STEP_END
134+
// REMOVE_START
135+
Assert.Equal(
136+
"London, Tel Aviv",
137+
string.Join(", ", citiesResult.Documents.Select(x => x["city"]).OrderBy(x => x))
138+
);
139+
// REMOVE_END
140+
141+
142+
// STEP_START query3
143+
AggregationRequest aggRequest = new AggregationRequest("*")
144+
.GroupBy("@city", Reducers.Count().As("count"));
145+
146+
AggregationResult aggResult = db.FT().Aggregate("idx:users", aggRequest);
147+
IReadOnlyList<Dictionary<string, RedisValue>> resultsList =
148+
aggResult.GetResults();
149+
150+
for (var i = 0; i < resultsList.Count; i++)
151+
{
152+
Dictionary<string, RedisValue> item = resultsList.ElementAt(i);
153+
Console.WriteLine($"{item["city"]} - {item["count"]}");
154+
}
155+
// >>> London - 1
156+
// >>> Tel Aviv - 2
157+
// STEP_END
158+
// REMOVE_START
159+
Assert.Equal(2, resultsList.Count);
160+
161+
var sortedResults = resultsList.OrderBy(x => x["city"]);
162+
Dictionary<string, RedisValue> testItem = sortedResults.ElementAt(0);
163+
Assert.Equal("London", testItem["city"]);
164+
Assert.Equal(1, testItem["count"]);
165+
166+
testItem = sortedResults.ElementAt(1);
167+
Assert.Equal("Tel Aviv", testItem["city"]);
168+
Assert.Equal(2, testItem["count"]);
169+
// REMOVE_END
170+
171+
// STEP_START cleanup_hash
172+
db.KeyDelete(["huser:1", "huser:2", "huser:3"]);
173+
try { db.FT().DropIndex("hash-idx:users"); } catch { }
174+
// STEP_END
175+
176+
// STEP_START make_hash_index
177+
var hashSchema = new Schema()
178+
.AddTextField("name")
179+
.AddTagField("city")
180+
.AddNumericField("age");
181+
182+
bool hashIndexCreated = db.FT().Create(
183+
"hash-idx:users",
184+
new FTCreateParams()
185+
.On(IndexDataType.HASH)
186+
.Prefix("huser:"),
187+
hashSchema
188+
);
189+
// STEP_END
190+
// REMOVE_START
191+
Assert.True(hashIndexCreated);
192+
// REMOVE_END
193+
194+
// STEP_START add_hash_data
195+
db.HashSet("huser:1", [
196+
new("name", "Paul John"),
197+
new("email", "[email protected]"),
198+
new("age", 42),
199+
new("city", "London")
200+
]);
201+
202+
db.HashSet("huser:2", [
203+
new("name", "Eden Zamir"),
204+
new("email", "[email protected]"),
205+
new("age", 29),
206+
new("city", "Tel Aviv")
207+
]);
208+
209+
db.HashSet("huser:3", [
210+
new("name", "Paul Zamir"),
211+
new("email", "[email protected]"),
212+
new("age", 35),
213+
new("city", "Tel Aviv")
214+
]);
215+
// STEP_END
216+
217+
// STEP_START query1_hash
218+
SearchResult findPaulHashResult = db.FT().Search(
219+
"hash-idx:users",
220+
new("Paul @age:[30 40]")
221+
);
222+
223+
foreach (Document doc in findPaulHashResult.Documents)
224+
{
225+
Console.WriteLine(
226+
$"Name: {doc["name"]}, email: {doc["email"]}, " +
227+
$"age: {doc["age"]}, city:{doc["city"]}"
228+
);
229+
}
230+
// >>> Name: Paul Zamir, email: [email protected], age: 35, ...
231+
// STEP_END
232+
// REMOVE_START
233+
Document d = findPaulHashResult.Documents[0];
234+
Assert.Equal(
235+
"Name: Paul Zamir, email: [email protected], age: 35, city:Tel Aviv",
236+
$"Name: {d["name"]}, email: {d["email"]}, " +
237+
$"age: {d["age"]}, city:{d["city"]}"
238+
);
239+
// REMOVE_END
240+
}
241+
}

0 commit comments

Comments
 (0)