Lucene.net初探

引言

       在分析同事开发的客户端搜索项目时注意到,搜索的关键是索引,而提到索引就不得不提Lucene.net,思路就是将需要搜索内容写入索引,客户端自己或局域网其他机器搜索时直接搜索索引,从而查看到你共享的信息。

       初探Lucene.net时关注了几个关键类:

       a):IndexReader 索引读取。

       b):IndexWriter  创建索引。

       c):StandardAnalyzer 分词解析,这个应用就比较多了,他解析英文和中文时会拆成单个的字母或者汉字,如果使用PanGuAnalyzer【盘古分析解析】则是拆分成词组搜索。

       d):IndexSearcher 索引搜索。

 

效果

1、写入内容时的索引文件

 

2、输入关键字之后搜索效果

 

代码

 static void Main(string[] args)
        {
            //CreateIndex("Oracle", "甲骨文公司,全称甲骨文股份有限公司(甲骨文软件系统有限公司),是全球最大的企业软件公司,总部位于美国加利福尼亚州的红木滩。1989年正式进入中国市场。2013年,甲骨文已超越 IBM ,成为继 Microsoft 后全球第二大软件公司");

            ReadIndex("oracle 公司 IBM");

            Console.ReadKey();

        }

        private static void CreateIndex(string title,string content)
        {
            string indexpath = AppDomain.CurrentDomain.BaseDirectory + "IndexData";
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexpath), new NativeFSLockFactory());

            bool isExist = IndexReader.IndexExists(directory); //判断该索引是否存在
            if (isExist)
            {
                if (IndexWriter.IsLocked(directory)) //写入索引时需要解锁
                {
                    IndexWriter.Unlock(directory);
                }
            }

            IndexWriter indexWriter=new IndexWriter(directory,new StandardAnalyzer(),!isExist,IndexWriter.MaxFieldLength.UNLIMITED);

            Document document =new Document();

            Field f1 =new Field("title",title,Field.Store.YES,Field.Index.TOKENIZED);
            Field f2 = new Field("content", content, Field.Store.YES, Field.Index.TOKENIZED);

            document.Add(f1);
            document.Add(f2);

            indexWriter.AddDocument(document);

            indexWriter.Optimize();
            indexWriter.Close();
        }

        private static void ReadIndex(string keywords)
        {
            string indexpath = AppDomain.CurrentDomain.BaseDirectory + "IndexData";
            string title = "";
            string content = "";

            StandardAnalyzer analyzer=new StandardAnalyzer(); //分词
            IndexSearcher searcher=new IndexSearcher(indexpath); //索引搜索  -- 传入索引文件路径

            //MultiFieldQueryParser多字段搜索,一次可以传入多个需要解析的内容, 
            //如果需要一次传入一个就使用QueryParse
            MultiFieldQueryParser parser =new MultiFieldQueryParser(new string[]{"title","content"},analyzer );


            Query query = parser.Parse(keywords);//转化为Lucene内部使用的查询对象
            Hits hits = searcher.Search(query); //执行搜索

            for (int i=0; i<hits.Length(); i++)
            {
                Document doc = hits.Doc(i);
                title += doc.Get("title");
                content += doc.Get("content");
            }
            searcher.Close();

            Console.WriteLine("Title:"+title);
            Console.WriteLine("Content:" + content);

        }
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值