zoukankan      html  css  js  c++  java
  • DotLucene搜索索引(上)

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

    1protected void search()
    2    {
    3         DateTime start = DateTime.Now;//搜索的开始时间
    4        //得到索引所在的目录,我们在上个console程序里把索引放到了index目录下
    5        string indexDirectory = Server.MapPath("index");
    6        //创建个索引搜索器
    7         IndexSearcher searcher = new IndexSearcher(indexDirectory);
    8        //分词并解析索引的text字段以便搜索
    9         Query thisQuery = QueryParser.Parse(this.Query,"text",new StandardAnalyzer());
    10        //为要绑定输出到页面的results建立几列
    11        this.Results.Columns.Add("path",typeof(string));
    12        this.Results.Columns.Add("sample",typeof(string));
    13        this.Results.Columns.Add("title",typeof(string));
    14        //开始搜索
    15         Hits hits = searcher.Search(thisQuery);
    16        //得到搜索返回的记录总数
    17        this.total = hits.Length();
    18        //创建一个高亮
    19         QueryHighlightExtractor highlighter = new QueryHighlightExtractor(thisQuery, new StandardAnalyzer(), "<B>", "</B>");
    20        //初始化startAt,以便得到要显示的结果集
    21        this.startAt = initStartAt();
    22        //得到当前页要显示的记录数量,包括以前所有页的记录数,这样把他与this.startAt结合就能够很好的知道当前页要显示的记录数了
    23        int resultsCount = smallOf(this.total,this.startAt+this.maxResults);
    24        //开始循环得到当前页要显示的记录
    25        for (int i = this.startAt; i < resultsCount; i++)
    26        {
    27           //得到每一行Hits的Document,因为Hits的没一行都是个Document对象
    28             Document doc = hits.Doc(i);
    29            //得到doc里面的列path的值
    30            string path = doc.Get("path");
    31            //再得到这个路径在web程序的路径,我们原来把文档放到了web根目录的documents目录下的
    32            string location = Server.MapPath(@"documents\"+path);
    33            //用StreamReader读取文档,因为我们不能够直接从索引中得到text字段的值,因为我们建立索引的时候没有存储他的
    34            string plainText;
    35            using (StreamReader sr = new StreamReader(location, System.Text.Encoding.Default))
    36            {
    37                 plainText = ParseHtml(sr.ReadToEnd());
    38             }

    39            //为结果集DataTable,Results添加个新行
    40             DataRow dr = this.Results.NewRow();
    41             dr["title"] = doc.Get("title");
    42             dr["path"] = @"documents/" + path;
    43             dr["sample"] = highlighter.GetBestFragment(plainText,80);
    44            //把行添加进DataTable
    45            this.Results.Rows.Add(dr);
    46         }

    47        //循环完毕,关闭搜索
    48         searcher.Close();
    49        //搜索花费多少时间
    50        this.duration = DateTime.Now - start;
    51        //给fromItem赋值,他总是startAt+1
    52        this.fromItem = this.startAt + 1;
    53        //给toItem赋值
    54        this.toItem = smallOf(this.total,this.startAt+this.maxResults);
    55
    56     }

  • 相关阅读:
    WEP编码格式
    OSK VFS read数据流分析
    科学剖析濒死体验 "复生"者讲述"死"前1秒钟
    Android的开发相对于tizen的开发难度
    minix文件系统分析
    贴给小程序(1) 查找第一个0值
    Linux下的QQ
    OSK USB 驱动
    LRU算法之hash+list实现(转)
    插入排序
  • 原文地址:https://www.cnblogs.com/88223100/p/1270624.html
Copyright © 2011-2022 走看看