Copied to clipboard

Flag this post as spam?

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


  • Tom Engan 430 posts 1173 karma points
    Oct 06, 2015 @ 14:01
    Tom Engan
    0

    Need a searchbox to a UmbracoExamine searchside

    Have a search page that works by entering search-results?keywords=[something], and the code looks like this:

    @using Examine.LuceneEngine.SearchCriteria
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        string searchTerm = Request.QueryString["keywords"];
        if (String.IsNullOrWhiteSpace(searchTerm))
        {
            searchTerm = "";
        }
        var searcher = ExamineManager.Instance;
        var searchCriteria = searcher.CreateSearchCriteria();
        var query = searchCriteria.GroupedOr(new[] { "nodeName", "name", "title", "bodyText", "seo" }, searchTerm).Compile();
        var searchResults = searcher.Search(query).Where(r => r["__IndexType"] == "content").ToList();
    }
    
    @if (searchResults.Any())
    {
        <ul class="search-results-box">
            @foreach (var result in searchResults)
            {
                var node = Umbraco.TypedContent(result.Id);
                var pathIds = result["__Path"].Split(',');
                var path = Umbraco.TypedContent(pathIds).Where(p => p != null).Select(p=> new {p.Name}).ToList();
                <li>
                    <section class="results-box">
                        <h3>
                            <a href="@node.Url">@String.Join(" > ", path.Select(p => p.Name))</a>
                        </h3>
                        <a href="@node.Url" class="results-url">@node.Url</a>
    
                        @if (result.Fields.ContainsKey("title"))
                        {
                            <p class="results-title"><strong>@result["title"]</strong></p>
                        }
    
                        @if (result.Fields.ContainsKey("bodyText"))
                        {
                            <p>@result["bodyText"].Truncate(250)</p>
                        }
                    </section>
                    <hr />
                </li>
            }
        </ul> 
    }
    else
    {
        <p>
            There are no results matching your search criteria: 
    
                @if (!String.IsNullOrWhiteSpace(searchTerm))
                {
                    <text>'@searchTerm'</text>
                }
        </p>
    }
    

    This is working when I write a word direct in the end of the querystring in the URL (but not yet with special characters like Norwegian ÆØÅ).

    Someone who knows how to associate a search box to search in this searchpage with name search-results?

    Have found some answers like these, but it not working as intended:

    <div class="box hidden-lg hidden-md hidden-sm pull-left">
    <div class="container-2">
        <span class="icon"><i class="fa fa-search"></i></span>
        <input type="search" name="keywords" class="form-control" id="search" placeholder="Søk.." onkeydown="Javascript: if (event.keyCode==13) searchRESLT();"/>
    </div>
    

    <script>
        function searchRESLT() {window.parent.location} = "/search-results?=" + document.getElementById("search").value;        
    </script>
    
  • Tom Engan 430 posts 1173 karma points
    Oct 19, 2015 @ 08:39
    Tom Engan
    100

    I'ts ok now. The solution was to include also onClick="Javascript: searchRESLT();" on the icon:

      <div class="box">
        <div class="container-2">            
              <span onClick="Javascript: searchRESLT();" class="icon"><span class="fa fa-search"></span></span>
              <input type="search" name="keywords" class="form-control" id="search" onKeydown="Javascript: if (event.keyCode==13) searchRESLT();" placeholder="Search.."/>
        </div>
      </div>
    

    The {} in the script was placed on the wrong place:

    function searchRESLT() {
        window.parent.location="/search-results?keywords="+document.getElementById("search").value
    };
    

    Case closed.

  • 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