主頁(yè) > 知識(shí)庫(kù) > 基于ASP.NET的lucene.net全文搜索實(shí)現(xiàn)步驟

基于ASP.NET的lucene.net全文搜索實(shí)現(xiàn)步驟

熱門(mén)標(biāo)簽:服務(wù)器配置 阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Mysql連接數(shù)設(shè)置 團(tuán)購(gòu)網(wǎng)站 Linux服務(wù)器 電子圍欄 銀行業(yè)務(wù)

在做項(xiàng)目的時(shí)候,需求添加全文搜索,選擇了lucene.net方向,調(diào)研了一下,基本實(shí)現(xiàn)了需求,現(xiàn)在將它分享給大家。理解不深請(qǐng)多多包涵。

在完成需求的時(shí)候,查看的大量的資料,本文不介紹詳細(xì)的lucene.net工程建立,只介紹如何對(duì)文檔進(jìn)行全文搜索。對(duì)于如何建立lucene.net的工程請(qǐng)大家訪問(wèn)

使用lucene.net搜索分為兩個(gè)部分,首先是創(chuàng)建索引,創(chuàng)建文本內(nèi)容的索引,其次是根據(jù)創(chuàng)建的索引進(jìn)行搜索。那么如何對(duì)文檔進(jìn)行索引呢,主要是對(duì)文檔的內(nèi)容進(jìn)行索引,關(guān)鍵是提取出文檔的內(nèi)容,按照常規(guī)實(shí)現(xiàn),由簡(jiǎn)到難,提取txt格式的文本相對(duì)比較簡(jiǎn)單,如果實(shí)現(xiàn)了提取txt文本,接下來(lái)就容易多了,萬(wàn)丈高樓平地起,這就是地基。

1.首先創(chuàng)建ASP.NET頁(yè)面。

這是一個(gè)極其簡(jiǎn)單的頁(yè)面,創(chuàng)建頁(yè)面之后,雙擊各個(gè)按鈕生成相應(yīng)的點(diǎn)擊事件,在相應(yīng)的點(diǎn)擊事件中實(shí)現(xiàn)程序設(shè)計(jì)。

2.實(shí)現(xiàn)索引部分。

前面已經(jīng)說(shuō)到了,索引主要是根據(jù)文本內(nèi)容建立索引,所以要提取文本內(nèi)容。創(chuàng)建提取txt格式文檔文本內(nèi)容的函數(shù)。

復(fù)制代碼 代碼如下:
 
//提取txt文件
public static string FileReaderAll(FileInfo fileName)
{
//讀取文本內(nèi)容,并且默認(rèn)編碼格式,防止出現(xiàn)亂碼
StreamReader reader = new StreamReader(fileName.FullName, System.Text.Encoding.Default);
string line = "";
string temp = "";
//循環(huán)讀取文本內(nèi)容
while ((line = reader.ReadLine()) != null)
{
temp += line;
}
reader.Close();
//返回字符串,用于lucene.net生成索引
return temp;
}

文本內(nèi)容已經(jīng)提取出來(lái)了,接下來(lái)要根據(jù)提取的內(nèi)容建立索引
復(fù)制代碼 代碼如下:
 
