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

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(string key, Configuration configuration) : base(key, configuration) { }

    public MySearchAgent(Configuration configuration) : base(configuration){ }

     ///

     /// Applying filters to results

     ///

     /// object to be added to results

     /// the search result's collections

     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(SearchResult1.Configuration);

}

 
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.