Skip to content

Commit b0324f2

Browse files
authored
Add doctest for Search Quickstart (#134)
1 parent 7d2b006 commit b0324f2

File tree

3 files changed

+277
-26
lines changed

3 files changed

+277
-26
lines changed

.github/workflows/doctests.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Documentation Tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
doctests:
14+
runs-on: ubuntu-latest
15+
services:
16+
redis-stack:
17+
image: redis/redis-stack-server:latest
18+
options: >-
19+
--health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
20+
ports:
21+
- 6379:6379
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: .NET Core 6
25+
uses: actions/setup-dotnet@v2
26+
with:
27+
dotnet-version: '6.0.x'
28+
- name: .NET Core 7
29+
uses: actions/setup-dotnet@v2
30+
with:
31+
dotnet-version: '7.0.x'
32+
- name: run tests
33+
working-directory: tests/Doc
34+
run: |
35+
dotnet test

tests/Doc/Doc.csproj

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<IsWindows>$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::get_Windows())))</IsWindows>
5-
<TargetFrameworks Condition=" '$(IsWindows)' == 'true'">net6.0;net7.0;net481</TargetFrameworks>
6-
<TargetFrameworks Condition=" '$(IsWindows)' != 'true'">net6.0;net7.0</TargetFrameworks>
7-
<ImplicitUsings>enable</ImplicitUsings>
8-
<Nullable>enable</Nullable>
9-
<LangVersion>latest</LangVersion>
10-
11-
12-
<IsPackable>false</IsPackable>
13-
<OutputType>Module</OutputType>
14-
</PropertyGroup>
15-
16-
<ItemGroup>
17-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
18-
<PackageReference Include="xunit" Version="2.4.2" />
19-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
20-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21-
<PrivateAssets>all</PrivateAssets>
22-
</PackageReference>
23-
<PackageReference Include="StackExchange.Redis" Version="2.6.104" />
24-
</ItemGroup>
25-
26-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<IsWindows>$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::get_Windows())))</IsWindows>
4+
<TargetFrameworks Condition=" '$(IsWindows)' == 'true'">net6.0;net7.0;net481</TargetFrameworks>
5+
<TargetFrameworks Condition=" '$(IsWindows)' != 'true'">net6.0;net7.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<LangVersion>latest</LangVersion>
9+
<IsPackable>false</IsPackable>
10+
<OutputType>Module</OutputType>
11+
</PropertyGroup>
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
14+
<PackageReference Include="xunit" Version="2.4.2" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="StackExchange.Redis" Version="2.6.104" />
20+
</ItemGroup>
21+
<ItemGroup>
22+
<ProjectReference Include="..\..\src\NRedisStack\NRedisStack.csproj" />
23+
</ItemGroup>
24+
</Project>

