-
Notifications
You must be signed in to change notification settings - Fork 1
/
Results04.razor
163 lines (146 loc) · 5.33 KB
/
Results04.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
@page "/Results04/{SearchTerm}"
@inject NavigationManager NavigationManager
@using MongoDB.Bson;
@using MongoDB.Driver;
@using AtlasSearchNYC.Datamodels;
@using System;
<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>
<div class="col-auto">
<div class="input-group">
<select class="form-select form-select-lg sortbox" id="ddl" @onchange=ChangeSort>
<option>Release Year</option>
<option>Score</option>
</select>
</div>
</div>
</div>
<div class="row">
<SearchBarBasic SearchTerm="@SearchTerm" Redirect="Results04" OnSearchCallback="@ChangeSearchTerm" />
</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" />
}
}
</div>
@code {
[Parameter]
public string SearchTerm {get; set;}
private List<Datamodels.Movie> movies = new List<Movie>();
private string SortBy = "Release Year";
protected async void ChangeSort(ChangeEventArgs e) {
SortBy = e.Value.ToString();
await RunSearch();
}
protected override async Task OnInitializedAsync()
{
await RunSearch();
}
private async void ChangeSearchTerm(string term) {
Console.WriteLine("R04 - ChangeSearchTerm " + term);
SearchTerm = term;
NavigationManager.NavigateTo("/Results04/" + SearchTerm);
await RunSearch();
}
private async Task RunSearch() {
var pipeline = new BsonDocument[] {
new BsonDocument("$search",
new BsonDocument
{
{ "index", "sort" },
{ "text",
new BsonDocument
{
{ "query", SearchTerm },
{ "path",
new BsonArray
{
"title",
"fullplot"
} }
} },
{ "sortBetaV1",
new BsonDocument("year", -1) },
{ "highlight",
new BsonDocument("path",
new BsonArray
{
"fullplot",
"title"
}) }
}),
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") }
}),
new BsonDocument("$limit", 25)
};
@if(SortBy == "Score") {
pipeline = new BsonDocument[] {
new BsonDocument("$search",
new BsonDocument
{
{ "index", "sort" },
{ "text",
new BsonDocument
{
{ "query", SearchTerm },
{ "path",
new BsonArray
{
"title",
"fullplot"
} }
} },
{ "highlight",
new BsonDocument("path",
new BsonArray
{
"fullplot",
"title"
}) }
}),
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") }
}),
new BsonDocument("$limit", 25)
};
}
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>(pipeline).ToListAsync();
movies = result;
}
}