Knowledgebase Home Page  >  SearchUnit  >  Version 2 Articles
Search the Knowledge Base
How can I programmatically filter results according to my own logic? (C#)
https://keyoti.com/kb/Default.aspx?ToDo=view&questId=77&catId=66

Options

Print this page
Email this to a friend
It is possible to change the behavior of the SearchResult control so that it returns results according to your own filter, this is done by simply subclassing the SearchAgent class (the core search class which SearchResult uses to search the index).
 
The example below shows how to customize SearchAgent and use it in SearchResult.  The subclass simply filters any pages with 'default.aspx' in their URL, however it can easily be adapted to filter anything.
 
 
Steps to add customized SearchAgent
 
 
1. Open code-behind for page holding the SearchResult control
 
2. Add a reference to the Keyoti.SearchEngine.Core assembly if one doesn't already exist
 
3. Create a new class, eg MySearchAgent
 
This example applies an arbitrary filter to the results

public class MySearchAgent : Keyoti.SearchEngine.Search.SearchAgent

{

public MySearchAgent():base(){}

public MySearchAgent(string key):base(key){}

/// <summary>

/// Applying filters to results

/// </summary>

/// <param name="result">object to be added to results</param>

/// <param name="results">the search result's collections</param>

protected override void AddResultItemToResults(Keyoti.SearchEngine.Search.ResultItem result, ArrayList results)

{

if( result.URIString.IndexOf("default.aspx") == -1 ) //if we have default in the URI, dont add to results

results.Add(result);

}

}

 

4. In the Page_Load method use the MySearchAgent class

private void Page_Load(object sender, System.EventArgs e)

{

    this.SearchResult1.SearchAgent = new MySearchAgent();

}

 
 
In this example we filter out the "default.aspx" page so that it's not in the results.  This also demonstrates how customization can be performed through subclassing.  If you have any questions about this please email support@keyoti.com

Related Questions:

Attachments:

No attachments were found.