提到Lucene,想必大家都有所耳聞,已經是數年前就出現的開源技術。很多站點都是利用它搭建自己網站的站內搜索。由于最近也在做數據檢索方面的東西,也學習了下Lucene.net的使用。
導入Lucene.NET 開發包
Lucene 是apache軟件基金會一個開放源代碼的全文檢索引擎工具包,是一個全文檢索引擎的架構,提供了完整的查詢引擎和索引引擎,部分文本分析引擎。Lucene的目的是為軟件開發人員提供一個簡單易用的工具包,以方便的在目標系統中實現全文檢索的功能,或者是以此為基礎建立起完整的全文檢索引擎。Lucene.Net 是 .NET 版的Lucene。
你可以在這里下載到最新的Lucene.NET
創建索引、更新索引、刪除索引

搜索,根據索引查找

IndexHelper 添加、更新、刪除索引
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 using System; using Lucene.Net.Store; using Lucene.Net.Index; using Lucene.Net.Analysis.PanGu; using Lucene.Net.Documents; namespace BLL { class IndexHelper { /// <summary> /// 日志小助手 /// </summary> static Common.LogHelper logger = new Common.LogHelper(typeof(SearchBLL)); /// <summary> /// 索引保存的位置,保存在配置文件中從配置文件讀取 /// </summary> static string indexPath = Common.ConfigurationHelper.AppSettingMapPath("IndexPath"); /// <summary> /// 創建索引文件或更新索引文件 /// </summary> /// <param name="item">索引信息</param> public static void CreateIndex(Model.HelperModel.IndexFileHelper item) { try { //索引存儲庫 FSDirectory directory = FSDirectory.Open(new System.IO.DirectoryInfo(indexPath), new NativeFSLockFactory()); //判斷索引是否存在 bool isUpdate = IndexReader.IndexExists(directory); if (isUpdate) { //如果索引目錄被鎖定(比如索引過程中程序異常退出),則首先解鎖 if (IndexWriter.IsLocked(directory)) { //解鎖索引庫 IndexWriter.Unlock(directory); } } //創建IndexWriter對象,添加索引 IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIM新聞熱點
疑難解答