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 15, 2015 @ 11:49
    Devin
    1

    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:

    [{"id":"http://localhost:33968/explore/our-umbraco/","label":"Our Umbraco","value":"Our Umbraco"},{"id" :"http://localhost:33968/extend/umbraco-forms/","label":"Umbraco Forms","value":"Umbraco Forms"}]
    

    HTML

    <form method="post" action="/results" name="searchTerm">
        <input type="text" id="searchTerm" name="searchTerm" size="21" maxlength="120">
        <input type="submit" value="Search">
    </form>
    

    autocomplete.js

    $(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>
    }
    
  • Devin 87 posts 251 karma points
    Jul 15, 2015 @ 15:07
    Devin
    0

    Solved it myself - I had incorrect naming with aliases lol.

  • MB 273 posts 936 karma points
    Mar 12, 2017 @ 13:55
    MB
    0

    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:

    <div role="status" aria-live="assertive" aria-relevant="additions" class="ui-helper-hidden-accessible">
    <div>No search results.</div>
    </div>
    

    If I go to http://localhost:52242/json?term=epic I get a fine list of everything. But the autocomplete can't find anything.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies