001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portlet.search;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.InputStreamReader;
024: import java.util.Vector;
025:
026: import javax.portlet.ActionRequest;
027: import javax.portlet.ActionResponse;
028: import javax.portlet.PortletException;
029:
030: import org.apache.lucene.analysis.Analyzer;
031: import org.apache.lucene.analysis.standard.StandardAnalyzer;
032: import org.apache.lucene.document.Document;
033: import org.apache.lucene.queryParser.QueryParser;
034: import org.apache.lucene.search.Hits;
035: import org.apache.lucene.search.IndexSearcher;
036: import org.apache.lucene.search.Query;
037: import org.apache.lucene.search.Searcher;
038: import org.w3c.dom.Element;
039:
040: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
041: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
042: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
043: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
044:
045: /**
046: *
047: *
048: * @author Padmanabh Dabke
049: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
050: */
051: public class Search extends BaseRequestProcessor implements
052: ActionProcessor {
053: String sIndexDir = "C:/temp/index";
054:
055: public void init(Element xmlConfig, ControllerPortletConfig config)
056: throws PortletException {
057: super .init(xmlConfig, config);
058: sIndexDir = config.getParameter("index-dir");
059: if (sIndexDir == null) {
060: throw new PortletException(
061: "Missing required global parameter: index-dir");
062: }
063: sIndexDir = getRealPath(config.getPortletContext(), sIndexDir);
064: }
065:
066: /**
067: * @param request
068: * @param response
069: * @param actionConfig
070: * @return @throws
071: * PortletException
072: * @throws IOException
073: */
074: public String process(ActionRequest request,
075: ActionResponse response, ActionConfig actionConfig)
076: throws PortletException, IOException {
077: Searcher searcher = null;
078: try {
079: request.getPortletSession().removeAttribute("search.hits");
080:
081: searcher = new IndexSearcher(sIndexDir);
082: Analyzer analyzer = new StandardAnalyzer();
083:
084: String line = request.getParameter("query");
085: if (line == null || line.trim().equals("")) {
086: return "success";
087: }
088: QueryParser parser = new QueryParser("contents", analyzer);
089: Query query = parser.parse(line);
090: System.out.println("Searching for: "
091: + query.toString("contents"));
092:
093: Hits hits = searcher.search(query);
094: System.out.println(hits.length()
095: + " total matching documents");
096: Vector hitList = new Vector();
097: for (int i = 0; i < hits.length(); i++) {
098: Document doc = hits.doc(i);
099: String[] hit = new String[3];
100: hit[0] = doc.get("title");
101: hit[1] = doc.get("url");
102: hit[2] = doc.get("summary");
103: hitList.addElement(hit);
104: }
105: request.getPortletSession().setAttribute("search.hits",
106: hitList);
107:
108: } catch (Exception e) {
109: throw new PortletException("Failed to search documents.", e);
110: } finally {
111: searcher.close();
112: }
113: return "success";
114: }
115: }
|