//第一個(gè)版本應該保存body和title,搜索結果形成超鏈接,不顯示正文。
protected void Button1_Click(object sender, EventArgs e)
{
string indexPath = "c:/index";//設置索引文件保存的路徑
//Directory表示索引文件(Lucene.net用來(lái)保存用戶(hù)扔過(guò)來(lái)的數據的地方)保存的地方
//是抽象類(lèi),兩個(gè)子類(lèi)FSDirectory(文件中)、RAMDire ctory (內存中)。indexpath表示索引的文件夾路徑
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
//IndexReader的靜態(tài)方法bool IndexExists(Directory directory)判斷目錄directory是否是一個(gè)索引目錄。
bool isUpdate = IndexReader.IndexExists(directory);
if (isUpdate)//假如該目錄存在
{
//如果索引目錄被鎖定(比如索引過(guò)程中程序異常退出),則首先解鎖
if (IndexWriter.IsLocked(directory))
{
//在對目錄寫(xiě)之前會(huì )先把目錄鎖定。兩個(gè)IndexWriter沒(méi)法同時(shí)寫(xiě)一個(gè)索引文件。IndexWriter在進(jìn)行寫(xiě)操作的時(shí)候會(huì )自動(dòng)加鎖,close的時(shí)候會(huì )自動(dòng)解鎖
IndexWriter.Unlock(directory);
}
}
//IndexReader對索引進(jìn)行讀取的類(lèi),對IndexWriter進(jìn)行寫(xiě)的類(lèi)
IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;//否則下載的是亂碼
//todo:讀取rss,獲得第一個(gè)item中的鏈接的編號部分就是最大的帖子編號
int maxId = GetMaxId();
for (int i = 1; i <= maxId; i++)
{
//依次獲得地址
string url = "http://localhost:8081/showtopic-" + i + ".aspx";
//根據地址依次將對應的該網(wǎng)頁(yè)下載下來(lái)
string html = wc.DownloadString(url);
//將各個(gè)文檔解析
HTMLDocumentClass doc = new HTMLDocumentClass();
doc.designMode = "on"; //不讓解析引擎去嘗試運行javascript
doc.IHTMLDocument2_write(html);
doc.close();
string title = doc.title;
string body = doc.body.innerText;//去掉標簽
//為避免重復索引,所以先刪除number=i的記錄,再重新添加
writer.DeleteDocuments(new Term("number",i.ToString()));
Document document = new Document();
//只有對需要全文檢索的字段才ANALYZED
document.Add(new Field("number", i.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("title", title, Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("body", body, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(document);
logger.Debug("索引" + i + "完畢");
}
writer.Close();
directory.Close();//不要忘了Close,否則索引結果搜不到
logger.Debug("全部索引完畢");
}
protected void Button2_Click(object sender, EventArgs e)
{
string indexPath = "c:/index";
string kw = TextBox1.Text;
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader);
PhraseQuery query = new PhraseQuery();
//todo:把用戶(hù)輸入的關(guān)鍵詞進(jìn)行拆詞
foreach (string word in CommonHelper.SplitWord(TextBox1.Text))//先用空格,讓用戶(hù)去分詞,空格分隔的就是詞“計算機 專(zhuān)業(yè)”
{
query.Add(new Term("body", word));
}
//query.Add(new Term("body","計算機"));
//query.Add(new Term("body", "專(zhuān)業(yè)"));
query.SetSlop(100);
TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);
searcher.Search(query, null, collector);
ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs;
List<SearchResult> listResult = new List<SearchResult>();
for (int i = 0; i < docs.Length; i++)
{
int docId = docs[i].doc;//取到文檔的編號(主鍵,這個(gè)是Lucene .net分配的)
//檢索結果中只有文檔的id,如果要取Document,則需要Doc再去取
//降低內容占用
Document doc = searcher.Doc(docId);//根據id找Document
string number = doc.Get("number");
string title = doc.Get("title");
string body = doc.Get("body");
SearchResult result = new SearchResult();
result.Number = number;
result.Title = title;
result.BodyPreview = Preview(body,TextBox1.Text);
listResult.Add(result);
}
repeaterResult.DataSource = listResult;
repeaterResult.DataBind();
}
private static string Preview(string body,string keyword)
{
//創(chuàng )建HTMLFormatter,參數為高亮單詞的前后綴
PanGu.HighLight.SimpleHTMLFormatter simpleHTMLFormatter =
new PanGu.HighLight.SimpleHTMLFormatter("<font color=\"red\">", "</font>");
//創(chuàng )建 Highlighter ,輸入HTMLFormatter 和 盤(pán)古分詞對象Semgent
PanGu.HighLight.Highlighter highlighter =
new PanGu.HighLight.Highlighter(simpleHTMLFormatter,
new Segment());
//設置每個(gè)摘要段的字符數
highlighter.FragmentSize = 100;
//獲取最匹配的摘要段
String bodyPreview = highlighter.GetBestFragment(keyword, body);
return bodyPreview;
}
private int GetMaxId()
{
XDocument xdoc = XDocument.Load("http://localhost:8081/tools/rss.aspx");
XElement channel = xdoc.Root.Element("channel");
XElement firstItem = channel.Elements("item").First();
XElement link = firstItem.Element("link");
Match match =
Regex.Match(link.Value, @"showtopic-(\d+)\.aspx");
string id =match.Groups[1].Value;
return Convert.ToInt32(id);
}
}
}
聯(lián)系客服