Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Devin 87 posts 251 karma points
    Jul 16, 2015 @ 08:37
    Devin
    0

    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:

    [{"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;
            }
        });
    });
    
  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jul 16, 2015 @ 11:04
    Ismail Mayat
    100

    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

  • Devin 87 posts 251 karma points
    Jul 16, 2015 @ 12:40
    Devin
    0

    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

  • Devin 87 posts 251 karma points
    Jul 16, 2015 @ 12:59
    Devin
    0

    Fixed by adding:

    @using Examine.LuceneEngine.SearchCriteria;

    Thanks Ismail :)

Please Sign in or register to post replies

Write your reply to:

Draft