Last active
September 27, 2019 18:54
-
-
Save icavalheiro/6fdb16c08a66864017c9cfc4f3f5f1ff to your computer and use it in GitHub Desktop.
Umbraco search controller based on contenttype controllers. Returns json's as responses.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Mvc; | |
using Umbraco.Core.Models.PublishedContent; | |
using Umbraco.Web.Mvc; | |
namespace YOUR_APP_NAME.Controllers | |
{ | |
public abstract class SearchController : RenderMvcController | |
{ | |
/// <summary> | |
/// Class meant to facilitate the serialization of the search result | |
/// </summary> | |
public class SearchElement | |
{ | |
public string Name; | |
public string Key; | |
public string Id; | |
public string Url; | |
public float Score; | |
public SearchElement(IPublishedContent content, float score) | |
{ | |
Name = content.Name; | |
Key = content.Key.ToString(); | |
Id = content.Id.ToString(); | |
Score = score; | |
Url = content.Url; | |
} | |
} | |
/// <summary> | |
/// Get the current query from the request | |
/// </summary> | |
/// <returns>The query</returns> | |
protected virtual string GetQuery() | |
{ | |
var query = Request["query"]; | |
if (query == null) | |
query = ""; | |
return query; | |
} | |
/// <summary> | |
/// Get the current language fromt the request. If none is set will search in all | |
/// </summary> | |
/// <returns>The language to be used in the search</returns> | |
protected virtual string GetLanguage() | |
{ | |
var lang = Request["lang"]; | |
if (lang == null) | |
return "*"; | |
return lang; | |
} | |
/// <summary> | |
/// Get the current search results | |
/// </summary> | |
/// <returns>The serach elements corresponding to what was searched</returns> | |
protected virtual IEnumerable<SearchElement> GetResult() | |
{ | |
var query = GetQuery(); | |
var results = Umbraco.ContentQuery | |
.Search(query, GetLanguage()) | |
.Select(x => new SearchElement(x.Content, x.Score)) | |
.AsQueryable(); | |
results = FilterSearch(results); | |
return results.ToArray(); | |
} | |
/// <summary> | |
/// Get the current search as a json string | |
/// </summary> | |
/// <returns>The json string</returns> | |
protected virtual string GetJson() | |
{ | |
return JsonConvert.SerializeObject(GetResult()); | |
} | |
/// <summary> | |
/// Get the current search response | |
/// </summary> | |
/// <returns>The response</returns> | |
protected virtual ActionResult GetResponse() | |
{ | |
return Content(GetJson(), "application/json"); | |
} | |
/// <summary> | |
/// Used to filter the search somehow. | |
/// You could implement here a filter that gets a variable form the request query | |
/// and somehow apply this rule to the response... | |
/// </summary> | |
/// <param name="input">The current search result without being filtered</param> | |
/// <returns>The output search results that were filtered</returns> | |
protected virtual IQueryable<SearchElement> FilterSearch(IQueryable<SearchElement> input) | |
{ | |
return input; | |
} | |
[HttpGet] | |
public ActionResult Index() | |
{ | |
return GetResponse(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment