Lucene.Net核心类简介

先运行写好的索引的代码,再向下讲解各个类的作用,不用背代码。

(*)Directory表示索引文件(Lucene.net用来保存用户扔过来的数据的地方)保存的地方,是抽象类,两个子类FSDirectory(文件中)、RAMDirectory (内存中)。使用的时候别和IO里的Directory弄混了。

创建FSDirectory的方法,FSDirectory directory =FSDirectory.Open(new DirectoryInfo(indexPath),new NativeFSLockFactory()), path索引的文件夹路径

IndexReader对索引进行读取的类,对IndexWriter进行写的类。IndexReader的静态方法bool IndexExists(Directory directory)判断目录directory是否是一个索引目录。IndexWriter的bool IsLocked(Directory directory) 判断目录是否锁定,在对目录写之前会先把目录锁定。两个IndexWriter没法同时写一个索引文件。IndexWriter在进行写操作的时候会自动 加锁,close的时候会自动解锁。IndexWriter.Unlock方法手动解锁(比如还没来得及close IndexWriter 程序就崩溃了,可能造成一直被锁定)。

创建索引

构造函数:IndexWriter(Directorydir, Analyzer a, bool create, MaxFieldLength mfl)因为IndexWriter把输入写入索引的时候,Lucene.net是把写入的文件用指定的分词器将文章分词(这样检索的时候才能查的快), 然后将词放入索引文件。

void AddDocument(Document doc),向索引中添加文档(Insert)。Document类代表要索引的文档(文章),最重要的方法Add(Field field),向文档中添加字段。Document是一片文档,Field是字段(属性)。Document相当于一条记录,Field相当于字段。

Field类的构造函数 Field(string name, string value, Field.Store store, Field.Indexindex, Field.TermVector termVector):name表示字段名; value表示字段值;

store表示是否存储value值,可选值Field.Store.YES存储,Field.Store.NO不存 储,Field.Store.COMPRESS压缩存储;默认只保存分词以后的一堆词,而不保存分词之前的内容,搜索的时候无法根据分词后的东西还原原 文,因此如果要显示原文(比如文章正文)则需要设置存储。

index表示如何创建索引,可选值Field.Index. NOT_ANALYZED,不创建索引,Field.Index. ANALYZED,创建索引;创建索引的字段才可以比较好的检索。是否碎尸万段!是否需要按照这个字段进行“全文检索”。

termVector表示如何保存索引词之间的距离。“北京欢迎你们大家”,索引中是如何保存“北京”和“大家”之间“隔多少单词”。方便只检索在一定距离之内的词。

为什么要把帖子的url做为一个Field,因为要在搜索展示的时候先帖子地址取出来构建超链接,所以Field.Store.YES;一般不需要 对url进行检索,所以Field.Index.NOT_ANALYZED 。根据《红楼梦》构建的“词:页数”纸,在构建完成后就可以把原文《红楼梦》扔了

案例:对1000至1100号帖子进行索引。“只要能看懂例子和文档,稍作修改即可实现自己的需求”。除了基础知识外,第三方开发包只要“能看懂,改改即可”

引入命名空间:

  1. using Lucene.Net.Store;  
  2. using System.IO;  
  3. using Lucene.Net.Index;  
  4. using Lucene.Net.Analysis.PanGu;  
  5. using Lucene.Net.Documents;  
  6. using Lucene.Net.Search;  

1 对数据进行索引

  1. string indexPath = @"C:\1017index";//注意和磁盘上文件夹的大小写一致,否则会报错。  
  2. FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());  
  3. bool isUpdate = IndexReader.IndexExists(directory);//判断索引库是否存在  
  4. if (isUpdate)  
  5. {
  6. //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁
  7. //Lucene.Net在写索引库之前会自动加锁,在close的时候会自动解锁
  8. //不能多线程执行,只能处理意外被永远锁定的情况
  9. if (IndexWriter.IsLocked(directory))  
  10. {
  11. IndexWriter.Unlock(directory);//un-否定。强制解锁
  12. }
  13. }
  14. IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);  
  15. for (int i = 1000; i < 1100; i++)  
  16. {
  17. string txt = File.ReadAllText(@"D:\我的文档\文章\" + i + ".txt");  
  18. Document document = new Document();//一条Document相当于一条记录  
  19. document.Add(new Field("id", i.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));  
  20. //每个Document可以有自己的属性(字段),所有字段名都是自定义的,值都是string类型
  21. //Field.Store.YES不仅要对文章进行分词记录,也要保存原文,就不用去数据库里查一次了
  22. //需要进行全文检索的字段加 Field.Index. ANALYZED
  23. document.Add(new Field("msg", txt, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));  
  24. //防止重复索引
  25. writer.DeleteDocuments(new Term("id", i.ToString()));//防止存在的数据//delete from t where id=i  
  26. //如果不存在则删除0条
  27. writer.AddDocument(document);//把文档写入索引库
  28. }
  29. writer.Close();
  30. directory.Close();//不要忘了Close,否则索引结果搜不到

2、搜索的代码

  1. string indexPath = @"C:\1017index";  
  2. string kw = TextBox1.Text;  
  3. FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());  
  4. IndexReader reader = IndexReader.Open(directory, true);  
  5. IndexSearcher searcher = new IndexSearcher(reader);  
  6. PhraseQuery query = new PhraseQuery();//查询条件  
  7. query.Add(new Term("msg", kw));//where contains("msg",kw)  
  8. //foreach (string word in kw.Split(' '))//先用空格,让用户去分词,空格分隔的就是词“计算机 专业”
  9. //{
  10. //    query.Add(new Term("msg", word));//contains("msg",word)
  11. //}
  12. query.SetSlop(100);//两个词的距离大于100(经验值)就不放入搜索结果,因为距离太远相关度就不高了
  13. TopScoreDocCollector collector = TopScoreDocCollector.create(1000, true);//盛放查询结果的容器  
  14. searcher.Search(query, null, collector);//使用query这个查询条件进行搜索,搜索结果放入collector  
  15. //collector.GetTotalHits()总的结果条数
  16. ScoreDoc[] docs = collector.TopDocs(0, collector.GetTotalHits()).scoreDocs;//从查询结果中取出第m条到第n条的数据
  17. List<SearchResult> list = new List<SearchResult>();  
  18. for (int i = 0; i < docs.Length; i++)//遍历查询结果  
  19. {
  20. int docId = docs[i].doc;//拿到文档的id。因为Document可能非常占内存(DataSetDataReader的区别)  
  21. //所以查询结果中只有id,具体内容需要二次查询
  22. Document doc = searcher.Doc(docId);//根据id查询内容。放进去的是Document,查出来的还是Document
  23. //Console.WriteLine(doc.Get("id"));
  24. //Console.WriteLine(doc.Get("msg"));
  25. SearchResult result = new SearchResult();  
  26. result.Id = Convert.ToInt32(doc.Get("id"));
  27. result.Msg = doc.Get("msg");//只有 Field.Store.YES的字段才能用Get查出来
  28. list.Add(result);
  29. }
  30. Repeater1.DataSource = list;
  31. Repeater1.DataBind();

