DotLucene搜索引擎Demo之:搜索索引

在上篇文章我们说了怎么建立索引,现在说的是怎么搜索这个索引,最主要的我们是要理解startAt的含义,理解了他什么问题都解决了。还有这个例子的分页很经典,我发现google和baidu用的都是这个分页方法。主要就两个方法,一个search()方法,主要是显示当前页的搜索记录
 1 None.gif protected   void  search()
 2 ExpandedBlockStart.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    }
还有就是一个Paging属性,他的作用就是分页,输出分页的html这个属性很经典
 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif    /// 开始分页
 3InBlock.gif    /// </summary>
 4ExpandedBlockEnd.gif    /// <returns></returns>

 5 None.gif      protected  DataTable Paging
 6 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 7InBlock.gif        get
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
 9InBlock.gif           //知道了startAt,分页也很容易了,现在根据startAt得到当前是第几页,注意,现在这里的页数也是暂时从0开始的
10InBlock.gif            int pageNumber = (this.startAt + this.maxResults - 1/ this.maxResults;
11InBlock.gif            DataTable dt = new DataTable();
12InBlock.gif            dt.Columns.Add("html");
13InBlock.gif            DataRow dr = dt.NewRow();
14InBlock.gif            //暂时得到当前页的html连接,注意这里当真正显示页数的时候要+1
15InBlock.gif            dr["html"= pagingNumberHtml(startAt,pageNumber+1,false);
16InBlock.gif            dt.Rows.Add(dr);
17InBlock.gif            //前面显示10页,如果有的话
18InBlock.gif            int previousPagesCount = 10;
19InBlock.gif            //循环把前面页的html连接插到前面去 
20InBlock.gif            for (int i = pageNumber - 1; i >= 0 && i >= pageNumber - previousPagesCount; i--)
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                DataRow r = dt.NewRow();
23InBlock.gif                r["html"= pagingNumberHtml(i*this.maxResults,i+1,true);
24InBlock.gif                dt.Rows.InsertAt(r,0);;
25ExpandedSubBlockEnd.gif            }

26InBlock.gif            //后面也显示10页,如果有的话
27InBlock.gif            int nextPagesCount = 10;
28InBlock.gif            for (int i = pageNumber + 1; i <= this.pageCount && i <= pageNumber + nextPagesCount; i++)
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif                DataRow r = dt.NewRow();
31InBlock.gif                r["html"= pagingNumberHtml(i*this.maxResults,i+1,true);
32InBlock.gif                dt.Rows.Add(r);
33ExpandedSubBlockEnd.gif            }

34InBlock.gif            //添加下一页的超级连接
35InBlock.gif            DataRow lastRow = dt.NewRow();
36InBlock.gif            lastRow["html"= "<a href='Search.aspx?q="+this.Query+"&start="+(pageNumber+1)*this.maxResults+"'>下一页</a>";
37InBlock.gif            dt.Rows.Add(lastRow);
38InBlock.gif            return dt;
39InBlock.gif
40ExpandedSubBlockEnd.gif        }
点这里 开始下载这两篇文章的源代码
如果喜欢本文请把他收藏到 99收藏夹

转载于:https://www.cnblogs.com/pwqzc/archive/2006/03/16/351167.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值