Package com.redpiranha.server.input.search

Source Code of com.redpiranha.server.input.search.SearchAgent

package com.redpiranha.server.input.search;

/*
* Copyright (C) 2001- 2004 Paul Browne, http://www.firstpartners.net,
*
* released under terms of the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This product includes software developed by the
* Apache Software Foundation (http://www.apache.org)."
*
*/

import org.apache.log4j.Logger;

import org.apache.lucene.analysis.Analyzer;
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.search.Searcher;

import com.redpiranha.server.input.index.lucene.*;
import com.redpiranha.common.*;


/**
* FACADE pattern to doing searches , perhaps as an EJB
*
* @author BROWNEPA Will become the main class for accessing searches Some of This Code is based on a Sample from
*         Apache Lucence /www.apache.org
*/
public class SearchAgent
{
        //  Handle to logger
        static Logger log = Logger.getLogger(SearchAgent.class);


        /**
         * Carry out a search against the index
         *
         * @param searchFor
         *
         * @return hits - a list of the hits that were made against the database
         *
@throws SearchException if the search messes up!
         */
        public Hits doSearch(SearchQuery searchFor) throws SearchException, Exception
         {
                //Local Variables
                Searcher searcher = null; //index to search
                Analyzer analyzer = new RpAnalyzer(); //analysis of this index

                Query query = null; //the (parsed) query
                Hits hits = null; //the hits against the database

                Document doc = null; //ID of current Document

                log.debug(" Original Query: " + searchFor);

                try
                 {
                        //Get a handle to the index
                        searcher = new IndexSearcher(Constants.INDEX_NAME);

                        //Build the query into Lucene specific format
                        query = QueryParser.parse(searchFor.getSearchString(), "contents", analyzer);

                        log.debug("Query Class:" + query.getClass().getName());
                        log.debug("Searching for: " + query.toString("contents"));
                        log.debug("Searching for: " + query.toString());

                        //Do the Search
                        hits = searcher.search(query);

                        log.debug("Number of Results:" + hits.length());

                        //Loop and display the results
                        for (int counter = 0; counter < hits.length(); counter++)
                         {
                                doc = hits.doc(counter);
                                log.debug("Rank:" + hits.score(counter) + " Info:" + doc.toString());
                        }

                        searcher.close();
                       
                } catch (java.io.IOException iex)
                 {
                        throw new SearchException(iex);
                } catch (org.apache.lucene.queryParser.ParseException pex)
                 {
                        throw new SearchException(pex);
                }

        log.debug("****** SearchAgent.doSearch completed");

                return hits;
        }
}
TOP

Related Classes of com.redpiranha.server.input.search.SearchAgent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.