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...
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(searchTerm, true); <div id="search"> <h2 class="flashHeader"><strong>Search Results</strong></h2> @ShowSummary(results, searchTerm) <div id="search_results"> @foreach (var result in results) { @DisplaySearchResult(result, searchTerm) } </div> </div> }
@helper ShowSummary(ISearchResults results, string searchTerm) { var count = results.Count(); <p 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 result, string searchTerm) { // Initialize title and description var title = string.Empty; var description = string.Empty;
// Get the nodeTypeAlias and based on this, set 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(description, searchTerm); // Get the item url var url = Library.NodeById(result.Fields["id"]).Url;
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.
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(searchTerm, true); <div id="search"> <h2 class="flashHeader"><strong>Search Results</strong></h2> @ShowSummary(results, searchTerm) <div id="search_results"> @foreach (var result in results) { @DisplaySearchResult(result, searchTerm) } </div> </div>
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
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
You need to provide more information, if you will.
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(searchTerm, true);
<div id="search">
<h2 class="flashHeader"><strong>Search Results</strong></h2>
@ShowSummary(results, searchTerm)
<div id="search_results">
@foreach (var result in results)
{
@DisplaySearchResult(result, searchTerm)
}
</div>
</div>
}
@helper ShowSummary(ISearchResults results, string searchTerm)
{
var count = results.Count();
<p 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 result, string searchTerm)
{
// Initialize title and description
var title = string.Empty;
var description = string.Empty;
// Get the nodeTypeAlias and based on this, set 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(description, searchTerm);
// Get the item url
var url = Library.NodeById(result.Fields["id"]).Url;
<div class="search_result">
<p class="search_result_title">
<a href="@url" class="search_title">@title</a>
</p>
<p class="search_result_description">
<span class="search_description">@Html.Raw(description)</span>
</p>
</div>
}
@functions
{
static string FormatText(string text, string 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 > 2 && !sentences[0].Equals(matchCase))
{
if (sentences[0].Length > 60)
{
text = "... " + text.Remove(0, sentences[0].Length - 40);
}
}
if (text.Length > 200)
{
text = text.Substring(0, 200) + " ...";
}
text = regex.Replace(text, string.Format("<strong>{0}</strong>", matchCase));
return text;
}
}
What kind of information do you need?
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.
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(searchTerm, true);
<div id="search">
<h2 class="flashHeader"><strong>Search Results</strong></h2>
@ShowSummary(results, searchTerm)
<div id="search_results">
@foreach (var result in results)
{
@DisplaySearchResult(result, searchTerm)
}
</div>
</div>
}
}
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
or
Cheers,
Michael.
Great!! That's working - thank you
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.
is working on a reply...