Knowledgebase Home Page  >  SearchUnit
Search the Knowledge Base
How can I show the % rank for a result with the SearchResult control? (C#)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=126&catId=54

Options

Print this page
Email this to a friend
To show a % rank next to a result, we exploit the ItemCreated event which will let us hook into the template creation and work with our own Label control (percentRank).
 
Assuming you already have 2 pages, one with SearchBox and the other with SearchResult - open the page holding SearchResult.
 
 
1. In the ResultItemTemplate in the SearchResult control, add a Label control, eg.
 
<ResultItemTemplate>
 <TABLE border="0">
   <TR>
    <TD class="SEResultItemLink"><A href="<%# Container.Uri %>"><%# Container.Title %></A></TD>
   </TR>
   <TR>
    <TD class="SEResultItemSummary"><%# Container.Summary%></TD>
   </TR>
   <TR>
    <TD class="SEResultItemURL"><%# Container.Uri %></TD>
   </TR>
   <tr>
   <td>
   <asp:Label ID="percentRank" Runat=server></asp:Label>
   </td>
   </tr>
  </TABLE>
 <BR />
</ResultItemTemplate>
 
 
2. In the code-behind for the SearchResult page, add this field;
 
int topWeight=-1;
 
3. again in the code-behind, add the following code to the Page_Load method;
 
if(ViewState["topWeight"]!=null)

topWeight = (int) ViewState["topWeight"];

this.SearchResult1.ItemCreated+=new Keyoti.SearchEngine.Web.SearchResult.SearchResultItemEventHandler(SearchResult1_ItemCreated);

4. and add this method;
 

private void SearchResult1_ItemCreated(object sender, Keyoti.SearchEngine.Web.SearchResultItemEventArgs e)

{

//If we are showing a ResultItem (and not a header, footer or error)

if(e.Item is Keyoti.SearchEngine.Web.ResultItem)

{

//Grab the result weight from the underlying data, note ResultItem is in a different namespace to the ResultItem above

int weight = ((Keyoti.SearchEngine.Search.ResultItem) e.Data).Weight;

if(topWeight==-1) //this is the first result, so store it's weight

{

topWeight = weight;

ViewState["topWeight"] = topWeight;

}

//find our new label and set it to the %

Label percLabel = (Label) e.Item.FindControl("percentRank");

percLabel.Text= ((10000*weight)/(100*topWeight))+"%";

}

}

 

If you have trouble or questions please email support@keyoti.com

Related Questions:

Attachments:

No attachments were found.