001    /**
002     * LuceneSearcher.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 10/04/2007
008     */
009    package jcolibri.extensions.textual.lucene;
010    
011    
012    
013    import jcolibri.extensions.textual.lucene.spanish.SpanishAnalyzer;
014    
015    import org.apache.lucene.analysis.Analyzer;
016    import org.apache.lucene.analysis.standard.StandardAnalyzer;
017    import org.apache.lucene.queryParser.QueryParser;
018    import org.apache.lucene.search.Hits;
019    import org.apache.lucene.search.IndexSearcher;
020    import org.apache.lucene.search.Query;
021    import org.apache.lucene.search.Searcher;
022    
023    /**
024     * Searchs for documents given a query and an index.
025     * @author Juan A. Recio-García
026     * @version 1.0
027     */
028    public class LuceneSearcher {
029            
030            /**
031             * Performs a search using Lucene
032             * @param index with the documents to search
033             * @param query to search
034             * @param fieldName field where search inside the documents
035             * @return the search result
036             */
037            public static LuceneSearchResult search(LuceneIndex index, String query, String fieldName)
038            {           
039                    try {
040    
041                        Searcher searcher = new IndexSearcher(index.getDirectory());
042                        Analyzer analyzer = new StandardAnalyzer();
043                        QueryParser parser = new QueryParser(fieldName, analyzer);
044                        Query q = parser.parse(query);
045                        Hits hits = searcher.search(q);
046                        LuceneSearchResult lsr = new LuceneSearchResult(hits, index);
047                        searcher.close();
048                        return lsr;
049                            
050                    } catch (Exception e) {
051                            org.apache.commons.logging.LogFactory.getLog(LuceneSearcher.class).error(e);
052                    }
053                return null;
054            }
055            
056            /**
057             * Performs a search over spanish texts using Lucene
058             * @param index with the documents to search
059             * @param query to search
060             * @param fieldName field where search inside the documents
061             * @return the search result
062             */
063            public static LuceneSearchResult searchSpanish(LuceneIndex index, String query, String fieldName)
064            {           
065                    try {
066    
067                        Searcher searcher = new IndexSearcher(index.getDirectory());
068                        Analyzer analyzer = new SpanishAnalyzer();
069                        QueryParser parser = new QueryParser(fieldName, analyzer);
070                        Query q = parser.parse(query);
071                        Hits hits = searcher.search(q);
072                        LuceneSearchResult lsr = new LuceneSearchResult(hits, index);
073                        searcher.close();
074                        return lsr;
075                            
076                    } catch (Exception e) {
077                            org.apache.commons.logging.LogFactory.getLog(LuceneSearcher.class).error(e);
078                    }
079                return null;
080            }
081    }