DotLucene搜索索引(上)

主要就两个方法,一个search()方法,主要是显示当前页的搜索记录

1None.gifprotected void search()
2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
3InBlock.gif         DateTime start = DateTime.Now;//搜索的开始时间
4InBlock.gif        //得到索引所在的目录,我们在上个console程序里把索引放到了index目录下
5InBlock.gif        string indexDirectory = Server.MapPath("index");
6InBlock.gif        //创建个索引搜索器
7InBlock.gif         IndexSearcher searcher = new IndexSearcher(indexDirectory);
8InBlock.gif        //分词并解析索引的text字段以便搜索
9InBlock.gif         Query thisQuery = QueryParser.Parse(this.Query,"text",new StandardAnalyzer());
10InBlock.gif        //为要绑定输出到页面的results建立几列
11InBlock.gif        this.Results.Columns.Add("path",typeof(string));
12InBlock.gif        this.Results.Columns.Add("sample",typeof(string));
13InBlock.gif        this.Results.Columns.Add("title",typeof(string));
14InBlock.gif        //开始搜索
15InBlock.gif         Hits hits = searcher.Search(thisQuery);
16InBlock.gif        //得到搜索返回的记录总数
17InBlock.gif        this.total = hits.Length();
18InBlock.gif        //创建一个高亮
19InBlock.gif         QueryHighlightExtractor highlighter = new QueryHighlightExtractor(thisQuery, new StandardAnalyzer(), "<B>", "</B>");
20InBlock.gif        //初始化startAt,以便得到要显示的结果集
21InBlock.gif        this.startAt = initStartAt();
22InBlock.gif        //得到当前页要显示的记录数量,包括以前所有页的记录数,这样把他与this.startAt结合就能够很好的知道当前页要显示的记录数了
23InBlock.gif        int resultsCount = smallOf(this.total,this.startAt+this.maxResults);
24InBlock.gif        //开始循环得到当前页要显示的记录
25InBlock.gif        for (int i = this.startAt; i < resultsCount; i++)
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif           //得到每一行Hits的Document,因为Hits的没一行都是个Document对象
28InBlock.gif             Document doc = hits.Doc(i);
29InBlock.gif            //得到doc里面的列path的值
30InBlock.gif            string path = doc.Get("path");
31InBlock.gif            //再得到这个路径在web程序的路径,我们原来把文档放到了web根目录的documents目录下的
32InBlock.gif            string location = Server.MapPath(@"documents\"+path);
33InBlock.gif            //用StreamReader读取文档,因为我们不能够直接从索引中得到text字段的值,因为我们建立索引的时候没有存储他的
34InBlock.gif            string plainText;
35InBlock.gif            using (StreamReader sr = new StreamReader(location, System.Text.Encoding.Default))
36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
37InBlock.gif                 plainText = ParseHtml(sr.ReadToEnd());
38ExpandedSubBlockEnd.gif             }
39InBlock.gif            //为结果集DataTable,Results添加个新行
40InBlock.gif             DataRow dr = this.Results.NewRow();
41InBlock.gif             dr["title"] = doc.Get("title");
42InBlock.gif             dr["path"] = @"documents/" + path;
43InBlock.gif             dr["sample"] = highlighter.GetBestFragment(plainText,80);
44InBlock.gif            //把行添加进DataTable
45InBlock.gif            this.Results.Rows.Add(dr);
46ExpandedSubBlockEnd.gif         }
47InBlock.gif        //循环完毕,关闭搜索
48InBlock.gif         searcher.Close();
49InBlock.gif        //搜索花费多少时间
50InBlock.gif        this.duration = DateTime.Now - start;
51InBlock.gif        //给fromItem赋值,他总是startAt+1
52InBlock.gif        this.fromItem = this.startAt + 1;
53InBlock.gif        //给toItem赋值
54InBlock.gif        this.toItem = smallOf(this.total,this.startAt+this.maxResults);
55InBlock.gif
56ExpandedBlockEnd.gif     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值