/************************************************************************** /* Prototyping class for searching. This is a modified version of /* src/demo/org/apache/lucene/demo/SearchFiles.java from the Lucene /* distribution. See COPYING.lucene in the toplevel directory for /* copyright information about Lucene. /* /* Copyright (c) 2003-2004 by Bernhard Bablok (mail@bablokb.de) /* /* This library is free software; you can redistribute it and/or modify /* it under the terms of the GNU Lesser General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This library is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU Lesser General Public License for more details. /* /* You should have received a copy of the GNU Lesser General Public License /* along with this library; see the file COPYING.LESSER. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.luala.prototype; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.lucene.store.Directory; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.Hits; import org.apache.lucene.queryParser.QueryParser; /** Prototyping class for searching. @version $Revision: 1.7 $ @author $Author: bablokb $ */ public class SearchFiles { private Searcher iSearcher; //////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { if (args.length != 2) { System.out.println("usage: java " + SearchFiles.class.getName() + " indexDir query"); System.exit(1); } try { SearchFiles sf = new SearchFiles(); Hits hits = sf.getHits(args[0],args[1]); System.out.println(hits.length() + " total matching documents"); sf.dumpHits(hits); sf.cleanup(); } catch (Exception e) { e.printStackTrace(); } } //////////////////////////////////////////////////////////////////////////// public Hits getHits(String indexDir, String queryString) throws Exception { iSearcher = new IndexSearcher(indexDir); Analyzer analyzer = new StandardAnalyzer(); Query query = QueryParser.parse(queryString, "content", analyzer); System.out.println("Searching for: " + query.toString("content")); Hits hits = iSearcher.search(query); return hits; } //////////////////////////////////////////////////////////////////////////// public void dumpHits(Hits hits) throws Exception { for (int i = 0; i < hits.length(); i++) { Document doc = hits.doc(i); String path = doc.get("path"); if (path != null) { System.out.println(i + ". " + path); } else { String url = doc.get("url"); if (url != null) { System.out.println(i + ". " + url); System.out.println(" - " + doc.get("title")); } else { System.out.println(i + ". " + "No path nor URL for this document"); } } } } //////////////////////////////////////////////////////////////////////////// public void cleanup() throws Exception { iSearcher.close(); } }