自己写的Lucene实例
建立索引
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 = "F:\\index"; //1 String dataDir = "F:\\"; //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 IKAnalyzer() ,//3 true, //3 IndexWriter.MaxFieldLength.LIMITED); //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("content-value", 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 } }
查询
public class Index { public static void main(String[] args) throws IllegalArgumentException, IOException, ParseException { String indexDir = "F:\\index"; String q="徐剑锋"; 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); is.setSimilarity(new IKSimilarity()); QueryParser parser = new QueryParser(Version.LUCENE_30, "content-value", new IKAnalyzer()); Query query = IKQueryParser.parse("content-value", q); // Query query=parser.parse(q); long start = System.currentTimeMillis(); TopDocs hits = is.search(query, 10); long end = System.currentTimeMillis(); System.out.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")); } is.close(); } }