Lucene学习(1)--------VS2015

来源:互联网 发布:卧蚕阿姨的淘宝店铺 编辑:程序博客网 时间:2024/06/17 08:55

最近做搜索功能发现有Lucene这么个东西,然后就试着写了个DEMO,功力有限请大家见谅

首先,感谢前辈们的分享
(注:部分内容参考ORCHARD,以及http://blog.csdn.net/shanluan_/article/details/9150161)

第一步,右键项目,选择管理NuGet程序包,在浏览中输入Lucene选择第一项进行安装,然后添加引用

using Lucene.Net.Analysis;using Lucene.Net.Analysis.Standard;using Lucene.Net.Documents;using Lucene.Net.Index;using Lucene.Net.Store;using Lucene.Net.Util;using System.IO;

第二步,写一个创建索引的方法

            string path = @"D:\Sample";            //创建索引库目录            var directoryInfo = new DirectoryInfo(path);            Lucene.Net.Store.Directory dit = FSDirectory.Open(directoryInfo);            //创建一个索引,采用StandardAnalyzer对句子进行分词            IndexWriter indexWriter = new IndexWriter(dit, new StandardAnalyzer(Version.LUCENE_29), true, IndexWriter.MaxFieldLength.UNLIMITED);            //域的集合:文档,类似于表的行            Document doc = new Document();            //要索引的字段            doc.Add(new Field("ID", "11", Field.Store.YES, Field.Index.NOT_ANALYZED));            doc.Add(new Field("Title", "测试", Field.Store.NO, Field.Index.ANALYZED));            doc.Add(new Field("Content","研究研究索引", Field.Store.YES, Field.Index.ANALYZED));            indexWriter.AddDocument(doc);            //对索引文件进行优化            indexWriter.Optimize();            indexWriter.Dispose();            dit.Dispose();

运行之后生成的索引内容可以通过索引查看器(lukeall)进行查看,实际内容请看下图

这里写图片描述

至此生成索引已完成,有不规范的地方欢迎指正

原创粉丝点击