zoukankan      html  css  js  c++  java
  • 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);
    
            }
        }
    
  • 相关阅读:
    2015年10月28日 json深切感受
    2015年10月27日 json数据小谈
    2015年10月26日 插件的使用
    2015/10/25 用bootstrap selectpicker实现带模糊查询的下拉列表
    2015年10月24号 哇,原来这JSP页面还可以用这东西!
    2015年10月23日 关于spring mvc的初认识
    2015年10月22日 杂感
    2015/10/21 http请求数据处理后显示
    2015/10/19总结:ajax传参、jquery.validate自定义日期校验
    记录一下----关于设计模式和面向对象设计原则
  • 原文地址:https://www.cnblogs.com/sword-successful/p/3941817.html
Copyright © 2011-2022 走看看