認識Lucene
Lucene是一款高性能的、可擴展的信息檢索(IR)工具庫。它是一款以JAVA實現的成熟、自由、開源的軟件項目,是Apache軟件基金會的一個項目,並且基於Apache軟件許可協議授權。
1.Lucene能做什麼?
Lucene只是一個軟件類庫,可以理解爲是一個工具箱,而並不是一套完整的搜索程序,當然如果你找一個比Lucene封裝的更完整的搜索程序,可以去了解Lucene姊妹開源軟件solr。Lucen只專注於建立索引和搜索功能,Lucene幫你實現了較爲複雜的索引建立過程和搜索過程,使用者只需要調用Lucene封裝好的API,即可以是自己的程序立刻擁有搜索功能。此外Lucene也不關心原始內容的獲取,這個需要使用者自己想辦法解決,再次強調一下,Lucene只關心建立索引和實現搜索這兩個部分。如果大家對爬取內容和網絡爬蟲感興趣,可以研究一下最後一節“附加”部分提到的開源軟件。
2.Lucene的歷史
- Lucene最初是由Doug Cutting編寫的,開源在SourceForge。
- 2001年9月,Lucene加入Apache軟件基金會的Jakarta家族,Jakarta大家應該很眼熟吧,想想經典的JAVA web容器Tomcat。
- 2005年該項目一躍成爲頂級的Apache項目。
- 2010年,Lucene的里程碑版本3.0面世。
- 至今爲止,Lucene已布了7.x版本。
3.Lucene在搜索程序中擔任的角色
Lucene只負責索引和搜索兩個部分,其他的如:文本的獲取,則由使用者自己解決。
4.建立索引的過程
建立索引的過程如下:
5.搜索索引的過程
搜索的過程如下:
6.實戰
- 準備文本內容
- 編寫索引程序
package com.lucene._1_1;
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;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import java.io.*;
import java.lang.annotation.Documented;
/**
* Created by xun.zhang on 2017/10/26.
*/
public class Indexer {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
throw new IllegalArgumentException("Usage: Java " + Indexer.class.getName() + " <index dir> <data dir>");
}
String indexDir = args[0];
String dataDir = args[1];
long start = System.currentTimeMillis();
Indexer indexer = new Indexer(indexDir);
int numIndexed;
try{
numIndexed = indexer.index(dataDir, new TextFilesFilter());
}finally {
indexer.close();
}
}
private IndexWriter writer;
public Indexer(String indexerDir) throws IOException {
Directory dir = FSDirectory.open(new File(indexerDir));
writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED);
}
public void close() throws IOException {
writer.close();
}
public int index(String dataDir, FileFilter filter) throws IOException {
File[] files = new File(dataDir).listFiles();
for (File f : files) {
if(!f.isDirectory() && !f.isHidden() && f.exists() && f.canRead() && (filter == null || filter.accept(f))) {
indexFile(f);
}
}
return writer.numDocs();
}
private static class TextFilesFilter implements FileFilter {
public boolean accept(File path) {
return path.getName().toLowerCase().endsWith(".txt");
}
}
protected Document getDocument(File f) throws IOException {
Document doc = new Document();
doc.add(new Field("contents", new FileReader(f)));
doc.add(new Field("filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("fullpath", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
return doc;
}
private void indexFile(File f) throws IOException {
System.out.println("Indexing " + f.getCanonicalPath());
Document doc = getDocument(f);
writer.addDocument(doc);
}
}
- 索引核心類及流程
- 建立搜索程序
package com.lucene._1_1;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import java.io.File;
import java.io.IOException;
/**
* Created by xun.zhang on 2017/10/26.
*/
public class Searcher {
public static void main(String[] args) throws IOException, ParseException {
if (args.length != 2) {
throw new IllegalArgumentException("Usage: Java " + Indexer.class.getName() + " <index dir> <data dir>");
}
String indexDir = args[0];
String q = args[1];
search(indexDir, q);
}
public static void search(String indexDir, String q) throws IOException, ParseException {
Directory dir = FSDirectory.open(new File(indexDir));
IndexSearcher is = new IndexSearcher(dir);
QueryParser parser = new QueryParser(Version.LUCENE_30, "contents", new StandardAnalyzer(Version.LUCENE_30));
Query query = parser.parse(q);
// System.out.println(query.getClass());
long start = System.currentTimeMillis();
TopDocs hits = is.search(query, 10);
long end = System.currentTimeMillis();
System.err.println("Found " + hits.totalHits + " document(s) (in " + (end - start) + " milliseconds) that matched query '" + q + "':");
for (ScoreDoc scoreDoc:hits.scoreDocs
) {
Document doc = is.doc(scoreDoc.doc);
System.out.println(doc.get("fullpath"));
}
}
}
- 搜索核心類及流程
- 常見的搜索理論模型
- 純布爾模型(Pure Boolean model),文檔不管是否匹配查詢請求,都不會被評分,在該模型下,匹配文檔與評分無關,也無序。一條查詢語句僅獲取所有匹配文檔集合的一個子集。
- 向量空間模型(Vector space model),查詢語句和文檔都是高維空間的向量模型,每一個獨立的項都是一個維度。查詢語句和文檔之間的相關性和相似性由各自的向量之間的距離計算獲得。
- 概率模型(Probabilistic model),在該模型中,採用全概率方法來計算文檔和查詢語句的匹配概率。
Lucene在實現上使用了向量空間模型和純布爾模型,並能針對具體搜索採用什麼模型。
7.附加:目前開源爬蟲軟件
- Solr,Apache Lucene項目的子項目,支持從關係數據庫和xml文檔中提取原始數據,以及能夠通過集成Tika來處理複雜文檔。
- Nutch,另外一個Apache Lucene的子項目,它包含大規模的爬蟲工具,能夠抓取和分辨Web站點數據。
- Grub,比較流行的開源Web爬蟲工具。
- Heritrix,是一款開源的Internet文檔搜索程序。
- Aperture,他支持從Web站點、文件系統和郵箱中抓取,並解析和索引其中的文本數據。
- 谷歌企業連接管理工程,提供大量針對非Web形式的內容連接方案。
參考:
Lucene實戰(第2版)著:Michael McCandles、Erik Hatcher、Otis Gospodnetic 譯:牛長流、肖宇