-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
121 lines (107 loc) · 3.25 KB
/
Copy pathProgram.cs
File metadata and controls
121 lines (107 loc) · 3.25 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
using System.Text.Json;
using FacetedSearch.Data;
using Microsoft.EntityFrameworkCore;
using ParadeDB.EntityFrameworkCore.Extensions;
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("Faceted Search Example");
Console.WriteLine(new string('=', 60));
await ExampleSetup.SetupMockItemsAsync(dbContext);
var count = await dbContext.MockItems.CountAsync();
Console.WriteLine($"\nLoaded {count} items");
var searchQuery = "shoes";
Console.WriteLine($"\nQuery: '{searchQuery}'");
Console.WriteLine("\n--- Facets + Rows (Top K) ---");
var results = await dbContext
.MockItems.Where(x => EF.Functions.MatchAll(x.Description, searchQuery))
.Select(x => new
{
x.Id,
x.Description,
x.Category,
x.Rating,
x.InStock,
x.Metadata,
CategoryTerms = EF.Functions.AggOver(
new
{
terms = new
{
field = "category",
size = 10,
order = new { _count = "desc" },
},
}
),
RatingTerms = EF.Functions.AggOver(
new
{
terms = new
{
field = "rating",
size = 10,
order = new { _count = "desc" },
},
}
),
ColorTerms = EF.Functions.AggOver(
new
{
terms = new
{
field = "metadata.color",
size = 10,
order = new { _count = "desc" },
},
}
),
})
.OrderByDescending(x => x.Rating)
.Take(5)
.ToListAsync();
Console.WriteLine("Top results:");
foreach (var item in results)
{
var color =
item.Metadata?.RootElement.TryGetProperty("color", out var c) == true
? c.GetString()
: "N/A";
var stock = item.InStock ? "In Stock" : "Out of Stock";
var desc = item.Description[..Math.Min(50, item.Description.Length)];
Console.WriteLine(
$" - {desc}... [{item.Category}] (rating: {item.Rating}, {stock}, color: {color})"
);
}
Console.WriteLine("\nFacet buckets:");
if (results.Count > 0)
{
static void PrintFacets(string label, JsonElement? json)
{
if (json is null)
{
return;
}
if (!json.Value.TryGetProperty("buckets", out var buckets))
{
return;
}
Console.WriteLine($"{label} ({buckets.GetArrayLength()} buckets)");
foreach (var bucket in buckets.EnumerateArray())
{
Console.WriteLine(
$" - {bucket.GetProperty("key")}: {bucket.GetProperty("doc_count")}"
);
}
}
PrintFacets("category_terms", results[0].CategoryTerms);
PrintFacets("rating_terms", results[0].RatingTerms);
PrintFacets("metadata.color_terms", results[0].ColorTerms);
}
Console.WriteLine();
Console.WriteLine(new string('=', 60));
Console.WriteLine("Done.");