Hello MJia,
SearchResult and Search box are not develop to work with multiple separate indexes and this is the idea of combined index. However there are two options.
1. Modify SearchBox control and pass different value in the url bar as a QueryString so that the SearchResult will display different results.
Sample code:
Modify SearchBox template
<%@ Control Language="C#" %>
<%@ Register Assembly="Telerik.Cms.Web.UI" Namespace="Telerik.Cms.Web.UI" TagPrefix="sfWeb" %>
<sfWeb:CssFileLink ID="CssFileLink1" FileName="~/Sitefinity/ControlTemplates/Search/searchCommonLayout.css" Media="screen" runat="server" />
<script type="text/C#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
searchButton1.Click += new EventHandler(searchButton1_Click);
}
void searchButton1_Click(object sender, EventArgs e)
{
if (CheckBox1.Checked)
{
CheckBox2.Checked = false;
Response.Redirect(Request.RawUrl + "?IndexCatalogue=" + CheckBox1.Text + "&SearchQuery=" + queryText.Text);
}
if (CheckBox2.Checked)
{
CheckBox1.Checked = false;
Response.Redirect(Request.RawUrl + "?IndexCatalogue=" + CheckBox2.Text + "&SearchQuery=" + queryText.Text);
}
}
</script>
<fieldset class="sf_searchBox">
<asp:Label runat="server" AssociatedControlID="queryText" Text="Search" />
<asp:TextBox ID="queryText" runat="server" CssClass="sf_searchText"></asp:TextBox>
<input style="display:none;" type="button" id="searchButton" runat="server" class="sf_searchSubmit" value="Search" alt="Search"/>
<asp:Button ID="searchButton1" runat="server" Text="CustomSearchButton" />
</fieldset>
<asp:CheckBox runat="server" ID="CheckBox1" Text="SearchInNews" AutoPostBack="true" />
<asp:CheckBox runat="server" ID="CheckBox2" Text="SearchInBlogs" AutoPostBack="true" />
2. Option two is creating a custom search result control which by default will load all results, but you will decide which of them to hide.
Sample code - you have to inherit from SearchResults class and implement custom logic inside CreateChildControls method.
Here you will set the new data source of SearchResult control depending on the items that you want to see displayed.
protected override void CreateChildControls()
{
this.layoutCnt.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
this.layoutCnt.ResultsList.DataBind();
this.Controls.Clear();
this.layoutCnt = new Container(this);
this.LayoutTemplate.InstantiateIn(layoutCnt);
if (!string.IsNullOrEmpty(Query))
{
string vPath = PathUtil.GetIndexPhysicalPath(this.IndexCatalogue);
string nPath = PathUtil.GetIndexPhysicalPath(NewCatalogName);
if (Directory.Exists(vPath) && Directory.Exists(nPath))
{
// check if query string is supplied to select the current page
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[PageKey]))
{
this.CurrentPage = Convert.ToInt32(
HttpContext.Current.Request.QueryString[PageKey]);
}
string searchQuery = Query;
string message = string.Empty;
bool isValid = this.EscapeSpecialChars ? this.EscapeSpecialChars : ValidateQuery(ref searchQuery, out message);
if (isValid)
{
int totalItems;
int totalItems2;
int startIndex = (this.CurrentPage - 1) * this.PostsPerPage;
try
{
IList<ResultItem> dataSource = SearchManager.Search(
searchQuery,
this.IndexCatalogue,
startIndex,
this.PostsPerPage,
this.WordsMode,
this.EscapeSpecialChars,
out totalItems);
IList<ResultItem> newDS = SearchManager.Search(
searchQuery,
NewCatalogName,
startIndex,
this.PostsPerPage,
this.WordsMode,
this.EscapeSpecialChars,
out totalItems2);
IList<ResultItem> newList = new List<ResultItem>();
foreach (ResultItem item in dataSource)
{
newList.Add(item);
}
foreach (ResultItem newDSitem in newDS)
{
newList.Add(newDSitem);
}
totalItems = totalItems + totalItems2;
int numberOfPages = (this.PostsPerPage == 0) ? 1 : (int)Math.Ceiling((double)totalItems / (double)this.PostsPerPage);
if (numberOfPages == 0 && totalItems > 0)
numberOfPages = 1;
string qeryTest = this.Query.Trim('\"');
((Control)this.layoutCnt.ResultsStats).EnableViewState = false;
this.layoutCnt.ResultsStats.Text = string.Format(this.layoutCnt.ResultsStats.Text, totalItems, qeryTest);
if (this.AllowPaging)
{
this.layoutCnt.Pager1.SelectedPageChanged += new EventHandler<EventArgs>(Pager_SelectedPageChanged);
this.layoutCnt.Pager1.PageCount = numberOfPages;
this.layoutCnt.Pager1.SelectedPage = this.CurrentPage;
}
this.layoutCnt.ResultsList.DataSource = newList;
}
catch (Telerik.Lucene.Net.QueryParsers.ParseException ex)
{
this.layoutCnt.ResultsStats.Text = "strange chars";
Log.Exception(ex);
}
}
else
{
this.layoutCnt.ResultsStats.Text = message;
}
}
else
{
this.layoutCnt.ResultsStats.Text = String.Format("InvalidIndex", this.IndexCatalogue);
}
}
else
{
this.layoutCnt.ResultsStats.Text = String.Empty;
}
this.layoutCnt.ResultsList.ItemDataBound += new RepeaterItemEventHandler(ResultsList_ItemDataBound);
this.layoutCnt.ResultsList.SkinID = this.SkinID;
Controls.Add(this.layoutCnt);
this.layoutCnt.ResultsList.DataBind();
}
Greetings,
Ivan Dimitrov
the Telerik team