忙了幾天終于實現一個簡單的全文搜索在此回顧總結一下
本文介紹一下lucene.net 是什么?lucene.net 能作什么?以及怎么做的問題?最后給出 lucene.net 實現全文搜索的一個示例
1、lucene.net 是什么?
lucene.net 起初是一個開源項目然后轉向商業化,也在lucene.net 2.0已經發布,不過是要money d ,lucene.net的命運有點類似于freetextbox ,它在 1.6.5 版本之后發布的 2.0 開始了商業路線,2.0 提供了 dll 方式的免費版本,源代碼版本則必須購買商業的許可 licence;不過它留下了 1.6.5 版本的源代碼,還是可以看到大部分的內部細節,但 2.0 版本中添加的對 mozilla 瀏覽器的支持部分只有通過它生成的 html 和 javascript 腳本去窺測。
lucene 是 java 世界中常用的索引 api,使用它提供的方法可以為文本資料創建索引,并提供檢索。(參考:nlucene 和 lucene .net)nlucene 是第一個的 .net 移植,也是一個有 .net 風格的版本,使用 .net 的命名規范和類庫設計。不過 nlucene 項目的 leader 由于精力原因,只發布了 1.2beta 版本。lucene.net 項目出現后,nlucene 就沒有新的計劃了。
lucene.net 當初號稱要做 up-to-date 的 .net lucene 移植,它只在命名方面采納了 .net 的建議,主要目標傾向于和 java lucene 兼容:一個是索引格式兼容,達到可以共同工作的目的;一個是命名接近(只相差很少,比如大小寫等),目的是可以方便開發者使用 java lucene 相關的代碼和資料。
不知什么時候 lucene.net 項目已經放棄了開源計劃,轉向了商業。它居然把 sourceforge 上已經開源的文件也刪除了。與此同時,sourceforge 上又出現了 dotlucene 項目,出于對 lucene.net 的抗議,dotlucene 幾乎將 lucene.net 的代碼原封不動放在上面作為他們的起點。(https://sourceforge.net/forum/forum.php?thread_id=1153933&forum_id=408004)。
說白了lucene.net就是是一個信息檢索的函數庫(library),利用它你可以為你的應用加上索引和搜索的功能.
lucene的使用者不必深入了解有關全文檢索的知識,僅僅學會使用庫中的幾個類,知道怎么調用library中的函數,就可以為你的應用實現全文檢索的功能.
不過千萬別期望lucene是一個象google和百度那樣的搜索引擎,它僅僅是一個工具,一個library.你也可以把它理解為一個將索引,搜索功能封裝的很好的一套簡單易用的api.利用這套api你可以做很多有關搜索的事情,而且很方便,它可以滿足你對一個應用做簡單的全文搜索,作為應用的開發者(非專業搜索引擎開發者)來說,它的功能足以滿足你。
2、lucene.net 可以作什么?
lucene可以對任何的數據做索引和搜索. lucene不管數據源是什么格式,只要它能被轉化為文字的形式,就可以被lucene所分析利用.也就是說不管是ms word, html ,pdf還是其他什么形式的文件只要你可以從中抽取出文字形式的內容就可以被lucene所用.你就可以用lucene對它們進行索引以及搜索.
3、使用 lucene.net 怎么做?
簡單的歸結為:創建索引,和使用索引,其中創建索引就是將要搜索的數據源的那些信息作為我們的關鍵信息來存儲或者是分析,為搜索留下標記就象word里面創建目錄(個人理解),使用索引就是在搜索的時候根據索引的信息來分析數據源將我們需要的信息提取出來。
具體請看一下示例:
創建索引的類
public class intranetindexer
{
/**/////索引寫入器
private indexwriter writer;
//要寫入索引的文件的根目錄
private string docrootdirectory;
//要匹配的文件格式
private string[] pattern;
/**//// <summary>
/// 初始化一個索引寫入器writer,directory為創建索引的目錄,true代表如果不存在索引文件將重新創建索引文件,如果已經存在索引文件將覆寫索引文件
/// </summary>
/// <param name="directory">傳入的要創建索引的目錄,注意是字符串值,如果目錄不存在,他將會被自動創建</param>
public intranetindexer(string directory)
{
writer = new indexwriter(directory, new standardanalyzer(), true);
writer.setusecompoundfile(true);
}
public void adddirectory(directoryinfo directory, string [] pattern)
{
this.docrootdirectory = directory.fullname;
this.pattern = pattern;
addsubdirectory(directory);
}
private void addsubdirectory(directoryinfo directory)
{
for(int i=0;i<pattern .length ;i++)
{
foreach (fileinfo fi in directory.getfiles(pattern[i]))
{
addhtmldocument(fi.fullname);
}
}
foreach (directoryinfo di in directory.getdirectories())
{
addsubdirectory(di);
}
}
public void addhtmldocument(string path)
{
string exname=path.getextension (path);
document doc = new document();
string html;
if(exname.tolower ()==".html" ||exname .tolower ()==".htm"||exname .tolower ()==".txt")
{
using(streamreader sr=new streamreader (path,system .text .encoding .default ))
{
html = sr.readtoend();
}
}
else
{
using (streamreader sr = new streamreader(path, system.text.encoding.unicode ))
{
html = sr.readtoend();
}
}
int relativepathstartsat = this.docrootdirectory.endswith("http://") ? this.docrootdirectory.length : this.docrootdirectory.length + 1;
string relativepath = path.substring(relativepathstartsat);
string title=path.getfilename(path);
//判斷若是網頁則去標簽否則不用
if(exname.tolower ()==".html" ||exname .tolower ()==".htm")
{
doc.add(field.unstored("text", parsehtml(html)));
}
else
{
doc.add (field .unstored ("text",html));
}
doc.add(field.keyword("path", relativepath));
//doc.add(field.text("title", gettitle(html)));
doc.add (field .text ("title",title));
writer.adddocument(doc);
}
/**//// <summary>
/// 去除網頁中的標簽
/// </summary>
/// <param name="html">網頁</param>
/// <returns>返回去除后的網頁文本</returns>
private string parsehtml(string html)
{
string temp = regex.replace(html, "<[^>]*>", "");
return temp.replace(" ", " ");
}
/**//// <summary>
/// 獲取網頁標題
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
private string gettitle(string html)
{
match m = regex.match(html, "<title>(.*)</title>");
if (m.groups.count == 2)
return m.groups[1].value;
return "文檔標題未知";
}
/**//// <summary>
/// 優化索引并關閉寫入器
/// </summary>
public void close()
{
writer.optimize();
writer.close();
}
}
首先建立document對象,然后為document對象添加一些屬性field.你可以把document對象看成是虛擬文件,將來將從此獲取信息.而field則看成是描述此虛擬文件的元數據(metadata).其中field包括四個類型: keywork
該類型的數據將不被分析,而會被索引并保存保存在索引中.
unindexed
該類型的數據不會被分析也不會被索引,但是會保存在索引.
unstored
和unindexed剛好相反,被分析被索引,但是不被保存.
text
和unstrored類似.如果值的類型為string還會被保存.如果值的類型為reader就不會被保存和unstored一樣.
最后將每一個document添加到索引當中。
下面是對索引進行搜索
//創建一個索引器
indexsearcher searcher = new indexsearcher(indexdirectory);
//解析索引的text字段以便搜索
query query = queryparser.parse(this.q, "text", new standardanalyzer());
//將搜索結果放在hits中
hits hits = searcher.search(query);
//統計搜索的總記錄數
this.total = hits.length();
//高亮顯示
queryhighlightextractor highlighter = new queryhighlightextractor(query, new standardanalyzer(), "<font color=red>", "</font>");
第一步利用indexsearcher打開索引文件用于后面搜索,其中的參數是索引文件的路徑.
第二步使用queryparser將可讀性較好的查詢語句(比如查詢的詞lucene ,以及一些高級方式lucene and .net)轉化為lucene內部使用的查詢對象.
第三步執行搜索.并將結果返回到hits集合.需要注意的是lucene并不是一次將所有的結果放入hits中而是采取一次放一部分的方式.出于空間考慮.
然后將搜索的結果進行處理并在頁面上顯示出來:
for (int i = startat; i < resultscount; i++)
{
document doc = hits.doc(i);
string path = doc.get("path");
string location =server.mappath("documents")+"http://"+path;
string exname=path.getextension (path);
string plaintext ;
string str=doc.get ("title");
if(exname==".html" || exname ==".htm" || exname ==".txt")
{
using (streamreader sr = new streamreader(location, system.text.encoding.default))
{
plaintext = parsehtml(sr.readtoend());
}
}
else
{
using (streamreader sr = new streamreader(location, system.text.encoding.unicode ))
{
plaintext = sr.readtoend();
}
}
//datatable 添加行
datarow row = this.results.newrow();
row["title"] = doc.get("title");
string ip=request.url.host;//獲取服務器ip
//request.url.port;
row["path"][email protected]"http://"+ip+"/webui/search/documents/"+path;
row["sample"] = highlighter.getbestfragments(plaintext, 80, 2, "");
this.results.rows.add(row);
}
searcher.close();//關閉搜索器
想對lucene.net 進行更高級,更全面,更深層次了解的請參閱一下網站:
http://www.alphatom.com/
http://blog.tianya.cn/blogger/view_blog.asp?blogname=aftaft
新聞熱點
疑難解答
圖片精選