• 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);
    
            }
        }
    
  • 相关阅读:
    webpack 3.X学习之CSS处理
    webpack 3.X学习之图片处理
    webpack 3.X学习之基本配置
    webpack 3.X学习之JS压缩与打包HTML文件
    webpack 3.X学习之初始构建
    【复习】VueJS之内部指令
    前端学习记录之Javascript-DOM
    javascript常用的Math对象的方法
    nodejs+mongoose+websocket搭建xxx聊天室
    Markdown常用语法
  • 原文地址:https://www.cnblogs.com/sword-successful/p/3941817.html
Copyright © 2020-2023  润新知