Lucene.net Sample
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Lucene.Net;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
public partial class LucenePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Test();
}
void Test()
{
Analyzer analyzer = new StandardAnalyzer();
// 建立一个内存目录
Directory directory = new RAMDirectory();
// 建立一个索引书写器
//Directory directory = FSDirectory.getDirectory("/tmp/testindex", true);
IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
//IndexWriter iwriter = new IndexWriter("c:/index/", analyzer, true);
iwriter.SetMaxFieldLength(25000);
//要索引的词,这就相当于一个个的要索引的文件
string[] words = { "中华人民共和国", "人民共和国", "人民", "共和国" };
//循环数组,创建文档,给文档添加字段,并把文档添加到索引书写器里
Document doc = null;
for (int i = 0; i < words.Length; i++)
{
doc = new Document();
doc.Add(new Field("fieldname", words[i], Field.Store.YES, Field.Index.TOKENIZED));
iwriter.AddDocument(doc);
}
//关闭索引读写器,一定要关哦,按理说应该把上面的代码用try括主,在finally里关闭索引书写器
iwriter.Optimize();
iwriter.Close();
// 构建一个索引搜索器
IndexSearcher isearcher = new IndexSearcher(directory);
// 用QueryParser.Parse方法实例化一个查询
QueryParser parser = new QueryParser("fieldname", analyzer);
Query query = parser.Parse("共和国");
//获取搜索结果
Hits hits = isearcher.Search(query);
//遍历结果集并输出
for (int i = 0; i < hits.Length(); i++)
{
Document hitDoc = hits.Doc(i);
Response.Write(hitDoc.Get("fieldname"));
}
isearcher.Close();
directory.Close();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Lucene.Net;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
public partial class LucenePage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Test();
}
void Test()
{
Analyzer analyzer = new StandardAnalyzer();
// 建立一个内存目录
Directory directory = new RAMDirectory();
// 建立一个索引书写器
//Directory directory = FSDirectory.getDirectory("/tmp/testindex", true);
IndexWriter iwriter = new IndexWriter(directory, analyzer, true);
//IndexWriter iwriter = new IndexWriter("c:/index/", analyzer, true);
iwriter.SetMaxFieldLength(25000);
//要索引的词,这就相当于一个个的要索引的文件
string[] words = { "中华人民共和国", "人民共和国", "人民", "共和国" };
//循环数组,创建文档,给文档添加字段,并把文档添加到索引书写器里
Document doc = null;
for (int i = 0; i < words.Length; i++)
{
doc = new Document();
doc.Add(new Field("fieldname", words[i], Field.Store.YES, Field.Index.TOKENIZED));
iwriter.AddDocument(doc);
}
//关闭索引读写器,一定要关哦,按理说应该把上面的代码用try括主,在finally里关闭索引书写器
iwriter.Optimize();
iwriter.Close();
// 构建一个索引搜索器
IndexSearcher isearcher = new IndexSearcher(directory);
// 用QueryParser.Parse方法实例化一个查询
QueryParser parser = new QueryParser("fieldname", analyzer);
Query query = parser.Parse("共和国");
//获取搜索结果
Hits hits = isearcher.Search(query);
//遍历结果集并输出
for (int i = 0; i < hits.Length(); i++)
{
Document hitDoc = hits.Doc(i);
Response.Write(hitDoc.Get("fieldname"));
}
isearcher.Close();
directory.Close();
}
}