tests/Doc/SearchQuickstartExample.cs

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// EXAMPLE: search_quickstart
2+
using NRedisStack.RedisStackCommands;
3+
using NRedisStack.Search;
4+
using NRedisStack.Search.Aggregation;
5+
using NRedisStack.Search.Literals.Enums;
6+
using StackExchange.Redis;
7+
8+
// REMOVE_START
9+
namespace NRedisStack.Doc;
10+
// REMOVE_END
11+
12+
public class SearchQuickstartExample
13+
{
14+
[Fact]
15+
public void run()
16+
{
17+
// STEP_START connect
18+
var redis = ConnectionMultiplexer.Connect("localhost:6379");
19+
var db = redis.GetDatabase();
20+
var ft = db.FT();
21+
var json = db.JSON();
22+
// STEP_END
23+
24+
// REMOVE_START
25+
try
26+
{
27+
ft.DropIndex("idx:bicycle");
28+
}
29+
catch
30+
{
31+
}
32+
// REMOVE_END
33+
34+
// STEP_START data_sample
35+
var bike1 = new
36+
{
37+
Brand = "Diaz Ltd",
38+
Model = "Dealer Sl",
39+
Price = 7315.58M,
40+
Description = "The Diaz Ltd Dealer Sl is a reliable choice" +
41+
" for urban cycling. The Diaz Ltd Dealer Sl " +
42+
"is a comfortable choice for urban cycling.",
43+
Condition = "used"
44+
};
45+
// STEP_END
46+
47+
var bicycles = new[]
48+
{
49+
bike1,
50+
new
51+
{
52+
Brand = "Bridges Group",
53+
Model = "Project Pro",
54+
Price = 3610.82M,
55+
Description =
56+
"This mountain bike is perfect for mountain biking. " +
57+
"The Bridges Group Project Pro is a responsive choice" +
58+
" for mountain biking.",
59+
Condition = "used"
60+
},
61+
new
62+
{
63+
Brand = "Vega, Cole and Miller",
64+
Model = "Group Advanced",
65+
Price = 8961.42M,
66+
Description =
67+
"The Vega, Cole and Miller Group Advanced provides an " +
68+
"excellent ride. With its fast carbon frame and 24 gears," +
69+
" this bicycle is perfect for any terrain.",
70+
Condition = "used"
71+
},
72+
new
73+
{
74+
Brand = "Powell-Montgomery",
75+
Model = "Angle Race",
76+
Price = 4050.27M,
77+
Description =
78+
"The Powell-Montgomery Angle Race is a smooth choice for" +
79+
" road cycling. The Powell-Montgomery Angle Race" +
80+
" provides a durable ride.",
81+
Condition = "used"
82+
},
83+
new
84+
{
85+
Brand = "Gill-Lewis",
86+
Model = "Action Evo",
87+
Price = 283.68M,
88+
Description =
89+
"The Gill-Lewis Action Evo provides a smooth ride. " +
90+
"The Gill-Lewis Action Evo provides an excellent ride.",
91+
Condition = "used"
92+
},
93+
new
94+
{
95+
Brand = "Rodriguez-Guerrero",
96+
Model = "Drama Comp",
97+
Price = 4462.55M,
98+
Description =
99+
"This kids bike is perfect for young riders. With its " +
100+
"excellent aluminum frame and 12 gears, this bicycle " +
101+
"is perfect for any terrain.",
102+
Condition = "new"
103+
},
104+
new
105+
{
106+
Brand = "Moore PLC",
107+
Model = "Award Race",
108+
Price = 3790.76M,
109+
Description =
110+
"This olive folding bike features a carbon frame and" +
111+
" 27.5 inch wheels. This folding bike is perfect for" +
112+
" compact storage and transportation.",
113+
Condition = "new"
114+
},
115+
new
116+
{
117+
Brand = "Hall, Haley and Hayes",
118+
Model = "Weekend Plus",
119+
Price = 2008.4M,
120+
Description =
121+
"The Hall, Haley and Hayes Weekend Plus provides a" +
122+
" comfortable ride. This blue kids bike features a" +
123+
" steel frame and 29.0 inch wheels.",
124+
Condition = "new"
125+
},
126+
new
127+
{
128+
Brand = "Peck-Carson",
129+
Model = "Sun Hybrid",
130+
Price = 9874.95M,
131+
Description =
132+
"With its comfortable aluminum frame and 25 gears," +
133+
" this bicycle is perfect for any terrain. The " +
134+
"Peck-Carson Sun Hybrid provides a comfortable ride.",
135+
Condition = "new"
136+
},
137+
new
138+
{
139+
Brand = "Fowler Ltd",
140+
Model = "Weekend Trail",
141+
Price = 3833.71M,
142+
Description =
143+
"The Fowler Ltd Letter Trail is a comfortable choice" +
144+
" for transporting cargo. This cargo bike is " +
145+
"perfect for transporting cargo.",
146+
Condition = "refurbished"
147+
}
148+
};
149+
150+
// STEP_START define_index
151+
var schema = new Schema()
152+
.AddTextField(new FieldName("$.Brand", "Brand"))
153+
.AddTextField(new FieldName("$.Model", "Model"))
154+
.AddTextField(new FieldName("$.Description", "Description"))
155+
.AddNumericField(new FieldName("$.Price", "Price"))
156+
.AddTagField(new FieldName("$.Condition", "Condition"));
157+
// STEP_END
158+
159+
// STEP_START create_index
160+
ft.Create(
161+
"idx:bicycle",
162+
new FTCreateParams().On(IndexDataType.JSON).Prefix("bicycle:"),
163+
schema);
164+
// STEP_END
165+
166+
// STEP_START add_documents
167+
for (int i = 0; i < bicycles.Length; i++)
168+
{
169+
json.Set($"bicycle:{i}", "$", bicycles[i]);
170+
}
171+
// STEP_END
172+
173+
// STEP_START query_single_term_and_num_range
174+
var query = new Query("folding @Price:[1000 4000]");
175+
var res = ft.Search("idx:bicycle", query).Documents;
176+
Console.WriteLine(string.Join("\n", res.Select(x => x["json"])));
177+
// Prints: {"Brand":"Moore PLC","Model":"Award Race","Price":3790.76,
178+
// "Description":"This olive folding bike features a carbon frame
179+
// and 27.5 inch wheels. This folding bike is perfect for compact
180+
// storage and transportation.","Condition":"new"}
181+
// STEP_END
182+
// REMOVE_START
183+
Assert.Single(res);
184+
Assert.Equal("bicycle:6", res[0].Id);
185+
// REMOVE_END
186+
187+
// STEP_START query_single_term_limit_fields
188+
var cargoQuery = new Query("cargo").ReturnFields("Price");
189+
var cargoRes = ft.Search("idx:bicycle", cargoQuery).Documents;
190+
Console.WriteLine(cargoRes.First()["Price"]);
191+
// Prints: 3833.71
192+
// STEP_END
193+
// REMOVE_START
194+
Assert.Single(cargoRes);
195+
Assert.Equal("bicycle:9", cargoRes[0].Id);
196+
// REMOVE_END
197+
198+
// STEP_START simple_aggregation
199+
var request = new AggregationRequest("*").GroupBy(
200+
"@Condition", Reducers.Count().As("Count"));
201+
var result = ft.Aggregate("idx:bicycle", request);
202+
203+
for (var i = 0; i < result.TotalResults; i++)
204+
{
205+
var row = result.GetRow(i);
206+
Console.WriteLine($"{row["Condition"]} - {row["Count"]}");
207+
}
208+
209+
// Prints:
210+
// refurbished - 1
211+
// used - 5
212+
// new - 4
213+
// STEP_END
214+
// REMOVE_START
215+
Assert.Equal(3, result.TotalResults);
216+
// REMOVE_END
217+
}
218+
}

0 commit comments

Comments
 (0)