-
Notifications
You must be signed in to change notification settings - Fork 5
/
SearchHelper.cs
469 lines (347 loc) · 19 KB
/
SearchHelper.cs
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using Examine;
using Examine.Search;
using Skybrud.Essentials.Collections;
using Skybrud.Essentials.Strings.Extensions;
using Skybrud.Umbraco.Search.Constants;
using Skybrud.Umbraco.Search.Models;
using Skybrud.Umbraco.Search.Models.Groups;
using Skybrud.Umbraco.Search.Options;
using Skybrud.Umbraco.Search.Options.Pagination;
using Skybrud.Umbraco.Search.Options.Sorting;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
// ReSharper disable SuspiciousTypeConversion.Global
namespace Skybrud.Umbraco.Search {
/// <summary>
/// Helper class for making Examine searches.
/// </summary>
public class SearchHelper : ISearchHelper {
private readonly IExamineManager _examine;
private readonly ILogger _logger;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
#region Properties
/// <summary>
/// Dictionary describing how certain characters should be replaced in search queries.
/// </summary>
protected Dictionary<char, string> Diacritics { get; } = new Dictionary<char, string> {
// Examine/Lucene converts "æ" to "ae"
{ 'æ', "ae" },
// Examine/Lucene converts "ø" to "o", not "oe"
{ 'ø', "o" },
// Examine/Lucene converts "å" to "a", not "aa"
{ 'å', "a" }
// TODO: Add support for more characters (may be a long list)
};
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance based on the specified services.
/// </summary>
/// <param name="examine">The current Examine manager.</param>
/// <param name="logger">The current Umbraco logger.</param>
/// <param name="umbracoContextAccessor">The current Umbraco context accessor.</param>
public SearchHelper(IExamineManager examine, ILogger logger, IUmbracoContextAccessor umbracoContextAccessor) {
_examine = examine;
_logger = logger;
_umbracoContextAccessor = umbracoContextAccessor;
}
#endregion
#region Member methods
/// <summary>
/// Performs a search using the specified <paramref name="sortOptions"/>.
/// </summary>
/// <param name="operation">The boolean operation the search should be based on.</param>
/// <param name="sortOptions">The sort options specyfing how the results should be sorted.</param>
/// <param name="results">The results of the search.</param>
/// <param name="total">The total amount of results returned by the search.</param>
protected virtual void Execute(IBooleanOperation operation, ISortOptions sortOptions, out IEnumerable<ISearchResult> results, out long total) {
// Cast the boolean operation to IQueryExecutor
IQueryExecutor executor = operation;
// If "SortField" doesn't specified, we don't apply any sorting
if (!string.IsNullOrWhiteSpace(sortOptions.SortField)) {
switch (sortOptions.SortOrder) {
case SortOrder.Ascending:
executor = operation.OrderBy(new SortableField(sortOptions.SortField, sortOptions.SortType));
break;
case SortOrder.Descending:
executor = operation.OrderByDescending(new SortableField(sortOptions.SortField, sortOptions.SortType));
break;
default:
throw new Exception($"Unsupported sort order: {sortOptions.SortOrder}");
}
}
if (sortOptions is IOffsetOptions offset) {
ISearchResults allResults = executor.Execute();
// Update the total amount of results
total = allResults.TotalItemCount;
// Apply limit and offset
results = allResults
.Skip(offset.Offset)
.Take(offset.Limit);
} else {
// Since no offset or limit is specified, we request all results
ISearchResults allResults = executor.Execute(int.MaxValue);
// Update the total amount of results
total = allResults.TotalItemCount;
// Update "results" with the value of "allResults" as we're not filtering the results
results = allResults;
}
}
/// <summary>
/// Performs a search using the specified <paramref name="options"/>.
/// </summary>
/// <param name="operation">The boolean operation the search should be based on.</param>
/// <param name="options">The options specyfing how the results should be sorted.</param>
/// <param name="results">The results of the search.</param>
/// <param name="total">The total amount of results returned by the search.</param>
protected virtual void Execute(IBooleanOperation operation, ISearchOptions options, out IEnumerable<ISearchResult> results, out long total) {
// Cast the boolean operation to IQueryExecutor
IQueryExecutor executor = operation;
// Start the search in Examine
ISearchResults allResults = executor.Execute(int.MaxValue);
// Update the total amount of results
total = allResults.TotalItemCount;
results = allResults;
// If "options" implements the interface, results are sorted using the "Sort" method
if (options is IPostSortOptions postSort) results = postSort.Sort(results);
// If "options" implements implement the interface, the results are paginated
if (options is IOffsetOptions offset) results = results.Skip(offset.Offset).Take(offset.Limit);
}
/// <summary>
/// Performs a search using the specified <paramref name="options"/> and returns the result of that search.
/// </summary>
/// <param name="options">The options for the search.</param>
/// <returns>An instance of <see cref="SearchResultList"/> representing the result of the search.</returns>
public virtual SearchResultList Search(ISearchOptions options) {
// Start measuring the elapsed time
Stopwatch sw = Stopwatch.StartNew();
// Get the searcher from the options
ISearcher searcher = GetSearcher(options);
// Create a new Examine query
IQuery query = CreateQuery(searcher, options);
// Get the boolean operation via the options class
IBooleanOperation operation = options.GetBooleanOperation(this, searcher, query);
// Declare some variables
long total;
IEnumerable<ISearchResult> results;
switch (options) {
case IExecuteOptions execute:
execute.Execute(operation, out results, out total);
break;
case ISortOptions sortOptions:
Execute(operation, sortOptions, out results, out total);
break;
default:
Execute(operation, options, out results, out total);
break;
}
sw.Stop();
if (options is IDebugSearchOptions debug && debug.IsDebug) {
_logger.Debug<SearchHelper>("Search of type {Type} completed in {Milliseconds} with {Query}", options.GetType().FullName, sw.ElapsedMilliseconds, query);
}
// Wrap the results
return new SearchResultList(options, query, total, results);
}
/// <summary>
/// Performs a search using the specified <paramref name="options"/> and returns the result of that search.
///
/// Each item in the result is parsed to the type of <typeparamref name="TItem"/> using <paramref name="callback"/>.
/// </summary>
/// <typeparam name="TItem">The common output type of each item.</typeparam>
/// <param name="options">The options for the search.</param>
/// <param name="callback">A callback used for converting an <see cref="ISearchResult"/> to <typeparamref name="TItem"/>.</param>
/// <returns>An instance of <see cref="SearchResultList{TItem}"/> representing the result of the search.</returns>
public virtual SearchResultList<TItem> Search<TItem>(ISearchOptions options, Func<ISearchResult, TItem> callback) {
// Make the search in Examine
SearchResultList results = Search(options);
// Iterate through and call "callback" for each item
IEnumerable<TItem> items = results.Items
.Select(callback)
.Where(x => x != null);
// Wrap the result
return new SearchResultList<TItem>(options, results.Query, results.Total, items);
}
/// <summary>
/// Performs a search using the specified <paramref name="options"/> and returns the result of that search.
///
/// Each item in the result first found in either the content cache or media cache, and then parsed to the type of <typeparamref name="TItem"/> using <paramref name="callback"/>.
/// </summary>
/// <typeparam name="TItem">The common output type of each item.</typeparam>
/// <param name="options">The options for the search.</param>
/// <param name="callback">A callback used for converting an <see cref="IPublishedContent"/> to <typeparamref name="TItem"/>.</param>
/// <returns>An instance of <see cref="SearchResultList{TItem}"/> representing the result of the search.</returns>
public virtual SearchResultList<TItem> Search<TItem>(ISearchOptions options, Func<IPublishedContent, TItem> callback) {
// Make the search in Examine
SearchResultList results = Search(options);
// Iterate through the result and look up the content
IEnumerable<TItem> items = results.Items
.Select(GetPublishedContentFromResult)
.WhereNotNull()
.Select(callback)
.Where(x => x != null);
// Wrap the result
return new SearchResultList<TItem>(options, results.Query, results.Total, items);
}
/// <summary>
/// Performs a search using the specified <paramref name="options"/> and returns the result of that search.
///
/// Each item in the result first found in either the content cache or media cache, and then parsed to the type of <typeparamref name="TItem"/> using <paramref name="callback"/>.
/// </summary>
/// <typeparam name="TItem">The common output type of each item.</typeparam>
/// <param name="options">The options for the search.</param>
/// <param name="callback">A callback used for converting an <see cref="IPublishedContent"/> to <typeparamref name="TItem"/>.</param>
/// <returns>An instance of <see cref="SearchResultList{TItem}"/> representing the result of the search.</returns>
public virtual SearchResultList<TItem> Search<TItem>(ISearchOptions options, Func<IPublishedContent, ISearchResult, TItem> callback) {
// Make the search in Examine
SearchResultList results = Search(options);
// Map the search results
IEnumerable<TItem> items = (
from x in results.Items
let content = GetPublishedContentFromResult(x)
let result = callback(content, x)
where result != null
select result
);
// Wrap the result
return new SearchResultList<TItem>(options, results.Query, results.Total, items);
}
/// <summary>
/// Converts the specified <paramref name="result"/> into an instance of <see cref="IPublishedContent"/>.
///
/// The method will look at the <c>__IndexType</c> to determine the type of the result, and then use the
/// relevant published cache (eg. content or media) to lookup the <see cref="IPublishedContent"/> equivalent of
/// <paramref name="result"/>.
/// </summary>
/// <param name="result">The result to look up.</param>
/// <returns>An instance of <see cref="IPublishedContent"/>.</returns>
protected virtual IPublishedContent GetPublishedContentFromResult(ISearchResult result) {
string indexType = result.GetValues("__IndexType").FirstOrDefault();
switch (indexType) {
case "content":
return _umbracoContextAccessor.UmbracoContext.Content.GetById(int.Parse(result.Id));
case "media":
case "pdf":
return _umbracoContextAccessor.UmbracoContext.Media.GetById(int.Parse(result.Id));
default:
return null;
}
}
/// <summary>
/// Performs a search based on the specified <paramref name="request"/>.
/// </summary>
/// <param name="request">The request the search should be based on.</param>
/// <param name="groups">An array of groups to used for the search.</param>
/// <returns>An instance of <see cref="GroupedSearchResult"/>.</returns>
public virtual GroupedSearchResult Search(HttpRequestBase request, SearchGroup[] groups) {
int[] selectedGroups = request.QueryString["groups"].ToInt32Array();
IEnumerable<SearchGroupResultList> result = (
from x in groups
where selectedGroups.Length == 0 || selectedGroups.Contains(x.Id)
select x?.Callback(x, request, null)
);
return new GroupedSearchResult(result);
}
/// <summary>
/// Replaces and removes diacritics in the specified <paramref name="input"/> string.
/// </summary>
/// <param name="input">The string.</param>
/// <returns>The result of the replacement.</returns>
public virtual string ReplaceDiacritics(string input) {
// See: http://www.levibotelho.com/development/c-remove-diacritics-accents-from-a-string/
string normalizedString = input.Normalize(NormalizationForm.FormD);
StringBuilder sb = new StringBuilder();
foreach (char c in normalizedString) {
// Look for the character in the replacement table
if (Diacritics.TryGetValue(c, out string replacement)) {
sb.Append(replacement);
continue;
}
UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark) {
sb.Append(c);
}
}
return sb.ToString().Normalize(NormalizationForm.FormC);
}
/// <summary>
/// Removes diacritics in the specified <paramref name="input"/> string.
/// </summary>
/// <param name="input">The string.</param>
/// <returns>The result of the operation.</returns>
public virtual string RemoveDiacritics(string input) {
// See: http://www.levibotelho.com/development/c-remove-diacritics-accents-from-a-string/
string normalizedString = input.Normalize(NormalizationForm.FormD);
StringBuilder sb = new StringBuilder();
foreach (char c in normalizedString) {
UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark) {
sb.Append(c);
}
}
return sb.ToString().Normalize(NormalizationForm.FormC);
}
/// <summary>
/// Returns a <see cref="DateTime"/> parsed from the field with the specified <paramref name="key"/>.
/// </summary>
/// <param name="result">The result.</param>
/// <param name="key">The key of the field.</param>
/// <returns>An instance of <see cref="DateTime"/>.</returns>
public virtual DateTime GetSortValueByDateTime(ISearchResult result, string key) {
return SearchUtils.Sorting.GetSortValueByDateTime(result, key);
}
/// <summary>
/// Returns a <see cref="DateTime"/> parsed from the <c>contentDate</c> field, or <c>createDate</c> if nothing else is specified.
/// </summary>
/// <param name="result">The result.</param>
/// <returns>An instance of <see cref="DateTime"/>.</returns>
public virtual DateTime GetSortValueByContentDate(ISearchResult result) {
return SearchUtils.Sorting.GetSortValueByContentDate(result);
}
/// <summary>
/// Returns the <see cref="ISearcher"/> as specified by the specified <paramref name="options"/>.
///
/// If <paramref name="options"/> doesn't specify a searcher, the searcher of <c>ExternalIndex</c> will be used as fallback.
/// </summary>
/// <param name="options">The search options.</param>
/// <returns>The <see cref="ISearcher"/> to be used for the search.</returns>
protected virtual ISearcher GetSearcher(ISearchOptions options) {
ISearcher searcher;
switch (options) {
case ISearcherOptions searcherOptions:
searcher = searcherOptions.Searcher;
if (searcher != null) return searcher;
break;
case IGetSearcherOptions getSearcherOptions:
searcher = getSearcherOptions.GetSearcher(_examine, this);
if (searcher != null) return searcher;
break;
default:
if (_examine.TryGetIndex(ExamineConstants.ExternalIndexName, out IIndex index)) {
searcher = index.GetSearcher();
if (searcher != null) return searcher;
}
break;
}
throw new Exception($"Failed determining searcher from {options.GetType()}");
}
/// <summary>
/// Creates a new query from the specified <paramref name="searcher"/> and <paramref name="options"/>.
/// </summary>
/// <param name="searcher">The searcher.</param>
/// <param name="options">The options for the search.</param>
/// <returns>An instance of <see cref="IQuery"/>.</returns>
protected virtual IQuery CreateQuery(ISearcher searcher, ISearchOptions options) {
return searcher.CreateQuery();
}
#endregion
}
}