$(function() {
$( "#searchTerm" ).autocomplete({
source: "/SearchJSON",
minLength: 2,
select: function (event, ui) {
//Redirect user when item selected from the id in the JSON
window.location.href = ui.item.id;
}
});
});
Search_JSON.cshtml
@using Examine
@using Examine.SearchCriteria
@using System.Web.Script.Serialization
@{
//Get the domain (http://localhost:6436)
var siteURL = "http://" + Request.Url.Authority;
//Get the values posted from the form
var searchTerm = Request["term"];
//Check if searchTerm is null from the posted form data...
if (String.IsNullOrEmpty(searchTerm))
{
//Stop all other code running in this Macro
return;
}
var searcher = ExamineManager.Instance.SearchProviderCollection["RazorSiteSearcher"];
var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
var query = searchCriteria.GroupedOr(new string[] { "nodeName", "bodyText" }, searchTerm).Compile();
var searchResults = searcher.Search(query);
/*
EXAMPLE JSON
[{ "id": "http://localhost/about.aspx", "label": "About", "value": "About" }]
*/
List<dynamic> searchResultKeyVals = new List<dynamic>();
//Convert the search results as JSON
foreach(var result in searchResults)
{
searchResultKeyVals.Add(new {
id = siteURL + umbraco.library.NiceUrl(result.Id),
label = result.Fields["nodeName"],
value = result.Fields["nodeName"]
});
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
var JSONResults = serializer.Serialize(searchResultKeyVals);
}
@* Ouput the JSON *@
@Html.Raw(JSONResults)
Search_Results.cshtml
@using Examine
@using Examine.SearchCriteria
@{
//Get the values posted from the form
var searchTerm = Request.Form["searchTerm"];
//Check if searchQuery is null from the posted form data...
if (searchTerm == null)
{
//If it is null then the form was not posted and the page was visited directly
<p>Please use the search box</p>
//Stop all other code running in this Macro
return;
}
var searcher = ExamineManager.Instance.SearchProviderCollection["RazorSiteSearcher"];
var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
var query = searchCriteria.GroupedOr(new string[] { "nodeName", "bodyText" }, searchTerm).Compile();
var searchResults = searcher.Search(query);
var noResults = searchResults.Count();
<p>You searched for @searchTerm, and found @noResults results</p>
<ul class="search-results">
@foreach (var result in searchResults)
{
<li>
<a href="@umbraco.library.NiceUrl(result.Id)">@result.Fields["nodeName"]</a>
</li>
}
</ul>
}
Can you help me Devin, I get the following error when typing something in the input I dont get a dropdown. If I look in the bottom of my webpage I see this HTML for the autocomplete:
Examine JSON search - Autocomplete not working
I have a working search box thanks to the following video tutorial here:
http://umbraco.com/help-and-support/video-tutorials/umbraco-fundamentals/razor-recipes/search/
The problem is, auto complete is not working for me and keeps displaying "No search results."
When I type the word "Umbraco" into the input, I do see the following in the console, but it's not autocompleting:
HTML
autocomplete.js
Search_JSON.cshtml
Search_Results.cshtml
Solved it myself - I had incorrect naming with aliases lol.
Can you help me Devin, I get the following error when typing something in the input I dont get a dropdown. If I look in the bottom of my webpage I see this HTML for the autocomplete:
If I go to
http://localhost:52242/json?term=epic
I get a fine list of everything. But the autocomplete can't find anything.is working on a reply...