-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (102 loc) · 3.73 KB
/
Copy pathProgram.cs
File metadata and controls
125 lines (102 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Microsoft.EntityFrameworkCore;
using ParadeDB.EntityFrameworkCore;
using ParadeDB.EntityFrameworkCore.Extensions;
using Quickstart.Data;
using Shared;
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseNpgsql(ExampleSetup.ConnectionString, o => o.UseParadeDb())
.UseSnakeCaseNamingConvention()
.Options;
await using var dbContext = new AppDbContext(options);
Console.WriteLine(new string('=', 60));
Console.WriteLine("Quickstart Example");
Console.WriteLine(new string('=', 60));
await ExampleSetup.SetupMockItemsAsync(dbContext);
var count = await dbContext.MockItems.CountAsync();
Console.WriteLine($"Loaded {count} mock items");
await DemoBasicSearch(dbContext);
await DemoScoredSearch(dbContext);
await DemoPhraseSearch(dbContext);
await DemoSnippetHighlighting(dbContext);
await DemoFilteredSearch(dbContext);
Console.WriteLine();
Console.WriteLine(new string('=', 60));
Console.WriteLine("Done!");
return;
static async Task DemoBasicSearch(AppDbContext db)
{
Console.WriteLine("\n--- Basic Search: 'shoes' ---");
var results = await db
.MockItems.Where(x => EF.Functions.MatchAll(x.Description, "shoes"))
.Take(5)
.ToListAsync();
foreach (var item in results)
Console.WriteLine($" • {Truncate(item.Description, 60)}...");
}
static async Task DemoScoredSearch(AppDbContext db)
{
Console.WriteLine("\n--- Scored Search: 'running' ---");
var results = await db
.MockItems.Where(x => EF.Functions.MatchAll(x.Description, "running"))
.Select(x => new { Item = x, Score = EF.Functions.Score(x.Id) })
.OrderByDescending(x => x.Score)
.Take(5)
.ToListAsync();
foreach (var result in results)
Console.WriteLine(
$" • {Truncate(result.Item.Description, 50)}... (score: {result.Score:F2})"
);
}
static async Task DemoPhraseSearch(AppDbContext db)
{
Console.WriteLine("\n--- Phrase Search: 'running shoes' ---");
var results = await db
.MockItems.Where(x => EF.Functions.Phrase(x.Description, "running shoes"))
.Select(x => new { Item = x, Score = EF.Functions.Score(x.Id) })
.OrderByDescending(x => x.Score)
.Take(5)
.ToListAsync();
foreach (var result in results)
Console.WriteLine(
$" • {Truncate(result.Item.Description, 50)}... (score: {result.Score:F2})"
);
}
static async Task DemoSnippetHighlighting(AppDbContext db)
{
Console.WriteLine("\n--- Snippet Highlighting: 'shoes' ---");
var results = await db
.MockItems.Where(x => EF.Functions.MatchAll(x.Description, "shoes"))
.Select(x => new
{
Score = EF.Functions.Score(x.Id),
Snippet = EF.Functions.Snippet(
x.Description,
new SnippetOptions { StartTag = "<b>", EndTag = "</b>" }
),
})
.OrderByDescending(x => x.Score)
.Take(3)
.ToListAsync();
foreach (var result in results)
Console.WriteLine($" • {result.Snippet}");
}
static async Task DemoFilteredSearch(AppDbContext db)
{
Console.WriteLine("\n--- Filtered Search: 'shoes' + in_stock + rating >= 4 ---");
var results = await db
.MockItems.Where(x =>
EF.Functions.MatchAll(x.Description, "shoes") && x.InStock && x.Rating >= 4
)
.Select(x => new { Item = x, Score = EF.Functions.Score(x.Id) })
.OrderByDescending(x => x.Score)
.Take(5)
.ToListAsync();
foreach (var result in results)
Console.WriteLine(
$" • {Truncate(result.Item.Description, 40)}... (rating: {result.Item.Rating})"
);
}
static string Truncate(string value, int length)
{
return value.Length <= length ? value : value[..length];
}