论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Asp.net教程
Tag:静态页面,treeview,gridview,repeater,dataset,sqldatareader,ado.net,上传,三层,ajax,xml,留言本,新闻发布,商城,注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,触发器,socket,form认证,登录,视频教程

应用lucene.net进行搜索测试

文章类别:Asp.net | 发表日期:2008-10-5 22:08:53

经过上篇我们已经对59部小说文本建立了索引,详情可见http://www.cnblogs.com/abob/archive/2006/08/26/487140.html
 1: 搜索关键字 using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Analysis.Cn;

namespace SearchTest
{
    class SearchTest
    {
        private IndexSearcher searcher = null;
        private Analyzer analyser = null;

        public SearchTest()
        {
            analyser = new ChineseAnalyzer();
            searcher = new IndexSearcher(IndexReader.Open("c:\\index"));
        }

        public Hits search(String queryString, string field)
        {
            if (searcher != null)
            {
                try
                {
                    QueryParser parser = new QueryParser(field,analyser);
                    parser.SetOperator(QueryParser.DEFAULT_OPERATOR_AND);
                    Query query = parser.Parse(queryString);
                    return searcher.Search(query);
                }
                catch { }
            }
            return null;
        }

        static void Main(string[] args)
        {
            SearchTest searcher = new SearchTest();
            DateTime start = DateTime.Now;
            Hits h = searcher.search("中国", "content");
            DateTime end = DateTime.Now;
            long time = end.Ticks - start.Ticks;
            Console.WriteLine("总共耗时:{0}毫秒",Convert.ToString(time));
            Console.WriteLine("总共找到{0}个文件",h.Length());
        }
    }
}

由于搜索是一种注重效率的行为,因此将其构建成一个轻量级的对象能节省不少内存和时间上的开销.在上面的代码中,内置了一个IndexSearcher对象,并调用其search方法来检索.
在代码最后的main函数中,输入了关键字"中国",搜索的结果是

 总共耗时:1406250毫秒
总共找到22个文件
请按任意键继续. . .

2:下面为了性能上的提高,我们来用多线程进行搜索
using System;
using System.Collections.Generic;
using System.Text;
using Lucene.Net.Analysis.Cn;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using System.Threading;


namespace MuiltiSearch
{
    class MultiSearch
    {

        static void Main(string[] args)
        {
            for (int i = 0; i < 50; i++)
            {
                SearchThread s = new SearchThread();
                Thread thread=new Thread(s.run);
                thread.Start();
            }
        }

        }

   public  class SearchThread
   {
        public void run()
        {
            Thread.Sleep(10);
            IndexSearcher searcher = new IndexSearcher(IndexReader.Open("c:\\index"));
            QueryParser parser = new QueryParser("content", new ChineseAnalyzer());
            parser.SetOperator(QueryParser.DEFAULT_OPERATOR_AND);
            Query query = parser.Parse("中国");
            DateTime start = DateTime.Now;

            Hits h = searcher.Search(query);

            DateTime end = DateTime.Now;
            long time = start.Ticks - end.Ticks;

            Console.WriteLine("用时{0}毫秒", time);
        }
    }
}

http://www.cnblogs.com/abob/archive/2006/08/27/487558.html

视频教程列表
文章教程搜索
 
Asp.net推荐教程
Asp.net热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058