Show language: C# VB.NET Both

Filtering Results

Search results can be filtered by custom criteria (URL, custom data, title, filetype, etc) programmatically, via a simple event handler, or subclass.

Filtering With A Central Event Handler

By directly hooking into the Configuration.CentralEventDispatcher.Action event we can easily obtain the preliminary results of a search, which we can then filter, reorder etc.

The code below handles the "Action" event in the central event system. It processes "ResultItemsFinalized" notifications, and uses the event argument to access the preliminary results.

With The SearchResult Control

Eg. to filter any results that include the word "News" in the title, the following code is used in the code-behind of the form holding the SearchResult control.

C#
    protected void Page_Load(object sender, EventArgs e)
    {
		SearchResult1.FilterLoadLevel = Keyoti.SearchEngine.Search.FilterLoadLevel.Everything;
        SearchResult1.Configuration.CentralEventDispatcher.Action += new 
		Keyoti.SearchEngine.Events.ActionEventHandler(CentralEventDispatcher_Action);
    }

    void CentralEventDispatcher_Action(object sender, Keyoti.SearchEngine.Events.ActionEventArgs e)
    {
        if (e.ActionData.Name == Keyoti.SearchEngine.Events.ActionName.ResultItemsFinalized)
        {
            Keyoti.SearchEngine.Utils.ResultItemList results =
					e.ActionData.Data as Keyoti.SearchEngine.Utils.ResultItemList;
            for (int i = 0; i < results.Count; i++)
            {
                if (results[i].Title.Contains("News"))
                {
                    results.RemoveAt(i);
                    i--;//need to rerun over this element number.
                }
            }
        }
    }
VB
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        SearchResult1.FilterLoadLevel = Keyoti.SearchEngine.Search.FilterLoadLevel.Everything
        AddHandler SearchResult1.Configuration.CentralEventDispatcher.Action, _
							AddressOf Me.CentralEventDispatcher_Action

    End Sub
    
    Private Sub CentralEventDispatcher_Action(ByVal sender As Object, _
							ByVal e As Keyoti.SearchEngine.Events.ActionEventArgs)

        If (e.ActionData.Name = Keyoti.SearchEngine.Events.ActionName.ResultItemsFinalized) Then

            Dim results As Keyoti.SearchEngine.Utils.ResultItemList = _ 
				CType(e.ActionData.Data,Keyoti.SearchEngine.Utils.ResultItemList)
            Dim i As Integer = 0

            Do While (i < results.Count)
                If results(i).Title.Contains("News") Then
                    results.RemoveAt(i)
                    i = (i - 1)
                    'need to rerun over this element number.
                End If
                i = (i + 1)
            Loop

        End If

    End Sub

With The SearchAgent Class

Eg. to filter any results that include the word "News" in the title, the following code is used in the class that uses SearchAgent

C#
    {
        ...
        SearchAgent agent = new SearchAgent(....);

        agent.FilterLoadLevel = Keyoti.SearchEngine.Search.FilterLoadLevel.Everything;
        agent.Configuration.CentralEventDispatcher.Action += new 
        Keyoti.SearchEngine.Events.ActionEventHandler(CentralEventDispatcher_Action);
        ...
        agent.Search(....);
    }

    void CentralEventDispatcher_Action(object sender, Keyoti.SearchEngine.Events.ActionEventArgs e)
    {
        ...same as the example above for the SearchResult control...
    }
VB
    Sub ...

        ...
        Dim agent As New SearchAgent(....)
        agent.FilterLoadLevel = Keyoti.SearchEngine.Search.FilterLoadLevel.Everything
        AddHandler agent.Configuration.CentralEventDispatcher.Action, _
							AddressOf Me.CentralEventDispatcher_Action

    End Sub
    
    Private Sub CentralEventDispatcher_Action(ByVal sender As Object, _
							ByVal e As Keyoti.SearchEngine.Events.ActionEventArgs)

        ...same as the example above for the SearchResult control...
    End Sub

Notes