protected void Button2_Click(object sender, EventArgs e)
{
//判斷存放文本的文件夾是否存在
if (!System.IO.Directory.Exists(filesDirectory))
{
Response.Write("script>alert('指定的目錄不存在');/script>");
return;
}
//讀取文件夾內(nèi)容
DirectoryInfo dirInfo = new DirectoryInfo(filesDirectory);
FileInfo[] files = dirInfo.GetFiles("*.*");
//文件夾判空
if (files.Count() == 0)
{
Response.Write("script>alert('Files目錄下沒(méi)有文件');/script>");
return;
}
//判斷存放索引的文件夾是否存在,不存在創(chuàng)建
if (!System.IO.Directory.Exists(indexDirectory))
{
System.IO.Directory.CreateDirectory(indexDirectory);
}
//創(chuàng)建索引
IndexWriter writer = new IndexWriter(FSDirectory.Open(new DirectoryInfo(indexDirectory)),
analyzer, true, IndexWriter.MaxFieldLength.LIMITED);

for (int i = 0; i files.Count(); i++)
{
string str = "";
FileInfo fileInfo = files[i];
//判斷文件格式,為以后其他文件格式做準(zhǔn)備
if (fileInfo.FullName.EndsWith(".txt") || fileInfo.FullName.EndsWith(".xml"))
{
//獲取文本
str = FileReaderAll(fileInfo);
}
Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
doc.Add(new Lucene.Net.Documents.Field("FileName", fileInfo.Name, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
//根據(jù)文本生成索引
doc.Add(new Lucene.Net.Documents.Field("Content", str, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.ANALYZED));
doc.Add(new Lucene.Net.Documents.Field("Path", fileInfo.FullName, Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO));
//添加生成的索引
writer.AddDocument(doc);
writer.Optimize();
}
writer.Dispose();
Response.Write("script>alert('索引創(chuàng)建成功');/script>");
}

3.索引創(chuàng)建完了,接下來(lái)就是搜索,搜索只要按照固定的格式書(shū)寫(xiě)不會(huì)出現(xiàn)錯(cuò)誤。
復(fù)制代碼 代碼如下:
 
protected void Button1_Click(object sender, EventArgs e)
{
//獲取關(guān)鍵字
string keyword = TextBox1.Text.Trim();
int num = 10;
//關(guān)鍵字判空
if (string.IsNullOrEmpty(keyword))
{
Response.Write("script>alert('請(qǐng)輸入要查找的關(guān)鍵字');/script>");
return;
}

IndexReader reader = null;
IndexSearcher searcher = null;
try
{
reader = IndexReader.Open(FSDirectory.Open(new DirectoryInfo(indexDirectory)), true);
searcher = new IndexSearcher(reader);
//創(chuàng)建查詢(xún)
PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(analyzer);
wrapper.AddAnalyzer("FileName", analyzer);
wrapper.AddAnalyzer("Path", analyzer);
wrapper.AddAnalyzer("Content", analyzer);
string[] fields = { "FileName", "Path", "Content" };

QueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, fields, wrapper);
//根據(jù)關(guān)鍵字查詢(xún)
Query query = parser.Parse(keyword);

TopScoreDocCollector collector = TopScoreDocCollector.Create(num, true);

searcher.Search(query, collector);
//這里會(huì)根據(jù)權(quán)重排名查詢(xún)順序
var hits = collector.TopDocs().ScoreDocs;

int numTotalHits = collector.TotalHits;

//以后就可以對(duì)獲取到的collector數(shù)據(jù)進(jìn)行操作
for (int i = 0; i hits.Count(); i++)
{
var hit = hits[i];
Lucene.Net.Documents.Document doc = searcher.Doc(hit.Doc);
Lucene.Net.Documents.Field fileNameField = doc.GetField("FileName");
Lucene.Net.Documents.Field pathField = doc.GetField("Path");
Lucene.Net.Documents.Field contentField = doc.GetField("Content");
//在頁(yè)面循環(huán)輸出表格
strTable.Append("tr>");
strTable.Append("td>" + fileNameField.StringValue + "/td>");
strTable.Append("/tr>");
strTable.Append("tr>");
strTable.Append("td>" + pathField.StringValue + "/td>");
strTable.Append("/tr>");
strTable.Append("tr>");
strTable.Append("td>" + contentField.StringValue.Substring(0, 300) + "/td>");
strTable.Append("/tr>");
}
}
finally
{
if (searcher != null)
searcher.Dispose();

if (reader != null)
reader.Dispose();
}
}

現(xiàn)在整個(gè)lucene.net搜索全文的過(guò)程就建立完了,現(xiàn)在可以搜索txt格式的文件,搜索其他格式的文件在以后添加,主要核心思想就是提取各個(gè)不同格式文件的文本內(nèi)容。

顯示效果如下:

在以后的博文里繼續(xù)接受搜索其他格式的文檔。

您可能感興趣的文章:
  • Lucene.Net實(shí)現(xiàn)搜索結(jié)果分類(lèi)統(tǒng)計(jì)功能(中小型網(wǎng)站)
  • Java實(shí)現(xiàn)lucene搜索功能的方法(推薦)
  • 基于Lucene的Java搜索服務(wù)器Elasticsearch安裝使用教程
  • 使用Java的Lucene搜索工具對(duì)檢索結(jié)果進(jìn)行分組和分頁(yè)
  • 使用Lucene.NET實(shí)現(xiàn)站內(nèi)搜索
  • 使用Lucene實(shí)現(xiàn)一個(gè)簡(jiǎn)單的布爾搜索功能

標(biāo)簽:衡水 衢州 棗莊 蚌埠 萍鄉(xiāng) 江蘇 大理 廣元

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于ASP.NET的lucene.net全文搜索實(shí)現(xiàn)步驟》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266