Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I have a razor macro in a template that will take the users input, find matching results and convert them to JSON for an autocomplete drop down.
This works great with "full" words. For example, if I type "Hello" in my input box, I'll get results, but if I type "Hell" I don't see any results.
If I visit "http://localhost:33968/searchJSON?term=starter", I see:
[{"id":"http://localhost:33968/learn/the-starter-kit/","label":"The starter kit","value":"The starter kit"}]
If I visit "http://localhost:33968/searchJSON?term=start", I see:
[]
Is there a way to append this to allow just a few characters for the search term?
I know I need to modify the query for Examine but just not sure how.
SearchJSON.cshtml
@using Examine @using Examine.SearchCriteria @using System.Web.Script.Serialization @{ var siteURL = "http://" + Request.Url.Authority; var searchTerm = Request["term"]; if (String.IsNullOrEmpty(searchTerm)) { 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); List<dynamic> searchResultKeyVals = new List<dynamic>(); 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); } @Html.Raw(JSONResults)
autocomplete.js (the minLength value does nothing in my instance)
$(function() { $("#searchTerm").autocomplete({ source: "/SearchJSON", minLength: 2, select: function(event, ui) { window.location.href = ui.item.id; } }); });
In your code you need to wildcard searchTerm
So add the following using
using UmbracoExamine.SearchCriteria;
then line
var query = searchCriteria.GroupedOr(new string[] { "nodeName", "bodyText" }, searchTerm).Compile();
change to
var query = searchCriteria.GroupedOr(new string[] { "nodeName", "bodyText" }, searchTerm.MultipleWildCard()).Compile();
Regards
Ismail
Hi Ismail,
When I make these modifications the macro breaks:
Error loading Partial View script (file: ~/Views/MacroPartials/SearchJSON.cshtml)
In fact, even just including the using breaks the script.
For some reason, I can't even debug it using ?umbDebugShowTrace=true (nothing happens).
Thanks
Fixed by adding:
@using Examine.LuceneEngine.SearchCriteria;
Thanks Ismail :)
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Exmamine - Search Term Matching - JSON Autocomplete
I have a razor macro in a template that will take the users input, find matching results and convert them to JSON for an autocomplete drop down.
This works great with "full" words. For example, if I type "Hello" in my input box, I'll get results, but if I type "Hell" I don't see any results.
If I visit "http://localhost:33968/searchJSON?term=starter", I see:
If I visit "http://localhost:33968/searchJSON?term=start", I see:
Is there a way to append this to allow just a few characters for the search term?
I know I need to modify the query for Examine but just not sure how.
SearchJSON.cshtml
autocomplete.js (the minLength value does nothing in my instance)
In your code you need to wildcard searchTerm
So add the following using
then line
change to
Regards
Ismail
Hi Ismail,
When I make these modifications the macro breaks:
Error loading Partial View script (file: ~/Views/MacroPartials/SearchJSON.cshtml)
In fact, even just including the using breaks the script.
For some reason, I can't even debug it using ?umbDebugShowTrace=true (nothing happens).
Thanks
Fixed by adding:
@using Examine.LuceneEngine.SearchCriteria;
Thanks Ismail :)
is working on a reply...