標籤:
Indexer:
import org.apache.lucene.index.IndexWriter;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store.Directory;import org.apache.lucene.util.Version;import java.io.File;import java.io.FileFilter;import java.io.IOException;import java.io.FileReader;// From chapter 1/** * This code was originally written for * Erik‘s Lucene intro java.net article */public class Indexer { public static void main(String[] args) throws Exception { if (args.length != 2) { throw new IllegalArgumentException("Usage: java " + Indexer.class.getName() + " <index dir> <data dir>"); } String indexDir = args[0]; //1 String dataDir = args[1]; //2 long start = System.currentTimeMillis(); Indexer indexer = new Indexer(indexDir); int numIndexed; try { numIndexed = indexer.index(dataDir, new TextFilesFilter()); } finally { indexer.close(); } long end = System.currentTimeMillis(); System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds"); } private IndexWriter writer; public Indexer(String indexDir) throws IOException { Directory dir = FSDirectory.open(new File(indexDir)); writer = new IndexWriter(dir, //3 new StandardAnalyzer( //3 Version.LUCENE_30),//3 true, //3 IndexWriter.MaxFieldLength.UNLIMITED); //3 } public void close() throws IOException { writer.close(); //4 } public int index(String dataDir, FileFilter filter) throws Exception { 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(); //5 } private static class TextFilesFilter implements FileFilter { public boolean accept(File path) { return path.getName().toLowerCase() //6 .endsWith(".txt"); //6 } } protected Document getDocument(File f) throws Exception { Document doc = new Document(); doc.add(new Field("contents", new FileReader(f))); //7 doc.add(new Field("filename", f.getName(), //8 Field.Store.YES, Field.Index.NOT_ANALYZED));//8 doc.add(new Field("fullpath", f.getCanonicalPath(), //9 Field.Store.YES, Field.Index.NOT_ANALYZED));//9 return doc; } private void indexFile(File f) throws Exception { System.out.println("Indexing " + f.getCanonicalPath()); Document doc = getDocument(f); writer.addDocument(doc); //10 }}
索引過程核心類:
IndexWriter
負責建立或開啟已有索引,以及向索引中添加、刪除或更新被索引文檔資訊,一般要通過構造器傳入Directory和Analyzer
Directory
抽象類別,描述了索引的存放位置
Analyzer
負責從被索引文本中提取語彙單元,只能處理純文字檔案,如果不是純文字,需要先轉換(如使用Tika)
Document
Document對象代表一些Field的集合
Field
Lucene只處理從二進位文檔中提取的一Field形式出現的文本,文檔的中繼資料作為文檔的不同域單獨儲存並索引
題外話:Lucene核心本身只處理java.lang.String、java.io.Reader和本地數字類型(int、float等)
Searcher:
import org.apache.lucene.document.Document;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.FSDirectory;import org.apache.lucene.store.Directory;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.util.Version;import java.io.File;import java.io.IOException;// From chapter 1/** * This code was originally written for * Erik‘s Lucene intro java.net article */public class Searcher { public static void main(String[] args) throws IllegalArgumentException, IOException, ParseException { if (args.length != 2) { throw new IllegalArgumentException("Usage: java " + Searcher.class.getName() + " <index dir> <query>"); } String indexDir = args[0]; //1 String q = args[1]; //2 search(indexDir, q); } public static void search(String indexDir, String q) throws IOException, ParseException { Directory dir = FSDirectory.open(new File(indexDir)); //3 IndexSearcher is = new IndexSearcher(dir); //3 QueryParser parser = new QueryParser(Version.LUCENE_30, // 4 "contents", //4 new StandardAnalyzer( //4 Version.LUCENE_30)); //4 Query query = parser.parse(q); //4 long start = System.currentTimeMillis(); TopDocs hits = is.search(query, 10); //5 long end = System.currentTimeMillis(); System.err.println("Found " + hits.totalHits + //6 " document(s) (in " + (end - start) + // 6 " milliseconds) that matched query ‘" + // 6 q + "‘:"); // 6 for(ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); //7 System.out.println(doc.get("fullpath")); //8 } is.close(); //9 }}
搜尋過程核心類:
IndexSearcher
用於搜尋由IndexWriter建立的索引,構造器需要傳入Directory擷取建立的索引。然後提供搜尋方法
Term
Term對象是搜尋的基本單元(與Field類似)
Query q = new TermQuery(new Term("contents","lucene"));TopDocs hits = searcher.search(q,10);
Query
Query是所有查詢類的基類,如TermQuery、BooleanQuery
TermQuery
TermQuery是最基本最簡單的查詢類型之一,用於匹配指定域中包含指定項的文檔
TopDocs
是一個簡單的指標容器,容納查詢結果
湯能養身整理,轉載註明
《Lucene In Action》 02 Hello Lucene World