/* * This file is part of Documancer (http://documancer.sf.net) * * Copyright (C) 2005 Vaclav Slavik * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: IndexerService.java,v 1.1 2005/02/05 10:38:15 vaclavslavik Exp $ * * Fulltext indexer using Lucene * */ import java.util.Vector; import java.util.Hashtable; import java.util.Enumeration; import java.io.IOException; import org.apache.xmlrpc.WebServer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.*; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.*; import org.apache.lucene.search.*; import org.apache.lucene.store.FSDirectory; /** Implementation of Lucene fulltext indexing for Documancer. */ public class IndexerService { private static final String LUCENE_VERSION = "1.4.3"; public IndexerService(WebServer server) { m_server = server; } /// Stops the server. public boolean kill() { m_server.shutdown(); return true; } /// Returns server status (evrything except "OK" means failure). public String getStatus() { return "OK"; } /// Returns human readable version of the indexer public String getNameAndVersion() { return "Lucene " + LUCENE_VERSION + " using " + System.getProperty("java.vm.name") + " " + System.getProperty("java.vm.version"); } /// Search index in given directory public Vector search(String directory, String query) throws IOException, ParseException { FSDirectory dir = FSDirectory.getDirectory(directory, false); StandardAnalyzer analyzer = new StandardAnalyzer(); Query queryobj = QueryParser.parse(query, "contents", analyzer); IndexSearcher searcher = new IndexSearcher(dir); Hits hits = searcher.search(queryobj); Vector results = new Vector(); for (int i = 0; i < hits.length(); i++) { Document doc = hits.doc(i); Hashtable h = new Hashtable(); h.put("title", doc.get("title")); h.put("url", doc.get("url")); h.put("score", new Float(hits.score(i))); results.add(h); } return results; } /// Starts indexing, creates new index in given directory public boolean startIndexing(String directory) throws IOException { FSDirectory store = FSDirectory.getDirectory(directory, true); m_writer = new IndexWriter(store, new StandardAnalyzer(), true); return true; } /// Indexes single document; startIndexing must be called before public boolean indexDocument(String url, Hashtable data) throws IOException { Document doc = new Document(); doc.add(new Field("url", url, true, true, true)); for (Enumeration e = data.keys(); e.hasMoreElements() ;) { String field = e.nextElement().toString(); doc.add(new Field(field, data.get(field).toString(), true, true, true)); } m_writer.addDocument(doc); return true; } /// Stops indexing and writes out finished index public boolean stopIndexing() throws IOException { m_writer.optimize(); m_writer.close(); m_writer = null; return true; } // active index writer for indexDocument and friends: private IndexWriter m_writer; // our XML-RPC server: private WebServer m_server; }