aspx代码:

  1. <form id="form1" runat="server">
  2. <div>
  3. <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="创建索引" />
  4. <br />
  5. <br />
  6. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  7. <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="搜索" />
  8. <br />
  9. <ul>
  10. <asp:Repeater ID="Repeater1" runat="server">
  11. <ItemTemplate><li>Id:<%#Eval("Id") %><br /><%#Eval("Msg") %></li></ItemTemplate>
  12. </asp:Repeater>
  13. </ul>
  14. </div>
  15. </form>
  16. 本文摘自51CTO;

Lucene.net站内搜索-最简单搜索引擎代码的更多相关文章

  1. Lucene.net站内搜索—4、搜索引擎第一版技术储备(简单介绍Log4Net、生产者消费者模式)

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  2. Lucene.net站内搜索—5、搜索引擎第一版实现

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  3. Lucene.net站内搜索—3、最简单搜索引擎代码

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  4. Lucene.net站内搜索—6、站内搜索第二版

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  5. Lucene.net站内搜索—2、Lucene.Net简介和分词

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  6. Lucene.net站内搜索—1、SEO优化

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  7. Lucene.Net 站内搜索

    Lucene.Net 站内搜索 一  全文检索: like查询是全表扫描(为性能杀手)Lucene.Net搜索引擎,开源,而sql搜索引擎是收费的Lucene.Net只是一个全文检索开发包(只是帮我们 ...

  8. 使用Lucene.NET实现简单的站内搜索

    使用Lucene.NET实现简单的站内搜索 导入Lucene.NET 开发包 Lucene 是apache软件基金会一个开放源代码的全文检索引擎工具包,是一个全文检索引擎的架构,提供了完整的查询引擎和 ...

  9. 基于lucene.net 和ICTCLAS2014的站内搜索的实现1

    Lucene.net是一个搜索引擎的框架,它自身并不能实现搜索.须要我们自己在当中实现索引的建立,索引的查找.全部这些都是依据它自身提供的API来实现.Lucene.net本身是基于java的,可是经 ...

随机推荐

  1. jenkins 插件

  2. GoogleTest 之路2-Googletest 入门(Primer)

    Why googletest? 为啥要用GoogleTest呢? googletest 是由测试技术Team 开发的带有google 特殊的需求和限制的测试框架. 不管你在什么平台上写C++代码,go ...

  3. 【android】安卓开发apk列表

    - 谷歌的Zxing框架的扫码软件 (目前国内的应用商店很少此种类型的扫码app) - 解析IP地址功能,从IP地址(子网掩码)自动解析出网段,广播地址

  4. python3爬取豆瓣top250电影

    需求:爬取豆瓣电影top250的排名.电影名称.评分.评论人数和一句话影评 环境:python3.6.5 准备工作: 豆瓣电影top250(第1页)网址:https://movie.douban.co ...

  5. 剑指Offer(书):顺时针打印数组

    题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1 ...

  6. Divisibility by 25 CodeForces - 988E

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  7. JAVA、JDK等入门概念,下载安装JAVA并配置环境变量

    一.概念 Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言,具体介绍可查阅百度JAVA百科,这里不再赘述. Java分为三个体系,分别为: Java SE(J2SE,Java2 Platf ...

  8. HBase0.94.2-cdh4.2.0需求评估测试报告1.0之二

    Hbase 配置文件: hbase-site.xml <configuration> <property> <name>hbase.cluster.distribu ...

  9. python - log日志

    # -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: study_logging.py @ide: PyCharm Comm ...

  10. doc下设置永久环境变量的好方法

    http://www-2w.blog.163.com/blog/static/97931518201021211123267/ 需要查看命令具体实现:setx machine “%path%”. 配置 ...