Monday, August 28, 2006
by Nik Kalyani
Monday, August 28, 2006 5:36:32 AM (Pacific Standard Time, UTC-08:00)

Testing an implementation of the DNN ISearchable interface implementation for a module can be time-consuming and slow if you rely on the DNN search engine indexer to run and then either check the database for results or use the Search UI. There is a simpler way.

Copy and paste the below script into DNNSearch.aspx (or grab the attachment at the end of this post), place the file in the root folder of your app and you can test ISearchable for any module instance with ease. The script requires you to provide a TabId and a ModuleId. It then queries the database for the BusinessController defined in the DesktopModules table and instantiates it exactly as the DNN search indexer does. It then calls GetSearchItems() and displays the results.

Unlike the DNN search indexer, DNNSearch does not make any changes to the database. It is useful only for testing if the ISearchable implementation is working correctly and does not provide any insights into any issues that the search index provider you are using may have.

 

DNNSearch.aspx

<%@ Import namespace="DotNetNuke.Entities.Modules" %>
<%@ Import namespace="DotNetNuke.Services.Search" %>
<%@ Import namespace="DotNetNuke.Common" %>
<%@ Page Language="c#" AutoEventWireup="false" %>
<script runat="server">
 
    void Results_Click(object sender, EventArgs e)
    {
        int moduleId = -1;
        try
        {
            moduleId = Convert.ToInt32(ModuleId.Text);
        }
        catch
        {
        }
 
        int tabId = -1;
        try
        {
            tabId = Convert.ToInt32(TabId.Text);
        }
        catch
        {
        }
 
        if ((moduleId > -1) && (tabId > -1))
            GetSearchResults(moduleId, tabId);
        else
            SearchResults.Text = "Both Module ID and Tab ID are required";
    }
 
    void GetSearchResults(int moduleId, int tabId)
    {
        ModuleController moduleController = new ModuleController();
        ModuleInfo moduleInfo = moduleController.GetModule(moduleId, tabId);
        StringBuilder sb = new StringBuilder();
 
        if (moduleInfo == null) 
        {
            SearchResults.Text = "No module found with ModuleID=" + moduleId.ToString() + " and TabID=" + tabId.ToString();
            return;
        }
 
        if (moduleInfo.BusinessControllerClass == "")
            SearchResults.Text = "The BusinessControllerClass in the database is blank.";
        else
        {
            try
            {
                object bizController = DotNetNuke.Framework.Reflection.CreateObject(moduleInfo.BusinessControllerClass, moduleInfo.BusinessControllerClass);
                if (bizController == null)
                    SearchResults.Text = "The Business Controller Class <b>" + moduleInfo.BusinessControllerClass + "</b> could not be instantiated.";
                else
                {    
                    SearchContentModuleInfo contentInfo = new SearchContentModuleInfo();
                    contentInfo.ModControllerType = (ISearchable) bizController;
                    contentInfo.ModInfo = moduleInfo;
                    SearchItemInfoCollection results = contentInfo.ModControllerType.GetSearchItems(contentInfo.ModInfo);
                    if (results != null)
                    {
                        int counter = 0;
                        foreach(SearchItemInfo searchItem in results)
                        {
                    if (moduleInfo.ModuleID == searchItem.ModuleId)
                    {
                        sb.Append("<p>Title: " + searchItem.Title);
                        sb.Append("<br>GUID: " + searchItem.GUID);
                        sb.Append("<br>Date: " + searchItem.PubDate.ToLongDateString());
                        sb.Append("<br>Description: " + searchItem.Description + "</p>");
                    }
                    counter++;
                        }
                        SearchResults.Text = counter.ToString() + " results found.<br>" + sb.ToString();
                    }
                    else
                        SearchResults.Text = "No search results.";
                }
            }
            catch(Exception e)
            {
                SearchResults.Text = "Error: " + e.Message + "<br><br>" + e.StackTrace;
            }
        }
    }
 
    override protected void OnInit(EventArgs e)
    {
        Results.Click += new EventHandler(Results_Click);
        base.OnInit(e);
    }
 
</script>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>Speerio ISearchable Test</title>
        <style>
              body, p {font-family: Verdana; font-size: 9pt}
        </style>
    </HEAD>
    <body>
        <p><font size="4">DNNSearch Script - by <a href="http://www.speerio.net">Speerio, Inc.</a></p>
        <p><font color="red" size="4">WARNING: Do not leave this script installed on a production system.</font></p>
        <form id="Form1" method="post" runat="server">
            <p>Tab ID: <asp:TextBox ID="TabId" Runat="server"></asp:TextBox></p>
            <p>Module ID: <asp:TextBox ID="ModuleId" Runat="server"></asp:TextBox></p>
            <asp:Button ID="Results" Runat="server" Text="Get Search Results" /></p>
            <p>To test for user-specific results, add code to GetSearchItems() to check for userid=N in querystring.</p>
            <p><b>Search Results:</b></p>
            <asp:Label ID="SearchResults" Runat="server" />
        </form>
    </body>
</HTML>

DNNSearch.zip (1.48 KB)

#    Comments [2] - Trackback    

Monday, September 04, 2006 2:04:28 AM (Pacific Standard Time, UTC-08:00)
This is very useful for discovering whether or not a module is being included in search (i.e. has been entered in SearchItem table). It doesn't however show the set of words that have been added to the search index, which would be a useful reassurance that the module really is being indexed as expected. Can you show us how to add this to your sample?
Laurence
Monday, September 04, 2006 5:40:41 AM (Pacific Standard Time, UTC-08:00)
Hi Laurence,

The purpose of this script is to verify the correct implementation of the ISearchable implementation. The actal indexing is handled by a provider which will vary from site to site. Since the script would need to be modified to identify the provider and then call the appropriate method, it's a non-trivial addition to the above.

It's a good idea and I'll look into enhancing the script with this capability.

Nik
Nik Kalyani
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, i, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
RSS feed
Search and Links
Bling

View Nik Kalyani's profile on LinkedIn

Contact me: nik*kalyani.com (replace "*")

TechBubble
www.flickr.com
This is a Flickr badge showing public photos from techbubble. Make your own badge here.
Statistics
Total Posts: 204
This Year: 22
This Month: 0
This Week: 0
Comments: 231
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Nik Kalyani
Sign In
All Content © 2008, Nik Kalyani