学习笔记:实战 Lucene,第 1 部分: 初识 Lucene

Lucene 简介

  Lucene 是一个基于 Java 的全文信息检索工具包,
  它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能。

现有的基于Lucene的搜索功能:
  比如 Eclipse 的帮助系统的搜索功能
  许多电商网站中的产品分类信息查询

Lucene 能够为文本类型的数据建立索引,
  所以你只要能把你要索引的数据格式转化的文本的,Lucene 就能对你的文档进行索引和搜索。
  不指定要索引的文档的格式也使 Lucene 能够几乎适用于所有的搜索应用程序。

索引和搜索

  Lucene 软件包分析

    Package: org.apache.lucene.document
      这个包提供了一些为封装要索引的文档所需要的类,比如 Document, Field。
      这样,每一个文档最终被封装成了一个 Document 对象。

    Package: org.apache.lucene.analysis
      这个包主要功能是对文档进行分词,因为文档在建立索引之前必须要进行分词,
      所以这个包的作用可以看成是为建立索引做准备工作。

    Package: org.apache.lucene.index
      这个包提供了一些类来协助创建索引以及对创建好的索引进行更新。
      这里面有两个基础的类:IndexWriter 和 IndexReader,
      其中 IndexWriter 是用来创建索引并添加文档到索引中的,IndexReader 是用来删除索引中的文档的。

    Package: org.apache.lucene.search
      这个包提供了对在建立好的索引上进行搜索所需要的类。比如 IndexSearcher 和 Hits,
      IndexSearcher 定义了在指定的索引上进行搜索的方法,Hits 用来保存搜索得到的结果。

一个简单的搜索应用程序

建立索引
  为了对文档进行索引,Lucene 提供了五个基础的类,他们分别是 Document, Field, IndexWriter, Analyzer, Directory。

  清单 1. 对文本文件建立索引

package TestLucene; 
import java.io.File; 
import java.io.FileReader; 
import java.io.Reader; 
import java.util.Date; 
import org.apache.lucene.analysis.Analyzer; 
import org.apache.lucene.analysis.standard.StandardAnalyzer; 
import org.apache.lucene.document.Document; 
import org.apache.lucene.document.Field; 
import org.apache.lucene.index.IndexWriter; 
/** 
* This class demonstrate the process of creating index with Lucene 
* for text files 
*/ 
public class TxtFileIndexer { 
     public static void main(String[] args) throws Exception{ 
     //indexDir is the directory that hosts Lucene's index files 
     File   indexDir = new File("D:\\luceneIndex"); 
     //dataDir is the directory that hosts the text files that to be indexed 
     File   dataDir  = new File("D:\\luceneData"); 
     Analyzer luceneAnalyzer = new StandardAnalyzer(); 
     File[] dataFiles  = dataDir.listFiles(); 
     IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true); 
     long startTime = new Date().getTime(); 
     for(int i = 0; i < dataFiles.length; i++){ 
          if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".txt")){
               System.out.println("Indexing file " + dataFiles[i].getCanonicalPath()); 
               Document document = new Document(); 
               Reader txtReader = new FileReader(dataFiles[i]); 
               document.add(Field.Text("path",dataFiles[i].getCanonicalPath())); 
               document.add(Field.Text("contents",txtReader)); 
               indexWriter.addDocument(document); 
          } 
     } 
     indexWriter.optimize(); 
     indexWriter.close(); 
     long endTime = new Date().getTime(); 
        
     System.out.println("It takes " + (endTime - startTime) 
         + " milliseconds to create index for the files in directory "
         + dataDir.getPath());        
     } 
} 

 

搜索文档
  现在我们就要在这个索引上进行搜索以找到包含某个关键词或短语的文档。
  Lucene 提供了几个基础的类来完成这个过程,它们分别是呢 IndexSearcher, Term, Query, TermQuery, Hits.

  清单 2 :在建立好的索引上进行搜索

 package TestLucene; 
 import java.io.File; 
 import org.apache.lucene.document.Document; 
 import org.apache.lucene.index.Term; 
 import org.apache.lucene.search.Hits; 
 import org.apache.lucene.search.IndexSearcher; 
 import org.apache.lucene.search.TermQuery; 
 import org.apache.lucene.store.FSDirectory; 
 /** 
 * This class is used to demonstrate the 
 * process of searching on an existing 
 * Lucene index 
 * 
 */ 
 public class TxtFileSearcher { 
     public static void main(String[] args) throws Exception{ 
        String queryStr = "lucene"; 
        //This is the directory that hosts the Lucene index 
        File indexDir = new File("D:\\luceneIndex"); 
        FSDirectory directory = FSDirectory.getDirectory(indexDir,false); 
        IndexSearcher searcher = new IndexSearcher(directory); 
        if(!indexDir.exists()){ 
             System.out.println("The Lucene index is not exist"); 
             return; 
        } 
        Term term = new Term("contents",queryStr.toLowerCase()); 
        TermQuery luceneQuery = new TermQuery(term); 
        Hits hits = searcher.search(luceneQuery); 
        for(int i = 0; i < hits.length(); i++){ 
             Document document = hits.doc(i); 
             System.out.println("File: " + document.get("path")); 
        } 
     } 
 }

 

参考
http://www.ibm.com/developerworks/cn/java/j-lo-lucene1/

posted @ 2012-07-22 10:04  万法自然~  阅读(139)  评论(0)    收藏  举报