Creating an ASP.NET Search Engine

UserSearch.vb

It contains the following properties:

SearchCriteria The user choice of search is stored and retrieved from here
SearchWords The search words used by the user is stored and retrieved from here
TotalFilesSearched Total Files Searched is read from here
TotalFilesFound Total Files Searched found is read from here
        '**********************************************************
        '
        ' SearchCriteria Property
        '
        ' Assign and retrieve SearchCriteria of the site
        '
        '**********************************************************
        Public Property SearchCriteria() As Searchs.SearchCriteria
            Get
                Return m_searchCriteria
            End Get
            Set(ByVal Value As Searchs.SearchCriteria)
                m_searchCriteria = Value
            End Set
        End Property

        '**********************************************************
        '
        ' SearchWords Property
        '
        'Assign and retrieve SearchWords of the site
        '
        '**********************************************************
        Public Property SearchWords() As String
            Get
                Return m_searchWords
            End Get
            Set(ByVal Value As String)
                m_searchWords = Value
            End Set
        End Property

        '**********************************************************
        '
        ' TotalFilesSearched Property
        '
        ' Retrieve TotalFilesSearched of the site
        '
        '**********************************************************
        Public ReadOnly Property TotalFilesSearched() As Integer
            Get
                Return m_totalFilesSearched
            End Get
        End Property

        '**********************************************************
        '
        ' TotalFilesFound Property
        '
        ' Retrieve TotalFilesFound of the site
        '
        '**********************************************************
        Public ReadOnly Property TotalFilesFound() As Integer
            Get
                Return m_totalFilesFound
            End Get
        End Property

        '**********************************************************
        '
        ' PageDataset Shared Property
        '
        ' Retrieve data of the entire site of the site
        '
        '**********************************************************
        Public ReadOnly Property PageDataset() As DataSet
            Get
                Return m_dstPages
            End Get
        End Property

Search Method

Actual processing of the search begins here. DataSet to store the search results is created here and ProcessDirectory method is called.

        '********************************************
        '
        ' Search Method
        '
        ' Search the entire site
        '
        '********************************************
        Public Function Search(ByVal targetDirectory As String) As DataSet
            'If the site is in English then use the server HTML encode method
            If Searchs.Site.EnglishLanguage = True Then
                'Replace any HTML tags with the HTML codes 
                'for the same characters (stops people entering HTML tags)
                m_searchWords = m_page.Server.HtmlEncode(m_searchWords)
                'If the site is not english just change the script tags
            Else
                'Just replace the script tag <> with HTML encoded < and >
                m_searchWords = Replace(m_searchWords, "<", "<", 1, -1, 1)
                m_searchWords = Replace(m_searchWords, ">", ">", 1, -1, 1)
            End If
            If m_dstPages Is Nothing Then
                m_dstPages = Searchs.PagesDataset.Create()
            End If
            ProcessDirectory(targetDirectory)
            Return m_dstPages
        End Function

ProcessDirectory Method

The ProcessDirectory loops through all the files and calls the ProcessFile method. Later, it also loops through the subdirectories and calls itself.

       '*********************************************
        '
        ' ProcessDirectory Method
        '
        ' Files in the directories are searched
        '
        '********************************************
        Private Sub ProcessDirectory(ByVal targetDirectory As String)
            Dim fileEntries As String()
            Dim subdirectoryEntries As String()
            Dim filePath As String
            Dim subdirectory As String

            fileEntries = Directory.GetFiles(targetDirectory)

            ' Process the list of files found in the directory

            For Each filePath In fileEntries
                m_totalFilesSearched += 1
                ProcessFile(filePath)
            Next filePath

            subdirectoryEntries = Directory.GetDirectories(targetDirectory)
            ' Recurse into subdirectories of this directory

            For Each subdirectory In subdirectoryEntries

                'Check to make sure the folder about to be searched 
                'is not a barred folder if it is then don't search
                If Not InStr(1, Searchs.Site.BarredFolders, _ 
                  Path.GetFileName(subdirectory), vbTextCompare) > 0 Then
                    'Call the search sub prcedure to search the web site
                    ProcessDirectory(subdirectory)
                End If

            Next subdirectory

        End Sub 'ProcessDirectory

ProcessFile Method

The ProcessFile calls the GetInfo which returns the Searchs.Page object which contains all the information of the particular file. Later, it checks if the matchcount is greater than 0 and calls the CheckFileInfo to clean up the information stored in the Page object. It then stores the file in the PagesDataset.

        '*******************************************************
        '
        ' ProcessFile Method
        '
        ' Real logic for processing found files would go here.
        '
        '*******************************************************
        Private Sub ProcessFile(ByVal FPath As String)
            Dim srchFile As Searchs.Page

            srchFile = GetInfo(FPath)
            If Not IsNothing(srchFile) Then

                srchFile.Search(m_searchWords, m_searchCriteria)
                If srchFile.MatchCount > 0 Then
                    m_totalFilesFound += 1
                    'Response.Write(srchFile.Contents)
                    srchFile.CheckFileInfo()
                    Searchs.PagesDataset.StoreFile(m_dstPages, srchFile)
                End If

            End If

        End Sub 'ProcessFile

GetInfo Method

The GetInfo method's main task is to get the data of the file. It calls the shared method Searchs.FileContent.GetFileInfo where much of the work is done.

        '*****************************************************************
        '
        ' GetInfo Method
        '
        ' File data is picked in this method
        '
        '*****************************************************************
        Private Function GetInfo(ByVal FPath As String) As Searchs.Page

            Dim fileInform As New FileInfo(FPath)
            Dim sr As StreamReader
            Dim srchFile As New Searchs.Page()
            Dim strBldFile As New StringBuilder()
            Dim strFileURL As String  'Holds the path to the file on the site


            'Check the file extension to make sure the file 
            'is of the extension type to be searched

            If InStr(1, Searchs.Site.FilesTypesToSearch, _ 
             fileInform.Extension, vbTextCompare) > 0 Then
                'm_page.Trace.Warn("File ext.", fileInform.Extension)
                'Check to make sure the file about to be searched 
                'is not a barred file if it is don't search the file
                If Not InStr(1, Searchs.Site.BarredFiles, _ 
                  Path.GetFileName(FPath), vbTextCompare) > 0 Then
                    'm_page.Trace.Warn("File", FPath)

                    If Not File.Exists(FPath) Then
                        'm_page.Trace.Warn("Error", _
                        'String.Format("{0} does not exist.", FPath))
                        'Add throw excetion here
                        '
                        '
                        Return Nothing
                    End If

                    Searchs.FileContent.GetFileInfo(FPath, srchFile)

                    Return srchFile

                End If
            End If
            Return Nothing
        End Function

You might also like...

Comments

About the author

Stevan Rodrigues United States

I am a Microsoft Certified Solutions Developer in .Net Architecture (MCSD.Net Early Achiever – one among the first 2500 worldwide), Microsoft Certified Application Developer in .Net – MCAD.Net (...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”