Copied to clipboard

Flag this post as spam?

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


  • Busra Sengul 9 posts 112 karma points MVP 4x c-trib
    Feb 19, 2019 @ 15:58
    Busra Sengul
    0

    SearchResults with macro pages

    Hey everyone,

    Would you please check my code and let me know what I'm missing to show the search results on the page?

    My partial view macro file,

    @using Examine.LuceneEngine.SearchCriteria
    @using System
    @using System.Linq
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
    
        var searchQuery= Request.QueryString["query"];
        if(String.IsNullOrWhiteSpace(searchQuery))
            {
                 searchQuery = "";
            }
        var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
        var searchCriteria = searcher.CreateSearchCriteria();
        var query = searchCriteria.GroupedOr( new []{"nodeName", "name", "title", "bodyText"},searchQuery).Compile();
        var SearchResults = searcher.Search(query).Where(x => x["__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();
    
                        if (node != null)
                        {
                         <div class="product col-sm-6 col-md-3 col-xs-6">
                                 <a href="@node.Url" title="@node.Name">@node.Name</a>
                         </div>
                        }
                    }
                 </ul>
        }
        else
        {
         <p>
    
          @if(!String.IsNullOrWhiteSpace(searchQuery))
            {
             <text> 'No results found for : @searchQuery</text>
             }
         </p>
        }
    

    My Search Results page(Where I want to show the results);

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = "Master.cshtml";
    }
    <div class="container-fluid">
        <div class="container">
            <div class="row">
                <div class="col-lg-6 pull-left">
                    @CurrentPage.bodyText
                </div>
                <div class="col-lg-offset-1">
    
                </div>
                <div class="col-lg-3 pull-right">
    
                </div>
            </div>
        </div>
    </div>
    

    My Navigation page's related part for searching;

    <div class="col-lg-3 input-group">
        <form action="@Umbraco.Content(1149).Url()" method="post" target="_blank">
            <input type="search" class="form-control" placeholder="Search" name="query" />
            <span class="input-group-btn"><button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button></span>
        </form>
    </div>
    

    I did the configuration on ~/config/ExamineSettings.config and ~/config/ExamineIndex.config

    Used this link; https://our.umbraco.com/documentation/reference/searching/examine/quick-start

    My macro is allowed in rich text editors and my text editor's alias is bodyText.

    What am I missing?

    (Ignore the styling, they are just for proper look)

  • Ravi Motha 290 posts 500 karma points MVP 8x c-trib
    Feb 19, 2019 @ 17:01
    Ravi Motha
    0

    have you checked there are records in the Examine index.. have a look at the developer tab and examine management..

  • Busra Sengul 9 posts 112 karma points MVP 4x c-trib
    Feb 20, 2019 @ 09:27
    Busra Sengul
    0

    Hey Ravi, Thanks for reply.

    I've checked Examine Index and Examine settings on developer tab as well. It seem to not working still.

    Still not seeing when I search something.

    My searchResults page shows the body text but does not include the macro in there. Still confused.

    Thanks in advance

  • Ravi Motha 290 posts 500 karma points MVP 8x c-trib
    Feb 20, 2019 @ 09:29
    Ravi Motha
    0

    okay next steps have you tried debugging it?? and stepping through to see if the search is a getting pages and then why its not displaying them..

    I'll see if i can have a tinker later..

  • Ravi Motha 290 posts 500 karma points MVP 8x c-trib
    Feb 20, 2019 @ 09:31
    Ravi Motha
    0

    also is there any particular reason you are using a macro??

  • Busra Sengul 9 posts 112 karma points MVP 4x c-trib
    Feb 20, 2019 @ 09:41
    Busra Sengul
    0

    It doesn't seem like searching whatever I wrote in the

      if(String.IsNullOrWhiteSpace(searchQuery))
            {
                 searchQuery = "";
            }
        var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
        var searchCriteria = searcher.CreateSearchCriteria();
        var query = searchCriteria.GroupedOr( new []{"nodeName", "name", "title", "bodyText"},searchQuery).Compile();
        var SearchResults = searcher.Search(query).Where(x => x["__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();
    
                        if (node != null)
                        {
                         <div class="product col-sm-6 col-md-3 col-xs-6">
                                 <a href="@node.Url" title="@node.Name">@node.Name</a>
                         </div>
                        }
                    }
                 </ul>
        }
        else
        {
         <p>
    
          @if(!String.IsNullOrWhiteSpace(searchQuery))
            {
             <text> 'No results found for : @searchQuery</text>
             }
         </p>
        }
    

    It jumps straight to else no matter I search. Even though the words I am sure they are in the content.

    I am so new in Umbraco, I am just fallowing a video set. But got stuck because I hate going on without solving the problem.

    I saw other search methods but I really want to figure this out.

    Thank you

  • Ravi Motha 290 posts 500 karma points MVP 8x c-trib
    Feb 20, 2019 @ 09:50
    Ravi Motha
    0

    so when you post the page to the macro you can see the search term in the macro

    So

    is posting and you can see that value in your macro here: var searchQuery= Request.QueryString["query"]; if(String.IsNullOrWhiteSpace(searchQuery)) { searchQuery = ""; }

  • Busra Sengul 9 posts 112 karma points MVP 4x c-trib
    Feb 20, 2019 @ 09:54
    Busra Sengul
    0

    No searchQuery doesn't carry the value I wrote

  • Busra Sengul 9 posts 112 karma points MVP 4x c-trib
    Feb 20, 2019 @ 16:26
    Busra Sengul
    101

    Fixed the problem. Took me a day and a half.

    I changed

     var searchQuery= Request.QueryString["query"];
    

    to

    string searchQuery= Request["query"];
    

    Now It's working!!

    Thanks for the help though :)

  • Ravi Motha 290 posts 500 karma points MVP 8x c-trib
    Feb 20, 2019 @ 17:27
    Ravi Motha
    1

    oh should have seen that.. so essentially your query was always null.. well done on getting there..

    its always the same start with the basics and work up to the difficult

Please Sign in or register to post replies

Write your reply to:

Draft