一.摘要
本篇文章作为Lucene.net的开篇文章向您展示在ASP.NET中的基本使用,更深入的话题比如:搜索的关键字高亮显示,索引从数据库中查询的数据,爬虫,索引更新等高级话题将在后续文章中描述.
二.内容
建立一个ASP.NET网站,添加Lucene.net.dll的引用,在Default.aspx页面里添加如下内容:
1: <%@ Page Language="C#" %>
2:
3: <%@ Import Namespace="Lucene.Net.Index" %>
4: <%@ Import Namespace="Lucene.Net.Analysis.Standard" %>
5: <%@ Import Namespace="Lucene.Net.Documents" %>
6: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7:
8: <script runat="server">1:
2:
3: protected void Page_Load(object sender, EventArgs e)4: {
5: //索引位置6: string sIndexLocation = MapPath("~/searchlucene/");7: bool created;8: //检测索引是否存在9: IsIndexExists(out created, sIndexLocation);10:
11: }
12: private void IsIndexExists(out bool created, string sIndexPath)13: {
14: created = false;15: if (!IndexReader.IndexExists(sIndexPath))16: {
17: IndexWriter writer = new IndexWriter(sIndexPath, new StandardAnalyzer(), true);18: created = true;19: writer.Close();
20: }
21: }
22: //创建索引23: protected void btnCreateIndex_Click(object sender, EventArgs e)24: {
25: IndexWriter writer = new IndexWriter(MapPath("~/searchlucene/"), new StandardAnalyzer(), false);26:
27: IndexDocument(writer, "中国", "China", "北京");28: IndexDocument(writer, "美国", "USA", "密歇根");29: IndexDocument(writer, "日本", "Jap", "小日本");30: IndexDocument(writer, "中国", "China", "人均");31: IndexDocument(writer, "2010", "情人节", "万维网");32:
33: writer.Optimize();
34: writer.Close();
35:
36: }
37: //添加文件到索引38: private void IndexDocument(IndexWriter writer, string sHeader, string sType, string sContent)39: {
40: Document doc = new Document();41:
42: doc.Add(new Field("header", sHeader, Field.Store.YES, Field.Index.TOKENIZED));43: doc.Add(new Field("type", sType, Field.Store.YES, Field.Index.UN_TOKENIZED));44: doc.Add(new Field("content", sContent, Field.Store.YES, Field.Index.TOKENIZED));45:
46: writer.AddDocument(doc);
47:
48: }
49: //搜索50: protected void btnSearch_Click(object sender, EventArgs e)51: {
52: ListBox1.Items.Clear();
53:
54: var searcher = new Lucene.Net.Search.IndexSearcher(MapPath("~/searchlucene/"));55: var oParser = new Lucene.Net.QueryParsers.QueryParser("content", new StandardAnalyzer());56: string sHeader = " OR (header:" + TextBox1.Text + ")";57: string sSearchQuery = "(" + TextBox1.Text + sHeader + ")";58:
59: //解析搜索关键字60: var oHitColl = searcher.Search(oParser.Parse(sSearchQuery));61: for (int i = 0; i < oHitColl.Length(); i++)62: {
63: Document oDoc = oHitColl.Doc(i);
64: ListBox1.Items.Add(new ListItem(oDoc.Get("header") + oDoc.Get("type") + oDoc.Get("content")));65: }
66:
67: searcher.Close();
68: }
script>
9:
10: <html xmlns="http://www.w3.org/1999/xhtml">
11: <head id="Head1" runat="server">
12: <title>Lucene例子title>
13: head>
14: <body>
15: <form id="form1" runat="server">
16: <div>
17: div>
18: 1-
19: <asp:Button ID="btnCreateIndex" runat="server" OnClick="btnCreateIndex_Click" Text="index create" />
20: <br />
21: <br />
22: 2- <asp:TextBox ID="TextBox1" runat="server">asp:TextBox>
23: <asp:Button ID="btnSearch" runat="server" onclick="btnSearch_Click" Text="Search" />
24: <p>
25: p>
26: <p>
27: p>
28: <asp:ListBox ID="ListBox1" runat="server" Height="322px" Width="712px">asp:ListBox>
29: form>
30: body>
31: html>
在文本框中输入关键字比如:中国.将列出包含中国关键字的条目
三.源码
由于CSDN上传的源码要经过审核,所以我稍后会以评论的形式提供完整版源码地址,谢谢您的访问!