使用Lucene.NET實(shí)現(xiàn)簡單的站內(nèi)搜索
Lucene 是apache軟件基金會(huì)一個(gè)開放源代碼的全文檢索引擎工具包,是一個(gè)全文檢索引擎的架構(gòu),提供了完整的查詢引擎和索引引擎,部分文本分析引擎。Lucene的目的是為軟件開發(fā)人員提供一個(gè)簡單易用的工具包,以方便的在目標(biāo)系統(tǒng)中實(shí)現(xiàn)全文檢索的功能,或者是以此為基礎(chǔ)建立起完整的全文檢索引擎。Lucene.Net 是 .NET 版的Lucene。
你可以在這里下載到最新的Lucene.NET



1 using System; 2 using Lucene.Net.Store; 3 using Lucene.Net.Index; 4 using Lucene.Net.Analysis.PanGu; 5 using Lucene.Net.Documents; 6 7 namespace BLL 8 { 9 class IndexHelper 10 { 11 /// <summary> 12 /// 日志小助手 13 /// </summary> 14 static Common.LogHelper logger = new Common.LogHelper(typeof(SearchBLL)); 15 /// <summary> 16 /// 索引保存的位置,保存在配置文件中從配置文件讀取 17 /// </summary> 18 static string indexPath = Common.ConfigurationHelper.AppSettingMapPath("IndexPath"); 19 20 /// <summary> 21 /// 創(chuàng)建索引文件或更新索引文件 22 /// </summary> 23 /// <param name="item">索引信息</param> 24 public static void CreateIndex(Model.HelperModel.IndexFileHelper item) 25 { 26 try 27 { 28 //索引存儲(chǔ)庫 29 FSDirectory directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath), new NativeFSLockFactory()); 30 //判斷索引是否存在 31 bool isUpdate = IndexReader.IndexExists(directory); 32 if (isUpdate) 33 { 34 //如果索引目錄被鎖定(比如索引過程中程序異常退出),則首先解鎖 35 if (IndexWriter.IsLocked(directory)) 36 { 37 //解鎖索引庫 38 IndexWriter.Unlock(directory); 39 } 40 } 41 //創(chuàng)建IndexWriter對象,添加索引 42 IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED); 43 //獲取新聞 title部分 44 string title = item.FileTitle; 45 //獲取新聞主內(nèi)容 46 string body = item.FileContent; 47 //為避免重復(fù)索引,所以先刪除number=i的記錄,再重新添加 48 //尤其是更新的話,更是必須要先刪除之前的索引 49 writer.DeleteDocuments(new Term("id", item.FileName)); 50 //創(chuàng)建索引文件 Document 51 Document document = new Document(); 52 //只有對需要全文檢索的字段才ANALYZED 53 //添加id字段 54 document.Add(new Field("id", item.FileName, Field.Store.YES, Field.Index.NOT_ANALYZED)); 55 //添加title字段 56 document.Add(new Field("title", title, Field.Store.YES, Field.Index.NOT_ANALYZED)); 57 //添加body字段 58 document.Add(new Field("body", body, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS)); 59 //添加url字段 60 document.Add(new Field("url", item.FilePath, Field.Store.YES, Field.Index.NOT_ANALYZED)); 61 //寫入索引庫 62 writer.AddDocument(document); 63 //關(guān)閉資源 64 writer.Close(); 65 //不要忘了Close,否則索引結(jié)果搜不到 66 directory.Close(); 67 //記錄日志 68 logger.Debug(String.Format("索引{0}創(chuàng)建成功",item.FileName)); 69 } 70 catch (SystemException ex) 71 { 72 //記錄錯(cuò)誤日志 73 logger.Error(ex); 74 throw; 75 } 76 catch (Exception ex) 77 { 78 //記錄錯(cuò)誤日志 79 logger.Error(ex); 80 throw; 81 } 82 } 83 84 /// <summary> 85 /// 根據(jù)id刪除相應(yīng)索引 86 /// </summary> 87 /// <param name="guid">要?jiǎng)h除的索引id</param> 88 public static void DeleteIndex(string guid) 89 { 90 try 91 { 92 ////索引存儲(chǔ)庫 93 FSDirectory directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath), new NativeFSLockFactory()); 94 //判斷索引庫是否存在索引 95 bool isUpdate = IndexReader.IndexExists(directory); 96 if (isUpdate) 97 { 98 //如果索引目錄被鎖定(比如索引過程中程序異常退出),則首先解鎖 99 if (IndexWriter.IsLocked(directory))100 {101 IndexWriter.Unlock(directory);102 }103 }104 IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);105 //刪除索引文件106 writer.DeleteDocuments(new Term("id", guid));107 writer.Close();108 directory.Close();//不要忘了Close,否則索引結(jié)果搜不到109 logger.Debug(String.Format("刪除索引{0}成功", guid));110 }111 catch (Exception ex)112 {113 //記錄日志114 logger.Error(ex);115 //拋出異常116 throw;117 }118 }119 }120 }IndexHelper 添加、更新、刪除索引
1 using Lucene.Net.Analysis; 2 using Lucene.Net.Analysis.PanGu; 3 using Lucene.Net.Documents; 4 using Lucene.Net.Index; 5 using Lucene.Net.Search; 6 using Lucene.Net.Store; 7 using Model.HelperModel; 8 using System; 9 using System.Collections.Generic; 10 11 namespace BLL 12 { 13 public static class SearchBLL 14 { 15 //一個(gè)類中可能會(huì)有多處輸出到日志,多處需要記錄日志,常將logger做成static 靜態(tài)變量 16 /// <summary> 17 /// 日志助手 18 /// </summary> 19 static Common.LogHelper logger = new Common.LogHelper(typeof(SearchBLL)); 20 /// <summary> 21 /// 索引保存位置 22 /// </summary> 23 static string indexPath = Common.ConfigurationHelper.AppSettingMapPath("IndexPath"); 24 /// <summary> 25 /// 搜索 26 /// </summary> 27 /// <param name="keyWords">用戶搜索的關(guān)鍵詞</param> 28 /// <returns>返回搜索的結(jié)果</returns> 29 public static List<SearchResult> Search(string keywords) 30 { 31 try 32 { 33 //索引存儲(chǔ)庫 34 FSDirectory directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath), new NoLockFactory()); 35 //創(chuàng)建IndexReader對象 36 IndexReader reader = IndexReader.Open(directory, true); 37 //創(chuàng)建IndexSearcher對象 38 IndexSearcher searcher = new IndexSearcher(reader); 39 //新建PhraseQuery 查詢對象 40 PhraseQuery query = new PhraseQuery(); 41 //把用戶輸入的關(guān)鍵詞進(jìn)行拆詞 42 foreach (string word in SplitWord(keywords)) 43 { 44 //添加搜索關(guān)鍵詞 45 query.Add(new Term("body", word)); 46 } 47 //設(shè)置分詞間距為100字之內(nèi) 48 query.SetSlop(100); 49 TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true); 50
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注