Package net.fp.rp.search.back.search

Source Code of net.fp.rp.search.back.search.RpSearcher

/*
* Copyright (C) 2004 Paul Browne, http://www.firstpartners.net,
* built with the help of Fast-Soft (fastsoftdev@yahoo.com)
*
* 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)."
*
* This product includes software developed by the
* Spring Framework Project (http://www.springframework.org)."
*
*/
package net.fp.rp.search.back.search;

import net.fp.rp.common.exception.RpException;
import net.fp.rp.search.back.struct.DocumStruct;
import net.fp.rp.search.mid.global.PluginManager;
import net.fp.rp.search.plugins.IIndexManager;
import net.fp.rp.search.plugins.ISearchQuery;
import net.fp.rp.search.plugins.ISearchResult;
import net.fp.rp.search.plugins.events.IInterestedInSearch;

import org.apache.log4j.Logger;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
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.Sort;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;

import java.util.LinkedList;


/**
* Searcher plugin
*
* @author Firstpartners.net
* Copyright @link www.firstpartners.net/red
*/
public class RpSearcher implements IInterestedInSearch {
    /** Logger for this class and subclasses */
    protected final Logger logger = Logger.getLogger(getClass());

    /** Search result object */
    private ISearchResult result;

    /**
     * Search the Known Data
     *
     * @param searchQuery Search queryy
     * @param sortField Sort field
     * @param sortDescMode Descendent mode = true, false - otherwise
     * @param maxDocNo The maxim count of documents which the searcher must to deliver
     * @param doThreadedSearch - true if we want to handle concurrant searches by different plugins
     *
     * @throws RpException Error occur in search process
     */
    public void doSearch(ISearchQuery searchQuery, final String sortField,
        final boolean sortDescMode, final int maxDocNo,boolean doThreadedSearch)
        throws RpException {
        //obtain a reference to the actual index
        IIndexManager indexer = PluginManager.getIndexManager();

        if (indexer != null) {
            Analyzer analyzer = new StandardAnalyzer();

            try {
                logger.debug("Index folder is :" + indexer.getRoot());

                Directory fsDir = FSDirectory.getDirectory(indexer.getRoot(),
                        false);
                IndexSearcher searcher = new IndexSearcher(fsDir);
                logger.info("Search query is " + searchQuery.getQuery());

                Query query = QueryParser.parse(searchQuery.getQuery(),
                        "content", analyzer);

                Sort sort = new Sort();
                sort.setSort(sortField, sortDescMode);

                Hits hits = searcher.search(query, sort);
                logger.debug("Query is " + query.toString() + " hits :" +
                    hits.length());

                LinkedList listDocs = new LinkedList();
                int countErrors = 0;

                for (int i = 0;
                        (i < hits.length()) && (listDocs.size() < maxDocNo);
                        i++) {
                    try {
                        //get the document and add it to the list
                        Document doc = hits.doc(i);
                        logger.debug("Doc path" +
                            doc.get(DocumStruct.FIELD_PATH));
                        logger.debug("CategName " +
                            doc.get(DocumStruct.FIELD_CATEGORY_NAME));
                        logger.debug("CalcScore " +
                            doc.get(DocumStruct.FIELD_CALCSCORE));
                        logger.debug("CategScore " +
                            doc.get(DocumStruct.FIELD_CATEGORY_SCORE));
                        logger.debug("GeneralScore " + doc.get(sortField));

                        //translation from the found it lucene document to the RPDocument to the return documents
                        DocumStruct docStruct = new DocumStruct();
                        docStruct.setTitle(doc.get(DocumStruct.FIELD_TITLE));
                        docStruct.setPath(doc.get(DocumStruct.FIELD_PATH));
                        docStruct.setDescription(doc.get(
                                DocumStruct.FIELD_DESCRIPTION));
                        docStruct.setLastUpdate(doc.get(
                                DocumStruct.FIELD_LASTUPDATE));
                        docStruct.setId(doc.get(DocumStruct.FIELD_DOCUMENTID));

                        docStruct.setCategoryName(doc.get(
                                DocumStruct.FIELD_CATEGORY_NAME));

                        try {
                            docStruct.setCategoryScore(Double.valueOf(doc.get(
                                        DocumStruct.FIELD_CATEGORY_SCORE))
                                                             .doubleValue());
                        } catch (Throwable e) {
                            //set the default category score
                            docStruct.setCategoryScore(1.00);
                        }

                        docStruct.setCategoryLocation(doc.get(
                                DocumStruct.FIELD_CATEGORY_LOCATION));

                        listDocs.add(docStruct);
                    } catch (IOException e) {
                        countErrors++;
                    }
                }

                //create the result
                result = new ResultPages(listDocs, hits.length(), countErrors);
            } catch (ParseException e) {
                logger.warn("Error in searching in the specified index");
            } catch (IOException e) {
                logger.warn("Error in searching in the specified index");
            }
        } else {
            //new message into the message properties file
            logger.warn("NO INDEXES FOUND");
            throw new RpException("app.search.index.noinit");
        }
    }

    /**
     * Poll to see if the search has finished
     *
     * @return True if the basic intelligence is ready
     */
    public boolean isReady() {
        if (result != null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Get the Search results generated by doSearch
     *
     * @return searchResults
     */
    public ISearchResult getResults() {
        Logger.getRootLogger().debug("Get the results");

        return result;
    }

    /**
     * No process occur for the searcher
     */
    public void onLoad() {
    }
}
TOP

Related Classes of net.fp.rp.search.back.search.RpSearcher

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.