Click here to Skip to main content
15,884,537 members
Articles / Web Development / ASP.NET

ASP.NET C# Search Engine (Highlighting, JSON, jQuery & Silverlight)

Rate me:
Please Sign up or sign in to vote.
4.60/5 (38 votes)
8 Mar 2009CPOL10 min read 376.2K   13.2K   184  
More professional ASP.NET C# search with proper document summary, query highlighting and RIA display options
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Searcharoo.Common;
using Mono.GetOptions;           //v4
using PFitzsimons.ConsoleColour; //v6

#region Mono.GetOptions attributes to drive command line argument parsing
// Instructions to 'set up' Mono.GetOptions
// http://www.talios.com/command_line_processing_with_monogetoptions.htm

// Attributes visible in " -V"
[assembly: Mono.About("Searcharoo Indexer (Spider)")]
[assembly: Mono.Author("Craig.Dunn (at) ConceptDevelopment.net")]
[assembly: Mono.AdditionalInfo("Searcharoo.Indexer.exe spiders and catalogs data for the Searcharoo.Engine")]

// This is text that goes after " [options]" in help output - there is none for this program
[assembly: Mono.UsageComplement("")]

// Attributes visible in " --help"
// are defined in AssemblyInfo.cs (not here)
#endregion

namespace Searcharoo.Indexer
{
    /// <summary>
    /// Searcharoo INDEXER console application
    /// </summary>
    /// <remarks>
    /// Colored-coded output courtesy of
    /// http://www.codeproject.com/csharp/Console_Apps__Colour_Text/ConsoleColour_src.zip
    /// (Philip Fitzsimons)
    /// </remarks>
    class Program
    {
        private static CommandLinePreferences clip;

        static void Main(string[] args)
        {
            clip = new CommandLinePreferences();

            clip.ProcessArgs(args);

            if (clip.Verbosity > 0)
            {
                ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Green, true);
                Console.Write("Searcharoo.Indexer");
                ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Red, true);
                Console.WriteLine(" v0.3");
                ConsoleColour.SetForeGroundColour();
            }

            ConsoleWriteLine(1, "=======================");
            Spider spider = new Spider();

            spider.SpiderProgressEvent += new SpiderProgressEventHandler(OnProgressEvent);
            spider.SpiderProgressEvent += new SpiderProgressEventHandler(OnProgressLogEvent);

            string[] startPages = Preferences.StartPage.Split(new char[] { ',',';'});
           
            Uri[] uris = new Uri[startPages.Length];
            for (int i = 0; i < startPages.Length; i++)
            {
                uris[i] = new Uri(startPages[i]);
            }
            Catalog catalog = null;
            if (uris.Length == 1)
            {   // legacy behaviour, just for testing/comparison
                catalog = spider.BuildCatalog(new Uri(Preferences.StartPage));
            }
            else
            {   // multiple start Uris allowed
                catalog = spider.BuildCatalog(uris);
            }
           
            ConsoleWriteLine(1, "=======================");
#if DEBUG
            //System.Threading.Thread.Sleep(30 * 1000);    // 30 seconds
            ConsoleWriteLine(1, "Press <enter> to finish...");
            if (clip.Verbosity > 0) Console.Read();
#endif            
        }

        private static void ConsoleWriteLine(int level, string text)
        {
            switch (level)
            {
                case 2:
                    ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Grey);
                    break;
                case 3:
                    ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Yellow);
                    break;
                case 4:
                    ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Red);
                    break;
                case 5:
                    ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Magenta);
                    break;
                default:
                    ConsoleColour.SetForeGroundColour();
                    break;
            }
            if (level <= clip.Verbosity)
            {
                Console.WriteLine(text);
            }
        }

        /// <summary>
        /// Handle events generated by the Spider (mostly reporting on success/fail of page load/index)
        /// </summary>
        public static void OnProgressEvent(object source, ProgressEventArgs pea)
        {
            if (pea.Level <= clip.Verbosity)
            {
                switch (pea.Level)
                {
                    case 2:
                        ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Grey);
                        break;
                    case 3:
                        ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Yellow);
                        break;
                    case 4:
                        ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Red);
                        break;
                    case 5:
                        ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Magenta);
                        break;
                    default:
                        ConsoleColour.SetForeGroundColour();
                        break;
                }
                Console.WriteLine(">{0} :: {1}", pea.Level, pea.Message);
            }
            
        }

        /// <summary>
        /// Log to disk events generated by the Spider (mostly reporting on success/fail of page load/index)
        /// </summary>
        public static void OnProgressLogEvent(object source, ProgressEventArgs pea)
        {
            //if (pea.Level < 3)
            //{
            //    Console.WriteLine(pea.Message + "<br>");
            //}
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
-- ooo ---
www.conceptdevelopment.net
conceptdev.blogspot.com
www.searcharoo.net
www.recipenow.net
www.racereplay.net
www.silverlightearth.com

Comments and Discussions