Copied to clipboard

Flag this post as spam?

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


  • Adam 4 posts 24 karma points
    Jun 26, 2012 @ 21:31
    Adam
    0

    Razor Search problem

    Hello,

    I have a problem with Razor Search on CWS - when I put value in search box and click search button everything works perfectly. But when i leave empty field and click search button I get this error: Error loading MacroEngine script (file: Search.cshtml). I cannot solve this...

    Adam

     

  • Grant Thomas 291 posts 324 karma points
    Jun 27, 2012 @ 10:50
    Grant Thomas
    0

    You need to provide more information, if you will.

  • Adam 4 posts 24 karma points
    Jun 27, 2012 @ 11:08
    Adam
    0

    I use standard Razor search - script file you can see below:

     

    @using Examine
    @using System.Text.RegularExpressions
    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
        // retrieve the search term from the query string
        var searchTerm Request.QueryString["search"];

        // Get the searcher from examine
        var searcher ExamineManager.Instance.SearchProviderCollection["CWSSearcher"];
        
        // Do the search
        
        var results searcher.Search(searchTermtrue);
        
        <div id="search">
            <h2 class="flashHeader"><strong>Search Results</strong></h2>
            @ShowSummary(resultssearchTerm)
            <div id="search_results">
            @foreach (var result in results)
            {
                @DisplaySearchResult(resultsearchTerm
            }
            </div>
        </div>
    }

    @helper ShowSummary(ISearchResults resultsstring searchTerm)
    {
        var count results.Count();
        <id="search_summary">
        @switch (count)
        {
            case 0:
                @Html.Raw("No matches were found for "<strong>@searchTerm</strong>
                break;
            case 1:
                @Html.Raw("Your search for ")<strong>@searchTerm</strong>@Html.Raw(" matches ")<strong>1</strong>@Html.Raw(" page")
                break;
            default:
                @Html.Raw("Your search for ")<strong>@searchTerm</strong>@Html.Raw(" matches ")<strong>@count</strong>@Html.Raw(" pages")
                break;
        }
        </p>
    }

    @helper DisplaySearchResult(SearchResult resultstring searchTerm)
    {
        // Initialize title and description
        var title string.Empty;
        var description string.Empty;

        // Get the nodeTypeAlias and based on thisset the title and description
        var nodeTypeAlias result.Fields["nodeTypeAlias"];
        switch(nodeTypeAlias)
        {
            case "CWS_Home":
            case "CWS_Textpage":
                title result.Fields["headerText"];
                description result.Fields["bodyText"];
                break;
            case "CWS_NewsItem":
            case "CWS_EventItem":
                title result.Fields["nodeName"];
                description result.Fields["bodyText"];
                break;
            case "CWS_TextpageTwoCol":
                title result.Fields["headerText"];
                description string.Concat(result.Fields["bodyTextColOne"]result.Fields["bodyTextColTwo"]);
                break;
        }

        // Format the description
        description FormatText(descriptionsearchTerm);
        
        // Get the item url
        var url Library.NodeById(result.Fields["id"]).Url;

        <div class="search_result">
            <class="search_result_title">
                <href="@url" class="search_title">@title</a>
            </p>
            <class="search_result_description">
                <span class="search_description">@Html.Raw(description)</span>
            </p>
        </div>
        
    }

    @functions
    {
        static string FormatText(string textstring matchCase)
        {
            if (string.IsNullOrEmpty(text|string.IsNullOrEmpty(matchCase))
            {
                return string.Empty;
            }

            var regex new Regex(string.Format(@"({0})"matchCase)RegexOptions.IgnoreCase);
            string[sentences regex.Split(text);
            if (sentences.Length &!sentences[0].Equals(matchCase))
            {
                if (sentences[0].Length 60)
                {
                    text "... " text.Remove(0sentences[0].Length 40);
                }
            }

            if (text.Length 200)
            {
                text text.Substring(0200" ...";
            }

            text regex.Replace(textstring.Format("<strong>{0}</strong>"matchCase));
            return text;
        }
    }

    What kind of information do you need?


  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jun 27, 2012 @ 12:35
    Michael Latouche
    0

    Hi Adam,

    I'm not sure as I haven't use examine yet, but could it be that the searcher.Search fails is the search term is empty or null?

    It also depends on what you want to do idf the search term is empty, but maybe you can try checking if the searchTerm is null or empty prior to launching the search, and if it is, display a warning message "please fill in a search term" or something like that? I guess the idea is not to return the whole site pages in case the search term is empty.

    Hope this helps.

    Cheers,

    Michael.

  • Adam 4 posts 24 karma points
    Jun 27, 2012 @ 12:55
    Adam
    0

    I tried to add if clause but it also throws exception (see below)

    @using Examine
    @using System.Text.RegularExpressions
    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
        // retrieve the search term from the query string
        var searchTerm Request.QueryString["search"];

        // Get the searcher from examine
        var searcher ExamineManager.Instance.SearchProviderCollection["CWSSearcher"];
        
        // Do the search
        if (searchTerm != null)

    {
        var results searcher.Search(searchTermtrue);
        
        <div id="search">
            <h2 class="flashHeader"><strong>Search Results</strong></h2>
            @ShowSummary(resultssearchTerm)
            <div id="search_results">
            @foreach (var result in results)
            {
                @DisplaySearchResult(resultsearchTerm
            }
            </div>
            </div>

    }
    }

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jun 27, 2012 @ 13:14
    Michael Latouche
    0

    Hi Adam,

    I think cheking on null will only work if you actually do not have the parameter "search" in your query string. In your case it will always be there, if you leave it blank, you will get an empty string. So I think it would work if you use something like

    if (!string.IsNullOrWhiteSpace(searchTerm))

    or

    if (!string.IsNullOrWhiteSpace(searchTerm))

    Cheers,

    Michael.

  • Adam 4 posts 24 karma points
    Jun 27, 2012 @ 13:34
    Adam
    0

    Great!! That's working - thank you

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jun 27, 2012 @ 14:43
    Michael Latouche
    0

    Nice :-)

    Can you maybe mark my previous post as the answer to the thread, so that other people having similar problem might find the solution more easily? Thx!

    Cheers,

    Michael.

Please Sign in or register to post replies

Write your reply to:

Draft