forked from episerver/cms-ui-extension-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomSearchQuery.cs
52 lines (49 loc) · 2.13 KB
/
CustomSearchQuery.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
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Cms.Shell.UI.Rest.ContentQuery;
using EPiServer.Core;
using EPiServer.ServiceLocation;
using EPiServer.Shell.ContentQuery;
using EPiServer.Shell.Search;
using EPiServer.Web.Routing;
namespace EPiServer.Templates.Alloy.UIExtensions.CustomSearch
{
[ServiceConfiguration(typeof(IContentQuery))]
public class CustomSearchQuery : ContentQueryBase
{
private readonly IContentRepository _contentRepository;
private readonly SearchProvidersManager _searchProvidersManager;
private readonly LanguageSelectorFactory _languageSelectorFactory;
public CustomSearchQuery(
IContentQueryHelper queryHelper,
IContentRepository contentRepository,
SearchProvidersManager searchProvidersManager,
LanguageSelectorFactory languageSelectorFactory)
: base(contentRepository, queryHelper)
{
_contentRepository = contentRepository;
_searchProvidersManager = searchProvidersManager;
_languageSelectorFactory = languageSelectorFactory;
}
/// <summary>
/// The key to trigger this query.
/// </summary>
public override string Name
{
get { return "CustomQuery"; }
}
protected override IEnumerable<IContent> GetContent(ContentQueryParameters parameters)
{
var queryText = HttpUtility.HtmlDecode(parameters.AllParameters["queryText"]);
var searchQuery = new Query(queryText);
var contentReferences = Enumerable.Empty<ContentReference>();
var searchProvider = _searchProvidersManager.GetEnabledProvidersByPriority("CMS/Pages", true).FirstOrDefault();
if (searchProvider != null)
{
contentReferences = searchProvider.Search(searchQuery).Select(result => ContentReference.Parse(result.Metadata["Id"])).Distinct();
}
return _contentRepository.GetItems(contentReferences, _languageSelectorFactory.AutoDetect(parameters.AllLanguages));
}
}
}