发布于 2015-02-01 02:13:22 | 1866 次阅读 | 评论: 0 | 来源: PHPERZ

这里有新鲜出炉的Lucene教程,程序狗速度看过来!

Apache Lucene全文检索引擎工具包

Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎。


一,Lucene 简介

Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言)。Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎。

Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能。Lucene 目前是 Apache Jakarta 家族中的一个开源项目。也是目前最为流行的基于 Java 开源全文检索工具包。
目前已经有很多应用程序的搜索功能是基于 Lucene 的,比如 Eclipse 的帮助系统的搜索功能。Lucene 能够为文本类型的数据建立索引,所以你只要能把你要索引的数据格式转化的文本的,Lucene 就能对你的文档进行索引和搜索。比如你要对一些 HTML 文档,PDF 文档进行索引的话你就首先需要把 HTML 文档和 PDF 文档转化成文本格式的,然后将转化后的内容交给 Lucene 进行索引,然后把创建好的索引文件保存到磁盘或者内存中,最后根据用户输入的查询条件在索引文件上进行查询。不指定要索引的文档的格式也使 Lucene 能够几乎适用于所有的搜索应用程序。

我们先看看官方的架构图:

          

二,官网demo

1、首先到官网下载lucene-4.10.3.zip

官方网址:http://lucene.apache.org/

2、解压zip包,其中有一个demo有一个是lucene-xml-query-demo.war可以放到tomcat 安装目录的webapps中

3、将tomcat服务器开启输入localhost:8080/lucene- xml-query-demo将会出现界面但是点击查询会报 java.lang.ClassNotFoundException:org.apache.lucene.xmlparser.webdemo.FormBasedXmlQueryDemo 这个错误。这个原因是新版本中FormBasedXmlQueryDemo的路径

变了,这时你就需要到该项目的web.xml中将 <servlet-class> org.apache.lucene.xmlparser.webdemo.FormBasedXmlQueryDemo </servlet-class>

更改为 <servlet-class>org.apache.lucene.demo.xmlparser.FormBasedXmlQueryDemo</servlet-class>

然后把lucene-4.1.0解压包下 analysis\common\lucene-analyzers-common-4.10.2.jar和 sandbox\lucene-sandbox-4.10.2.jar

这两个文件拷贝到WEB-INF\lib文件夹下面,这时在点击查询就不会出现问题了输入java查询结果如下

三、简单实现

lucene基本工作原理简单可以理解为创建索引,而根据索引查询

下面是简单的例子

TxtFileInderxer作用是将D:/luceneData中所有的.txt文件建立索引并将所有的索引存放在D:/luceneIndex中

package org.com.test;  
  
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.Store;  
import org.apache.lucene.document.TextField;  
import org.apache.lucene.index.IndexWriter;  
import org.apache.lucene.index.IndexWriterConfig;  
import org.apache.lucene.store.FSDirectory;  
import org.apache.lucene.util.Version;  
  
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(Version.LUCENE_4_10_2);  
        // 对文档进行分词  
        Analyzer luceneAnalyzer = new StandardAnalyzer();  
        File[] dataFiles = dataDir.listFiles();  
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(  
                Version.LUCENE_4_10_3, luceneAnalyzer);  
        // 创建索引  
        IndexWriter indexWriter = new IndexWriter(FSDirectory.open(indexDir),  
                indexWriterConfig);  
        // 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 document = new Document();  
                Reader txtReader = new FileReader(dataFiles[i]);  
                document.add(new TextField("path", dataFiles[i]  
                        .getCanonicalPath(), Store.YES));  
                // document.add(Field.Text("contents", txtReader));  
                document.add(new TextField("contents", txtReader));  
                indexWriter.addDocument(document);  
            }  
        }  
        indexWriter.commit();  
        // 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());  
    }  
  
} 

TxtFileSearcher作用是从D:/luceneIndex中读取索引并查询.txt文件中含有lucene的文件

package org.com.test;  
  
import java.io.File;  
import org.apache.lucene.document.Document;  
import org.apache.lucene.index.IndexReader;  
import org.apache.lucene.index.Term;  
import org.apache.lucene.search.IndexSearcher;  
import org.apache.lucene.search.ScoreDoc;  
import org.apache.lucene.search.TermQuery;  
import org.apache.lucene.search.TopDocs;  
import org.apache.lucene.store.Directory;  
import org.apache.lucene.store.FSDirectory;  
  
public class TxtFileSearcher {  
    public static void main(String[] args) throws Exception {  
        String queryStr = "java";  
        // This is the directory that hosts the Lucene index  
        File indexDir = new File("D:\\luceneIndex");  
        Directory directory = FSDirectory.open(indexDir);  
        // FSDirectory fsDirectory = FSDirectory.open(indexDir);  
        IndexReader indexReader = IndexReader.open(directory);  
        // FSDirectory directory = FSDirectory.getDirectory(indexDir,false);  
        // IndexReader indexReader = IndexReader.open(fsDirectory);  
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);  
        // IndexSearcher searcher = new IndexSearcher(indexReader);  
        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);  
        TopDocs topDocs = indexSearcher.search(luceneQuery, 1000);  
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;  
        if (scoreDocs == null || scoreDocs.length == 0) {  
            System.out.println("The  Lucene index is not exist");  
        }  
        for (int i = 0; i < scoreDocs.length; i++) {  
            Document document = indexSearcher.doc(scoreDocs[i].doc);  
            System.out.println("File: " + document.get("path"));  
        }  
        indexReader.close();  
    }  
}  

执行结果如下:

以"D:\\luceneData"为数据源建立索引:

按“java”为关键字搜索结果:

你正在学习使用lucene吗,这里有新鲜出炉的lucene教程



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务