Copied to clipboard

Flag this post as spam?

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


  • Rayyan 46 posts 238 karma points
    Aug 07, 2017 @ 08:20
    Rayyan
    0

    Define Search Term in search results pagination

    Hello, I'm working on Search Results Pagination but i couldn't define the Search Term to use it in the pagination, here's my code;

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using System.Web.Mvc.Html
    @using Umbraco.Web
    @using UmbracoExamine
    @using Examine.LuceneEngine.SearchCriteria;
    @using Examine.LuceneEngine;
    @using Examine.LuceneEngine.Providers;
    @{
        Layout = "Master.cshtml";
        string  term ="productName:" + Request.QueryString["searchTerm"]  + "* productDes:"+ Request.QueryString["searchTerm"]  +"*"    ;
        //retrieve current page from query string
        int currentPage = 1;
        if (!String.IsNullOrEmpty(Request.QueryString["page"]))
        {
            int.TryParse(Request.QueryString["page"], out currentPage);
        }
    
        var noresults = false;
        //container for the searcher
        Examine.Providers.BaseSearchProvider searcher = null;
        //container for the results
        ISearchResults searchResults = null;
        //number of results
        var resultsCount = 0;
        //required results per page
        var pageSize = 10;
        if (Model.Content.HasValue("numberOfItemsPerPage"))
        {
            pageSize = Model.Content.GetPropertyValue<int>("numberOfItemsPerPage");
        }
    
    }
    
    <div class="container">
        <div class="row">
            <div class="col-md-8">
                <div class="searchedKeyword">
            <span class="amman"></span> <span class="keyword">@term</span>
                </div>
                @{
                    if (term != null)
                    {
    
                        //now we need to work out which searcher to use
                        //so we'll switch/case on the filter  parameter in the querystring
    
                        searcher = ExamineManager.Instance.SearchProviderCollection["productSearcher"];
    
    
                        //N.B. The following is REALLY basic but should get you started. It's this area where we could really go to town with Lucene.
    
                        //create search criteria
                        var searchCriteria = searcher.CreateSearchCriteria();
    
                        //pass the criteria our search term as a raw query
                        var query = searchCriteria.RawQuery(term);
    
                        //action a search based on our query
                        searchResults = searcher.Search(query);
                        resultsCount = searchResults.TotalItemCount;
                        if (resultsCount < 1)
                        {
                            noresults = true;
                        }
                    }
                    else
                    {
                        noresults = true;
                    }
    
                    if (noresults)
                    {
                        <div class="noresults media">
                            <h4>
                                     !
                            </h4>
                            <h5>
                                               .
                            </h5>
                        </div>
                    }
                    else
                    {
                        //number of pages
                        int totalPages = 1;
                        if (resultsCount > pageSize)
                        {
                            totalPages = (int)Math.Ceiling((double)resultsCount / pageSize);
    
                            if (currentPage > totalPages)
                            {
                                currentPage = totalPages;
                            }
                            else if (currentPage < 1)
                            {
                                currentPage = 1;
                            }
    
                            <div>
                                @resultsCount results
                            </div>
    
                            <div>
                                Page @currentPage of @totalPages pages
                            </div>
                        }
    
                        //iterate the results
                        //we could be clever here using linq queries but I want
                        //to keep it simple, easily readable and understandable
                        //for this exercise
                        foreach (var searchResult in searchResults.Skip((currentPage - 1) * pageSize).Take(pageSize))
                        {
                            @Html.Partial("ListproductItem", new ViewDataDictionary { { "productId", searchResult.Id } })
                        }
    
                        <nav aria-label="Page navigation">
                            <ul class="pagination">
                                <li class="@(currentPage == 1 ? "disabled" : "")">
                                    <a href="@(currentPage > 1 ? "/search-results?searchTerm=" + searchTerm + "&page=" + (currentPage - 1).ToString() : "#")" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                                @for (var i = 1; i <= totalPages; i++)
                                {
                                    <li>
                                        <a href="/search-results?term=@searchTerm &page=@i">@i</a>
                                    </li>
                                }
                                <li class="@(currentPage == totalPages ? "disabled" : "")">
                                    <a href="@(currentPage < totalPages ? "/search-results?searchTerm=" + searchTerm + "&page=" + (currentPage + 1).ToString() : "#")" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            </ul>
                        </nav>
                    }
                }
            </div>
        </div>
    </div>
    

    The URL i'm getting from the code above showing below, which is not correct, (d) is the keyword that i searched for;

    domain.com/search-results?searchTerm=productName:d*%20productDes:d*&page=1
    

    And the right URL that i'm seeking as follows:

    domain.com/search-results?searchTerm=d&page=1
    

    Thanks indeed!

  • Rayyan 46 posts 238 karma points
    Aug 08, 2017 @ 08:13
    Rayyan
    101

    SOLVED, the code above is missing this line,

    var searchTerm = Request["searchTerm"];
    
  • 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