B2C商城關鍵技術點總結(站內搜索、定時任務)

1.站內搜索javascript

1.1Lucene.Net創建信息索引  html

 1             string indexPath = @"E:\xxx\xxx";//索引保存路徑
 2             FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
 3             bool isUpdate = IndexReader.IndexExists(directory);
 4             if (isUpdate)
 5             {
 6                 //若是索引目錄被鎖定(好比索引過程當中程序異常退出),則首先解鎖
 7                 if (IndexWriter.IsLocked(directory))
 8                 {
 9                     IndexWriter.Unlock(directory);
10                 }
11             }
12             IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
13 
14             for (int i = 4939; i <= 6087; i++)
15             {
16                 //由於從服務器下載頁面有可能失敗,爲了不失敗時程序終止,因此要處理異常,寫入日誌
17                 //這裏能預知的異常是服務器下載失敗異常,WebException
18                 try
19                 {
20                     WebClient wc = new WebClient();
21                     wc.Encoding = Encoding.UTF8;
22                     string url = "http://localhost:3448/Book.aspx?id=" + i;
23                     string html = wc.DownloadString(url);
24 
25                     HTMLDocumentClass htmlDoc = new HTMLDocumentClass();
26                     htmlDoc.designMode = "on"; //不讓解析引擎去嘗試運行javascript
27                     htmlDoc.IHTMLDocument2_write(html);
28                     htmlDoc.close();
29 
30                     string title = htmlDoc.title;
31                     string content = "";
32                     if (htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DetailsView1_txtContent") != null)
33                     {
34                         if (htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DetailsView1_txtContent").innerText != null)
35                         {
36                             content = htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DetailsView1_txtContent").innerText;
37                         }
38                     }
39                     //爲避免重複索引,因此要先刪除"url"=url的記錄,再從新添加
40                     writer.DeleteDocuments(new Term("url", url));
41 
42                     Document document = new Document();
43                     document.Add(new Field("url", url, Field.Store.YES, Field.Index.NOT_ANALYZED));
44                     document.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
45                     document.Add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
46                     writer.AddDocument(document);
47 
48                     logger.Debug("索引" + i + "完畢");
49                 }
50                 catch (WebException webe)
51                 {
52                     logger.Error(webe.Message);
53                 }
54             }
55             writer.Close();
56             directory.Close();//不要忘了Close,不然索引結果搜不到
57             logger.Debug("所有索引完畢");            

1.2盤古分詞並高亮java

 1         public List<SearchContentResult> GetSearchContentResult(string kw, int startIndex,int pageSize,  out int count)
 2         {
 3             string indexPath = @"E:xxx\xxx\index";
 4             FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
 5             IndexReader reader = IndexReader.Open(directory, true);
 6             IndexSearcher searcher = new IndexSearcher(reader);
 7 
 8             //將用戶搜索的關鍵字進行分詞
 9             string[] strs = CommonHelper.FenCi(kw.ToLower());
10             PhraseQuery query = new PhraseQuery();
11             foreach (string str in strs)
12             {
13                 query.Add(new Term("content", str));
14             }
15             query.SetSlop(100);
16 
17             TopScoreDocCollector collector = TopScoreDocCollector.create(2000, true);
18             searcher.Search(query, null, collector);
19             count = collector.GetTotalHits();
20             ScoreDoc[] docs = collector.TopDocs(startIndex,pageSize).scoreDocs;
21             List<SearchContentResult> scs = new List<SearchContentResult>();
22             for (int i = 0; i < docs.Length; i++)
23             {
24                 int docId = docs[i].doc;
25                 Document doc = searcher.Doc(docId);
26                 SearchContentResult sc = new SearchContentResult();
27                 sc.Url = doc.Get("url");
28                 sc.Title = doc.Get("title");
29                 sc.Body = highLight(kw, doc.Get("content"));
30                 scs.Add(sc);
31             }
32             return scs;
33         }
34 
35         private static String highLight(string keyword, String content)
36         {
37             PanGu.HighLight.SimpleHTMLFormatter formatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color='red'>", "</font>");
38             PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(formatter, new Segment());
39             highlighter.FragmentSize = 500;
40             string msg = highlighter.GetBestFragment(keyword, content);
41             if (string.IsNullOrEmpty(msg))
42             {
43                 return content;
44             }
45             else
46             {
47                 return msg;
48             }
49         }

2.Quartz.Net定時任務web

在Global類中聲明一個靜態變量服務器

static IScheduler sched;

保證其在系統中是惟一的url

 1             //創建一個Quartz任務          
 2             ISchedulerFactory sf = new StdSchedulerFactory();
 3             sched = sf.GetScheduler();
 4             JobDetail job = new JobDetail("job1", "group1", typeof(IndexJob));//IndexJob爲實現了IJob接口的類
 5 
 6             Trigger trigger = TriggerUtils.MakeDailyTrigger("trigger", 10, 46);
 7             trigger.JobGroup = "group1";
 8             trigger.JobName = "job1";
 9             
10             sched.AddJob(job, true);
11             sched.ScheduleJob(trigger);
12             sched.Start();

添加任務類,並繼承接口spa

1     public class IndexJob : IJob
2     {
3         private static ILog logger = LogManager.GetLogger(typeof(IndexJob));
4         public void Execute(JobExecutionContext context)
5         {
6             //此處寫執行的代碼
7         }
8     }
相關文章
相關標籤/搜索