/************************************************************************** /* This class implements a configurable Lucene-search engine. /* /* 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.lib; import java.io.*; import java.util.*; import org.apache.lucene.document.*; import org.apache.lucene.store.*; import org.apache.lucene.search.*; import org.apache.lucene.queryParser.*; import org.apache.lucene.analysis.*; import org.apache.lucene.analysis.de.*; import org.apache.lucene.analysis.ru.*; import org.apache.lucene.analysis.standard.*; import de.bablokb.luala.lib.*; /** This class implements a configurable Lucene-search engine. @version $Revision: 1.10 $ @author $Author: bablokb $ */ public class SearchEngine { //////////////////////////////////////////////////////////////////////////// /** The index-directory. If this directory is not-writable, we will disable locks. */ private String iIndexDir; //////////////////////////////////////////////////////////////////////////// /** The default field for the query. This defaults to {@link StandardDocumentFactory#CONTENT}. */ private String iDefaultField; //////////////////////////////////////////////////////////////////////////// /** The analyzer for the search. This should be the same analyzer as for indexing. */ private Analyzer iAnalyzer; //////////////////////////////////////////////////////////////////////////// /** Default-Constructor. Creates a default analyzer based on the current default locale. */ public SearchEngine() { String currentCountry = Locale.getDefault().getCountry(); if (currentCountry.equals("DE")) iAnalyzer = new GermanAnalyzer(); // else if (currentCountry.equals("RU")) // iAnalyzer = new RussianAnalyzer(???); else iAnalyzer = new StandardAnalyzer(); iDefaultField = StandardDocumentFactory.CONTENT; } //////////////////////////////////////////////////////////////////////////// /** Construct a SearchEngine with given directory. @param dir The directory with the index. @throws FileNotFoundException if the directory does not exist @throws IllegalArgumentException if the argument is not a directory */ public SearchEngine(String dir) throws FileNotFoundException, IllegalArgumentException { this(); setIndexDirectory(dir); } //////////////////////////////////////////////////////////////////////////// /** Sets the index directory. @param dir The directory with the index. @throws FileNotFoundException if the directory does not exist @throws IllegalArgumentException if the argument is not a directory */ public void setIndexDirectory(String dir) throws FileNotFoundException, IllegalArgumentException { File d = new File(dir); if (!d.exists()) throw new FileNotFoundException("could not find directory: " + dir); if (!d.isDirectory()) throw new IllegalArgumentException("not a directory: " + dir); iIndexDir = dir; } //////////////////////////////////////////////////////////////////////////// /** Sets the analyzer. @param a The analyzer to use. */ public void setAnalyzer(Analyzer a) { iAnalyzer = a; } //////////////////////////////////////////////////////////////////////////// /** Sets the default fields for queries. @param name The name of the default field. */ public void setField(String name) { iDefaultField = name; } //////////////////////////////////////////////////////////////////////////// /** Search the index and return the results. @param queryString The query to perform @return An array with all documents matching the query @throws IOException @throws ParseException */ public Document[] search(String queryString) throws IOException, org.apache.lucene.queryParser.ParseException { // Directory dir = RODirectory.getDirectory(iIndexDir); String dir = iIndexDir; Searcher searcher = new IndexSearcher(dir); Query query = QueryParser.parse(queryString,iDefaultField,iAnalyzer); Hits hits = searcher.search(query); Document[] results = new Document[hits.length()]; for (int i = 0; i