原文地址點擊打開鏈接
本文爲大家分享的是一篇 lucene4.10.3的入門教程,lucene是一個開放源代碼的全文檢索引擎工具包,出自apache基金會。感興趣的同學參考下
一,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”爲關鍵字搜索結果: