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();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值