Knowledgebase Home Page  >  SearchUnit
Search the Knowledge Base
How to add a 'search within results' feature.
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=254&catId=54

Options

Print this page
Email this to a friend

Searching within the current results set is fairly straightforward, it’s just the original query AND the new query.  Therefore we can give the user a ‘search within results’ checkbox quite easily and modify the search query as needed (ANDing the old query and the new one).

 

1.       In the HeaderTemplate of the SearchResult control, add the following controls

 

         <SearchEngine:SearchBox ID='sb1' runat="server" ResultPageURL="." />

         <asp:HiddenField ID="oldSearchQuery" runat="server" Value="<%# Container.QueryExpression %>" />

         <asp:CheckBox Text="Search Within" runat=server id='cb1' />

 

(the Ids are referred to in the code below)

 

2.       In the Page_Load, add some code to check for the checkbox being checked, and to amend the query if needed

 

protected void Page_Load(object sender, EventArgs e)

    {

        if (SearchResult1.Controls.Count > 0)

        {

            CheckBox searchWithin = SearchResult1.Controls[0].FindControl("cb1") as CheckBox;

            HiddenField oldQuery = SearchResult1.Controls[0].FindControl("oldSearchQuery") as HiddenField;

            Keyoti.SearchEngine.Web.SearchBox searchBox = SearchResult1.Controls[0].FindControl("sb1") as Keyoti.SearchEngine.Web.SearchBox;

            if (searchWithin != null && searchWithin.Checked)

            {

                Response.Redirect(Request.Url.AbsolutePath + "?QueryExpr=(" + oldQuery.Value + ") AND (" + searchBox.QueryTextBox.Value + ")");

            }

        }

    }

 

Note that the above code contains the string “QueryExpr” which is the default request parameter name used by our controls – this can actually be changed in the web.config to a different name, if you have done this you will need to change “QueryExpr” in the code above accordingly.

 


Related Questions:

Attachments:

No attachments were found.