-
Notifications
You must be signed in to change notification settings - Fork 1
/
Results06.razor
303 lines (260 loc) · 9.94 KB
/
Results06.razor
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
@page "/Results06/{SearchTerm}"
@page "/Results06"
@inject NavigationManager NavigationManager
@using MongoDB.Bson;
@using MongoDB.Driver;
@using AtlasSearchNYC.Datamodels;
@using System;
@using MongoDB.Bson.IO;
<PageTitle>Content</PageTitle>
<div class="row">
<h1>Search Results</h1>
</div>
<div class="row">
<div class="col-auto">
<h2><label for="ddl" class="form-label">Sort By</label></h2>
<div class="input-group">
<select class="form-select form-select-lg sortbox" id="ddl" @onchange=ChangeSort>
<option>Score</option>
<option>Release Year</option>
</select>
</div>
</div>
<div class="col-auto">
@if(facet != null) {
<h2>Genres</h2>
<div class="form-check">
<select class="form-select" name="genres" id="genres" @bind="SelectedGenres" multiple>
@foreach (var f in facet.Genres)
{
<option value="@f.Key">@f.Key (@f.Count)</option>
}
</select>
</div>
}
</div>
<div class="col-auto">
@if(facet != null) {
<h2>Cast</h2>
<div class="form-check">
<select class="form-select" name="cast" id="cast" @bind="SelectedCast" multiple>
@foreach (var f in facet.Cast)
{
<option value="@f.Key">@f.Key (@f.Count)</option>
}
</select>
</div>
}
</div>
<div class="col-auto">
<button class="btn btn-lg btn-leafy" @onclick=RunSearchButton>Search</button>
</div>
</div>
<div class="row">
<SearchBarAutocomplete SearchTerm="@SearchTerm" Redirect="Results06" OnSearchCallback="@ChangeSearchTerm" ShowSuggestions=false />
</div>
<div class="row" style="margin-top:20px;">
@if(movies != null) {
@foreach (var t in movies)
{
<MovieTile SourceMovie=@t ShowSearchScore=true ShowMLT=true ChangeSearchCallback="@ChangeSearchTerm" ShowMovieDetails=true LearnMoreCallback="@OpenMovieModal" />
}
}
</div>
@if(ShowModal) {
<div class="ctr-bg-blurer"></div>
<div class="modal" tabindex="-1" id="ctr_modal">
<div class="modal-dialog modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Movie Details</h5>
<button type="button" class="btn-close" @onclick=ToggleModal ></button>
</div>
<div class="modal-body">
<MovieDetails SourceMovieId=SelectedMovieId />
</div>
</div>
</div>
</div>
}
@code {
[Parameter]
public string SearchTerm {get; set;} = null;
private List<Datamodels.Movie> movies = new List<Movie>();
private Datamodels.FacetResponse facet = null;
private string SortBy = "Score";
private string[] SelectedGenres { get; set; } = new string[] { };
private string[] SelectedCast { get; set; } = new string[] { };
private ObjectId SelectedMovieId;
private bool ShowModal = false;
private void ToggleModal() {
ShowModal = !ShowModal;
}
protected async void ChangeSort(ChangeEventArgs e) {
SortBy = e.Value.ToString();
await RunSearch();
await RunFacets();
}
protected override async Task OnInitializedAsync()
{
if(SearchTerm != null) {
await RunSearch();
await RunFacets();
}
}
private async void RunSearchButton() {
NavigationManager.NavigateTo("/Results06/" + SearchTerm);
await RunSearch();
await RunFacets();
}
private async void ChangeSearchTerm(string term) {
Console.WriteLine("R06 - ChangeSearchTerm " + term);
SearchTerm = term;
NavigationManager.NavigateTo("/Results06/" + SearchTerm);
await RunSearch();
await RunFacets();
}
private async void OpenMovieModal(ObjectId id) {
Console.WriteLine("R06 - Open Modal");
SelectedMovieId = id;
ShowModal = true;
StateHasChanged();
}
private async Task RunSearch() {
List<BsonDocument> p = new List<BsonDocument>();
var searchStage = new BsonDocument();
searchStage.Add(new BsonDocument("index","facets"));
var basicSearch = new BsonDocument("text", new BsonDocument {
{ "query", SearchTerm },
{ "fuzzy", new BsonDocument() },
{ "path",
new BsonArray {
"title",
"fullplot"
}
}
});
@if(SelectedCast.Length==0 && SelectedGenres.Length==0) {
searchStage.Add(basicSearch);
} else {
var filter = new List<BsonDocument>();
if(SelectedCast.Length > 0) {
var cbarray = new BsonArray();
cbarray.AddRange(SelectedCast);
var castDoc = new BsonDocument("text", new BsonDocument{
{"path","cast"},
{"query",cbarray}
});
filter.Add(castDoc);
}
if(SelectedGenres.Length > 0) {
var cgarray = new BsonArray();
cgarray.AddRange(SelectedGenres);
var genDoc = new BsonDocument("text", new BsonDocument{
{"path","genres"},
{"query",cgarray}
});
filter.Add(genDoc);
}
var fbarray = new BsonArray();
fbarray.AddRange(filter);
searchStage.Add(new BsonElement("compound", new BsonDocument {
new BsonElement( "filter", fbarray),
new BsonElement( "must", new BsonArray { basicSearch})
}
));
}
@if(SortBy != "Score") {
searchStage.Add("sortBetaV1", new BsonDocument("year", -1) );
}
searchStage.Add("highlight", new BsonDocument(
"path",
new BsonArray
{
"fullplot",
"title"
})
);
p.Add(new BsonDocument("$search", searchStage));
p.Add(new BsonDocument("$project",
new BsonDocument
{
{ "title", 1 },
{ "fullplot", 1 },
{ "year", 1 },
{ "genres", 1 },
{ "poster", 1 },
{ "score",
new BsonDocument("$meta", "searchScore") },
{ "highlights",
new BsonDocument("$meta", "searchHighlights") }
}));
p.Add(new BsonDocument("$limit", 25));
var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; // key part
Console.WriteLine(p.ToJson(jsonWriterSettings));
string MDBCONNSTR = Environment.GetEnvironmentVariable("MDBCONNSTR");
var settings = MongoClientSettings.FromConnectionString(MDBCONNSTR);
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
var client = new MongoClient(settings);
var database = client.GetDatabase("sample_mflix");
var col = database.GetCollection<Datamodels.Movie>("movies");
var result = await col.Aggregate<Datamodels.Movie>(p.ToArray()).ToListAsync();
movies = result;
}
private async Task RunFacets() {
var pipeline = new BsonDocument[] {
new BsonDocument("$searchMeta",
new BsonDocument
{
{ "index", "facets" },
{ "facet",
new BsonDocument
{
{ "operator",
new BsonDocument("text",
new BsonDocument
{
{ "path",
new BsonArray
{
"fullplot",
"title"
} },
{ "query", SearchTerm },
{ "fuzzy", new BsonDocument() }
}) },
{ "facets",
new BsonDocument
{
{ "genresFacet",
new BsonDocument
{
{ "type", "string" },
{ "path", "genres" }
} },
{ "castFacet",
new BsonDocument
{
{ "type", "string" },
{ "path", "cast" }
} }
} }
} }
}),
new BsonDocument("$project",
new BsonDocument
{
{ "genresFacetBuckets", "$facet.genresFacet.buckets" },
{ "castFacetBuckets", "$facet.castFacet.buckets" }
})
};
string MDBCONNSTR = Environment.GetEnvironmentVariable("MDBCONNSTR");
var settings = MongoClientSettings.FromConnectionString(MDBCONNSTR);
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
var client = new MongoClient(settings);
var database = client.GetDatabase("sample_mflix");
var col = database.GetCollection<Datamodels.FacetResponse>("movies");
var result = await col.Aggregate<Datamodels.FacetResponse>(pipeline).ToListAsync();
facet = result[0];
}
}