apache

发布日期  发布: 2009-2-17 | 发布人  发布者: 游子 | 来源  来源: 江西广告网


就像每个程序都有一个Hello World来让人体验它一样,lucene也可以很简单的提供一个实例。如下(来自lucene in action的例子)有两个类组成: 一个是建立索引 import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Date; 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; public class Indexer { public static void main(String[] args) throws Exception { if (args.length != 2) { throw new Exception("Usage: java " Indexer.class.getName() " <index dir> <data dir>"); } File indexDir = new File(args[0]); File dataDir = new File(args[1]); long start = new Date().getTime(); int numIndexed = index(indexDir, dataDir); long end = new Date().getTime(); System.out.println("Indexing " numIndexed " files took " (end - start) " milliseconds"); } // open an index and start file directory traversal public static int index(File indexDir, File dataDir) throws IOException { if (!dataDir.exists() || !dataDir.isDirectory()) { throw new IOException(dataDir " does not exist or is not a directory"); } IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true); writer.setUseCompoundFile(false); indexDirectory(writer, dataDir); int numIndexed = writer.docCount(); writer.optimize(); writer.close(); return numIndexed; } // recursive method that calls itself when it finds a directory [1] [2] [3] [4] private static void indexDirectory(IndexWriter writer, File dir) throws IOException { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i ) { File f = files; if (f.isDirectory()) { indexDirectory(writer, f); } else if (f.getName().endsWith(".txt")) { indexFile(writer, f); } } } // method to actually index file using Lucene private static void indexFile(IndexWriter writer, File f) throws IOException { if (f.isHidden() || !f.exists() || !f.canRead()) { return; } System.out.println("Indexing " f.getCanonicalPath()); Document doc = new Document(); doc.add(Field.Text("contents", new FileReader(f))); doc.add(Field.Keyword("filename", f.getCanonicalPath())); writer.addDocument(doc); } } import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Date; 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; public class Indexer { public static void main(String[] args) throws Exception { if (args.length != 2) { throw new Exception("Usage: java " Indexer.class.getName() " <index dir> <data dir>"); } File indexDir = new File(args[0]); File dataDir = new File(args[1]); long start = new Date().getTime(); int numIndexed = index(indexDir, dataDir); long end = new Date().getTime(); System.out.println("Indexing " numIndexed " files took " (end - start) " milliseconds"); } // open an index and start file directory traversal public static int index(File indexDir, File dataDir) throws IOException { if (!dataDir.exists() || !dataDir.isDirectory()) { throw new IOException(dataDir " does not exist or is not a directory"); } IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true); writer.setUseCompoundFile(false); indexDirectory(writer, dataDir); int numIndexed = writer.docCount(); writer.optimize(); writer.close(); return numIndexed; } // recursive method that calls itself when it finds a directory private static void indexDirectory(IndexWriter writer, File dir) throws IOException { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i ) { File f = files; if (f.isDirectory()) { indexDirectory(writer, f); } else if (f.getName().endsWith(".txt")) { indexFile(writer, f); } } } 上一页 [1] [2] [3] [4] // method to actually index file using Lucene private static void indexFile(IndexWriter writer, File f) throws IOException { if (f.isHidden() || !f.exists() || !f.canRead()) { return; } System.out.println("Indexing " f.getCanonicalPath()); Document doc = new Document(); doc.add(Field.Text("contents", new FileReader(f))); doc.add(Field.Keyword("filename", f.getCanonicalPath())); writer.addDocument(doc); } } 另一个是搜索: view plaincopy to clipboardprint? import java.io.File; import java.util.Date; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class Searcher { public static void main(String[] args) throws Exception { if (args.length != 2) { throw new Exception("Usage: java " Searcher.class.getName() " <index dir> <auery>"); } File indexDir = new File(args[0]); String q = args[1]; if (!indexDir.exists() || !indexDir.isDirectory()) { throw new Exception(indexDir " does not exist or is not a directory."); } search(indexDir, q); } public static void search(File indexDir, String q) throws Exception { Directory fsDir = FSDirectory.getDirectory(indexDir, false); IndexSearcher is = new IndexSearcher(fsDir); Query query = QueryParser.parse(q, "contents", new StandardAnalyzer()); long start = new Date().getTime(); Hits hits = is.search(query); long end = new Date().getTime(); System.err.println("Found " hits.length() " document(s) (in " (end - start) " milliseconds) that matched query ‘" q "’:"); for (int i = 0; i < hits.length(); i ) { Document doc = hits.doc(i); System.out.println(doc.get("filename")); } } } import java.io.File; import java.util.Date; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class Searcher { 上一页 [1] [2] [3] [4] public static void main(String[] args) throws Exception { if (args.length != 2) { throw new Exception("Usage: java " Searcher.class.getName() " <index dir> <auery>"); } File indexDir = new File(args[0]); String q = args[1]; if (!indexDir.exists() || !indexDir.isDirectory()) { throw new Exception(indexDir " does not exist or is not a directory."); } search(indexDir, q); } public static void search(File indexDir, String q) throws Exception { Directory fsDir = FSDirectory.getDirectory(indexDir, false); IndexSearcher is = new IndexSearcher(fsDir); Query query = QueryParser.parse(q, "contents", new StandardAnalyzer()); long start = new Date().getTime(); Hits hits = is.search(query); long end = new Date().getTime(); System.err.println("Found " hits.length() " document(s) (in " (end - start) " milliseconds) that matched query ‘" q "’:"); for (int i = 0; i < hits.length(); i ) { Document doc = hits.doc(i); System.out.println(doc.get("filename")); } } } ok,这样就简单实现了,在搜索目录下所有txt,找出包括某一个字符串的txt文件名的功能。 上一页 [1] [2] [3] [4]
本站文章部分内容来自互联网,供读者交流和学习,如有涉及作者版权问题请及时与我们联系,以便更正或删除。感谢所有提供信息的网站,并欢迎各类媒体与我们进行信息共享合作。
关闭本窗口 | 打印 | 收藏此页 |  推荐给好友 | 举报

版块排行

  • SEO搜索                                    5984
  • Web软件                                    3334
  • 交互设计                                    3279
  • 平面软件                                    2575
  • 设计欣赏                                    2501
  • 游戏世界                                    1244
  • 程序开发                                    830
  • 前沿视觉                                    560
  • 电脑网络                                    514
  • 摄影赏析                                    291