1. Implementation of the engine (which involves database table operations, the stored procedure is used here ):
Using System; <br/> using System. collections. generic; <br/> using System. linq; <br/> using System. web; </p> <p> using System. data; <br/> using System. data. sqlClient; </p> <p> using System. text; <br/> using System. text. regularExpressions; <br/> using Internship. data; </p> <p> namespace Internship. webSite <br/>{</p> <p> public static class Searching <br/>{</p> <p> private static List <Entry> _ Catalog = new List <Entry> (); </P> <p> public static List <Entry> Catalog <br/>{< br/> get {return _ Catalog ;} <br/> set {_ Catalog = value ;}< br/>}</p> <p> public static void AddToCatalog (Entry entry Entry) <br/>{< br/> Catalog. add (entry); <br/>}< br/> public static void RemoveFromCatalog (Entry entry) <br/>{< br/> Catalog. remove (entry); <br/>}</p> <p> static Searching () <br/>{< br/> Catalog = BuildCatalog (); <br/>}</p> <p> Public static List <Entry> Hit (String searchItem) <br/>{< br/> List <Entry> entry = new List <Entry> (); </p> <p> List <Result> result = BuildResultSet (searchItem, false); <br/> foreach (var e in result) <br/> entry. add (e. IEntry); </p> <p> return entry; <br/>}</p> <p> private static List <Result> BuildResultSet (string searchTerm, bool includeComments) <br/>{< br/> List <Result> results = new List <Result> (); <Br/> string term = CleanContent (searchTerm. toLowerInvariant (). trim (), false); <br/> string [] terms = term. split (new char [] {''}, StringSplitOptions. removeEmptyEntries); <br/> string regex = string. format (System. globalization. cultureInfo. invariantCulture, "({0})", string. join ("|", terms); </p> <p> foreach (Entry entry in Catalog) <br/>{< br/> Result result = new Result (); <br/> result. rank = 0; <br/> result. IEntry = entry; </p> <p> int titleMatches = Regex. matches (entry. title, regex ). count; <br/> result. rank = titleMatches * 20; </p> <p> int postMatches = Regex. matches (entry. content, regex ). count; <br/> result. rank + = postMatches; </p> <p> if (result. rank> 0) <br/>{</p> <p> if (result. IEntry. content. length> 200) <br/>{< br/> result. IEntry. content = result. IEntry. content. substring (0,200 ); <Br/> result. IEntry. content + = ".... "; <br/>}< br/> result. IEntry. title = Replace (result. IEntry. title, terms); <br/> result. IEntry. content = Replace (result. IEntry. content, terms); </p> <p> results. add (result); <br/>}</p> <p> results. sort (); <br/> return results; <br/>}< br/> public static List <Entry> BuildCatalog () <br/>{< br/> List <Entry> entry = new List <Entry> (); <br/> using (SqlCon Nection cn = new SqlConnection (InternshipSettings. webSiteConnectionString) <br/>{< br/> SqlCommand cmd = new SqlCommand ("GetAllJobPostings", cn); <br/> cmd. commandType = CommandType. storedProcedure; </p> <p> cn. open (); <br/> using (SqlDataReader dr = cmd. executeReader () <br/>{< br/> while (dr. read () <br/>{< br/> Entry en = new Entry (); <br/> en. kinds = 1; // 1 for jobposting, 2 for company inst Roduction <br/> en. ID = dr ["JobPostingID"]. toString (); <br/> en. title = dr ["JobPostingTitle"]. toString (); <br/> en. content = dr ["JobPostingText"]. toString (); <br/> en. postDate = dr ["JobPostingTime"]. toString (); </p> <p> en. content = CleanContent (en. content, true); <br/> entry. add (en ); </p> <p >}</p> <p> using (SqlConnection cn = new SqlConnection (InternshipSettings. webSiteConnectionSt Ring) <br/>{</p> <p> SqlCommand cmd = new SqlCommand ("GetAllCompanyIntroductions ctions", cn); <br/> cmd. commandType = CommandType. storedProcedure; </p> <p> cn. open (); </p> <p> using (SqlDataReader dr = cmd. executeReader () <br/>{< br/> while (dr. read () <br/>{< br/> Entry en = new Entry (); <br/> en. kinds = 2; // 1 for jobposting, 2 for company instroduction <br/> en. ID = dr ["IntroductionID"]. toString (); <Br/> en. title = dr ["CompanyName"]. toString (); <br/> en. content = dr ["Introduction"]. toString () + dr ["Address"]. toString () + dr ["Phone"]. toString (); </p> <p> en. content = CleanContent (en. content, true); <br/> entry. add (en); <br/>}</p> <p> return entry; <br/>}</p> <p> private static readonly Regex STRIP_HTML = new Regex ("<[^>] *>", RegexOptions. compiled); <br/> /// <summary> <br/> /// Strips all HTML tags from the specified string. <br/> /// </summary> <br/> // <param name = "html"> The string containing HTML </param> <br/> // /<returns> A string without HTML tags </returns> <br/> public static string StripHtml (string html) <br/>{< br/> if (string. isNullOrEmpty (html) <br/> return string. empty; </p> <p> return STRIP_HTML.Replace (html, ""/* string. empty */); <br/>}< br/> public static String CleanContent (string content, bool removeHtml) <br/>{< br/> if (removeHtml) <br/> content = StripHtml (content ); </p> <p> content = content <br/>. replace ("\", string. empty) <br/>. replace ("|", string. empty) <br/>. replace ("(", string. empty) <br/>. replace (")", string. empty) <br/>. replace ("[", string. empty) <br/>. replace ("]", string. empty) <br/>. replace ("*", string. empty) <br/>. replace ("? ", String. empty) <br/>. replace ("}", string. empty) <br/>. replace ("{", string. empty) <br/>. replace ("^", string. empty) <br/>. replace ("+", string. empty) <br/>. replace ("", string. empty); </p> <p> string [] words = content. split (new char [] {'', '\ n',' \ R'}, StringSplitOptions. removeEmptyEntries); <br/> StringBuilder sb = new StringBuilder (); <br/> for (int I = 0; I <words. length; I ++) <br/>{< br/> String word = words [I]. ToLowerInvariant (). Trim (); <br/> if (word. Length> 1 /*&&! _ StopWords. contains (word) */) <br/> sb. append (word + ""); <br/> // sb. append (word); <br/>}</p> <p> return sb. toString (); <br/>}</p> <p> private static string Replace (string src, string [] terms) <br/>{</p> <p> foreach (var term in terms) <br/>{< br/> string replace = "<font color = \" # FF0066 \ ">" + term + "</font> "; <br/> src = src. replace (term, replace); <br/>}< br/> return src; <br/>}< br/>
2. How to use it?
<Br/> if (! String. isNullOrEmpty (TB. text) <br/>{< br/> CurPage = 1; </p> <p> entry = Searching. hit (TB. text); </p> <p> if (entry. count % PAGE_SIZE = 0) <br/> TolPage = entry. count/PAGE_SIZE; <br/> else <br/> TolPage = entry. count/PAGE_SIZE + 1; </p> <p> DataBind (CurPage, TolPage); <br/>}