佳木斯湛栽影视文化发展公司

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

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

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

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

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

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

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

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

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

前面已經(jīng)說到了,索引主要是根據(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)提取出來了,接下來要根據(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目錄下沒有文件');/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)建完了,接下來就是搜索,搜索只要按照固定的格式書寫不會出現(xiàn)錯誤。
復(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('請輸入要查找的關(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)建查詢
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)鍵字查詢
Query query = parser.Parse(keyword);

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

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

int numTotalHits = collector.TotalHits;

//以后就可以對獲取到的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");
//在頁面循環(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)在整個lucene.net搜索全文的過程就建立完了,現(xiàn)在可以搜索txt格式的文件,搜索其他格式的文件在以后添加,主要核心思想就是提取各個不同格式文件的文本內(nèi)容。

顯示效果如下:

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

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

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

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

    • 400-1100-266
    鄂托克前旗| 宜宾市| 靖安县| 泊头市| 庄河市| 阜平县| 台安县| 大洼县| 绥德县| 和龙市| 唐海县| 扶风县| 永善县| 壤塘县| 德阳市| 五家渠市| 武冈市| 金阳县| 渝北区| 苏尼特左旗| 锡林浩特市| 榆社县| 邮箱| 华坪县| 闸北区| 龙门县| 永康市| 河间市| 怀仁县| 奉化市| 安庆市| 卢龙县| 鹿邑县| 绍兴市| 张掖市| 苍溪县| 蒲城县| 吐鲁番市| 中西区| 鄄城县| 乌拉特后旗|