基于lucene的全文检索

认识lucene
Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能。Lucene 目前是 Apache Jakarta 家族中的一个开源项目。也是目前最为流行的基于 Java 开源全文检索工具包。

利用lucene构建搜索程序
1、建立索引
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{
         /*存放索引文件的目录*/
         File   indexDir = new File("D:
    luceneIndex");
         /*该目录为需要产生索引的txt文件目录*/
         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());       
         }
    }

   简要说明:类 IndexWriter 的构造函数需要三个参数,第一个参数指定了所创建的索引要存放的位置,他可以是一个 File 对象,也可以是一个 FSDirectory 对象或者 RAMDirectory 对象。第二个参数指定了 Analyzer 类的一个实现,也就是指定这个索引是用哪个分词器对文挡内容进行分词。第三个参数是一个布尔型的变量,如果为 true 的话就代表创建一个新的索引,为 false 的话就代表在原来索引的基础上进行操作。接着程序遍历了目录下面的所有文本文档,并为每一个文本文档创建了一个 Document 对象。然后把文本文档的两个属性:路径和内容加入到了两个 Field 对象中,接着在把这两个 Field 对象加入到 Document 对象中,最后把这个文档用 IndexWriter 类的 add 方法加入到索引中去。这样我们便完成了索引的创建。接下来我们进入在建立好的索引上进行搜索的部分。

2、在建立好的索引上进行搜索
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";
    /*此处为存放索引的目录*/
            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.docinformation;
              System.out.println("File: " + document.get("path"));
            }
         }
    }

    简要说明:类 IndexSearcher 的构造函数接受一个类型为 Directory 的对象,Directory 是一个抽象类,它目前有两个子类:FSDirctory 和 RAMDirectory. 我们的程序中传入了一个 FSDirctory 对象作为其参数,代表了一个存储在磁盘上的索引的位置。构造函数执行完成后,代表了这个 IndexSearcher 以只读的方式打开了一个索引。然后我们程序构造了一个 Term 对象,通过这个 Term 对象,我们指定了要在文档的内容中搜索包含关键词”lucene”的文档。接着利用这个 Term 对象构造出 TermQuery 对象并把这个 TermQuery 对象传入到 IndexSearcher 的 search 方法中进行查询,返回的结果保存在 Hits 对象中。最后我们用了一个循环语句把搜索到的文档的路径都打印了出来。

至此我们就创建了一个简单的搜索应用程序了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值