Lucene.Net基本用法

Admin 于 2008-10-04 09:02:02 发表.Net

本文仅记录一些简单的使用方法,供初学者参考。[from:http://www.rainsts.net/article.asp?id=313]

被阅1225次, 0票Lucene.Net 发表评论

Lucene.Net简单应用

Admin 于 2008-10-01 02:24:25 发表.Net

Lucene.Net是一个性能很好的信息检索库(或者叫搜索引擎内核)。Lucene.Net提供一套完整的API供应用程序实现全文索引与精确查询功能。这里只描述最基本的应用:

 1、建立索引

  1. //state the file location of the index 
  2. string indexFileLocation = @"C:\Index";  
  3. Lucene.Net.Store.Directory dir = 
  4.     Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation, true); 
  5.  
  6. //create an analyzer to process the text 
  7. Lucene.Net.Analysis.Analyzer analyzer = new 
  8. Lucene.Net.Analysis.Standard.StandardAnalyzer();  
  9.  
  10. //create the index writer with the directory and analyzer defined. 
  11. Lucene.Net.Index.IndexWriter indexWriter = new 
  12. Lucene.Net.Index.IndexWriter(dir, analyzer,  
  13.            /*true to create a new index*/ true);  
  14.  
  15. //create a document, add in a single field 
  16. Lucene.Net.Documents.Document doc = new 
  17. Lucene.Net.Documents.Document(); 
  18.  
  19. Lucene.Net.Documents.Field fldContent =  
  20.   new Lucene.Net.Documents.Field("content",  
  21.   "The quick brown fox jumps over the lazy dog"
  22.   Lucene.Net.Documents.Field.Store.YES,  
  23.   Lucene.Net.Documents.Field.Index.TOKENIZED,  
  24.   Lucene.Net.Documents.Field.TermVector.YES); 
  25.  
  26. doc.Add(fldContent); 
  27.  
  28. //write the document to the index 
  29. indexWriter.AddDocument(doc); 
  30.  
  31. //optimize and close the writer 
  32. indexWriter.Optimize();  
  33. indexWriter.Close(); 

 这里用到了5个主要类:Directory、Analyzer、IndexWriter、Document和Field。通过Directory 实例记录需要存储的索引;Analyzer用于分析目标文本;结合Directory与Analyzer创建IndexWriter实例;结合 Document实例与Field实例保存文档要被IndexWriter存储的内容。然后调用IndexWriter的Optimize方法与 Close方法。
    1、Lucene.Net.Store.Directory  - Directory代表目标索引目录。Lucene.Net中包含了两种实现:FSDirectory与RAMDirectory,分别将索引结果存放于 文件系统与内存。开发人员也可以通过继承Directory来实现自己的存储方法。
    2、Lucene.Net.Analysis.Analyzer - Analyzer用于将文本分割成词语或词组,同时可去除多余的词语(比如英语里的and、a、the等等)。StandardAnalyzer实例化时 可传递要忽略的词语列表,如果传递的参数为空则使用Lucene.Net默认的设置。开发人员可通过继承Analyzer实现自己的文档索引方式。
    3、Lucene.Net.Index.IndexWriter - 用于配合Analyzer将分析结果保存到Directory。
    4、Lucene.Net.Docuemnts.Docuemnt - 用于代表一封EMail、或者一个网页、一篇文章等。
    5、Lucene.Net.Documents.Field - Docement包含一个用于描述文档的Field列表。每个Field包括一个name与相应的value,其中value包含了可用于查询的文本。 Field.Store用于告诉IndexWriter将field的value保存到索引以便检索时取出。此外,Field.Index用于告诉 IndexWriter如何索引当前field,比如Field.Index.TOKENIZED表示将文本拆 分,Field.Index.UNTOKENIZED则相反。

2、在索引中进行查找

  1. //state the file location of the index 
  2. string indexFileLocation = @"C:\Index"
  3. Lucene.Net.Store.Directory dir = 
  4.     Lucene.Net.Store.FSDirectory.GetDirectory(indexFileLocation, true); 
  5.  
  6. //create an index searcher that will perform the search 
  7. Lucene.Net.Search.IndexSearcher searcher = new 
  8. Lucene.Net.Search.IndexSearcher(dir); 
  9.  
  10. //build a query object 
  11. Lucene.Net.Index.Term searchTerm =  
  12.   new Lucene.Net.Index.Term("content""fox"); 
  13. Lucene.Net.Search.Query query = new Lucene.Net.Search.TermQuery(searchTerm); 
  14.  
  15. //execute the query 
  16. Lucene.Net.Search.Hits hits = searcher.Search(query); 
  17.  
  18. //iterate over the results. 
  19. for (int i = 0; i < hits.Length(); i++) 
  20.     Document doc = hits.Doc(i); 
  21.     string contentValue = doc.Get("content"); 
  22.  
  23.     Console.WriteLine(contentValue); 

 涉及的类有:Directory、IndexSearch、Query、Hits、Term。其中Query有多种实现:TermQuery, BooleanQuery, PhraseQuery, PrefixQuery, PhrasePrefixQuery, RangeQuery, FilteredQuery, 及 SpanQuery。Lucene.Net.Search.Hits用于表示查找的结果。

被阅1339次, 0票Lucene.Net 发表评论
1 / 1 / 2 | « 1 » |
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号