Abstract
This is a continuation of our Implementing Lucene search with Sitecore CMS example. In this section we will build the search results control (sublayout).
Building the search results sublayout
Using either Developer Center or Visual Studio, create a new sublayout that will process the results of submitted by the search box sublayout created in part 1 of this sample. The code for the layout will look like the following:
<h3>Search Results</h3>
<p>
<asp:Label ID="lblSearchString" runat="server" Text="Label"></asp:Label><br />
<asp:Literal ID="literalResultCount" runat="server" /><br />
<asp:Repeater runat='server' ID="rptResults">
<ItemTemplate>
<div class="result">
<span class="searchlink"><a href='<%# Eval("Link") %>'>
<%# Eval("Title") %></a></span>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:Literal runat="server" ID="litResults"></asp:Literal>
<asp:Panel ID="pnResultsPanel" runat="server"></asp:Panel>
</p>
All of the heavy lifting is handled in the code behind for the search results sublayout.
using System;
using System.Web.UI;
using System.Collections.Generic;
using Sitecore.Data.Fields;
using Sitecore.Links;
using Sitecore.Web;
using Lucene.Net.Search;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.QueryParsers;
using Sitecore.Data.Indexing;
using Sitecore.Data.Items;
using Sitecore.Globalization;
using Sitecore.Data;
using Sitecore.Configuration;
using Lucene.Net.Documents;
using Sitecore;
namespace ntegral.sitecore
{
/// <summary>
/// Summary description for sublayout
/// </summary>
public partial class SearchResults : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
// Decode the search string query string
string searchStr = Server.UrlDecode(WebUtil.GetQueryString("q"));
// Be sure we have an empty search string at very least (avoid null)
if (searchStr == null) searchStr = string.Empty;
// If the visitor provided no criteria, don't bother searching
if (searchStr == string.Empty)
lblSearchString.Text = "search criteria" + "search no criteria";
else
{
// Remind the visitor what they provided as search criteria
lblSearchString.Text = "search criteria" + searchStr;
SearchInternal(searchStr);
}
}
private void SearchInternal(string search)
{
string indexName = “custom”;
Database db = Factory.GetDatabase("web");
Index index = db.Indexes[indexName];
Sitecore.Diagnostics.Error.AssertNotNull(index,
"There is no " + indexName + " index on the current database (" +
db.Name + ")");
IndexSearcher searcher = index.GetSearcher(db);
try
{
Analyzer analyzer = new StandardAnalyzer();
QueryParser qp = new QueryParser(Index.ContentFieldName, analyzer);
qp.SetDefaultOperator(QueryParser.Operator.AND);
Query query = qp.Parse(search);
Hits hits = searcher.Search(query);
int docsCount = 0;
for (int i = 0; i < hits.Length(); i++)
{
Item item = Index.GetItem(hits.Doc(i), db);
if (item != null)
{
System.Web.UI.WebControls.Literal lable = new System.Web.UI.WebControls.Literal();
if (item.TemplateName == "Home") || (item.TemplateName == “Generic Page”)
{
lable.Text = string.Format("<b>{0}</b><br/><a href=\"{1}{2}\">Go To Page</a><br/>", item.Name,
item.Paths.IsMediaItem ? item.Fields["Path"].Value : LinkManager.GetItemUrl(item), "");
pnResultsPanel.Controls.Add(lable);
docsCount++;
}
}
}
SetResultsSummary(search, docsCount);
}
catch
{
string errorText = Translate.Text("Error executing search request") + ". ";
errorText += Translate.Text("Please input a valid search string") + ".";
LiteralControl errorControl =
new LiteralControl("<span style=\"color:Red; font-weight:bold;\">" + errorText + "</span>");
pnResultsPanel.Controls.Add(errorControl);
}
finally
{
// DO NOT forget to close searcher. Because it may lock index.
// Be careful not to call this method while you are still using objects like Hits.
searcher.Close();
}
}
private void SetResultsSummary(string request, int resultsCount)
{
literalResultCount.Text = String.Format(
Translate.Text(
"<span class='searchResultsText'>Your search <span class='searchResultsTextBold'>'{0}'</span> returned {1} {2}</span>"),
request, resultsCount,
(resultsCount == 1 ? Translate.Text("document") : Translate.Text("documents")));
}
}
}