Coverage Report - fr.paris.lutece.plugins.digglike.service.digglikesearch.DigglikeLuceneSearchEngine
 
Classes in this File Line Coverage Branch Coverage Complexity
DigglikeLuceneSearchEngine
0 %
0/39
0 %
0/10
7
 
 1  
 /*
 2  
  * Copyright (c) 2002-2014, Mairie de Paris
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  *
 9  
  *  1. Redistributions of source code must retain the above copyright notice
 10  
  *     and the following disclaimer.
 11  
  *
 12  
  *  2. Redistributions in binary form must reproduce the above copyright notice
 13  
  *     and the following disclaimer in the documentation and/or other materials
 14  
  *     provided with the distribution.
 15  
  *
 16  
  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 17  
  *     contributors may be used to endorse or promote products derived from
 18  
  *     this software without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  *
 32  
  * License 1.0
 33  
  */
 34  
 package fr.paris.lutece.plugins.digglike.service.digglikesearch;
 35  
 
 36  
 import fr.paris.lutece.plugins.digglike.business.SubmitFilter;
 37  
 import fr.paris.lutece.plugins.digglike.service.search.DigglikeIndexer;
 38  
 import fr.paris.lutece.portal.service.search.IndexationService;
 39  
 import fr.paris.lutece.portal.service.search.LuceneSearchEngine;
 40  
 import fr.paris.lutece.portal.service.util.AppLogService;
 41  
 
 42  
 import java.util.ArrayList;
 43  
 import java.util.Collection;
 44  
 import java.util.List;
 45  
 
 46  
 import org.apache.lucene.document.Document;
 47  
 import org.apache.lucene.index.DirectoryReader;
 48  
 import org.apache.lucene.index.IndexReader;
 49  
 import org.apache.lucene.index.Term;
 50  
 import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
 51  
 import org.apache.lucene.search.BooleanClause;
 52  
 import org.apache.lucene.search.IndexSearcher;
 53  
 import org.apache.lucene.search.Query;
 54  
 import org.apache.lucene.search.ScoreDoc;
 55  
 import org.apache.lucene.search.TermQuery;
 56  
 import org.apache.lucene.search.TopDocs;
 57  
 
 58  
 
 59  
 /**
 60  
  * LuceneSearchEngine
 61  
  */
 62  0
 public class DigglikeLuceneSearchEngine implements DigglikeSearchEngine
 63  
 {
 64  
     /**
 65  
      * Return search results
 66  
      * 
 67  
      * @param strQuery The search query
 68  
      * @param filter the filter
 69  
      * @return Results as a collection of SearchResult
 70  
      */
 71  
     public List<DigglikeSearchItem> getSearchResults( String strQuery, SubmitFilter filter )
 72  
     {
 73  0
         List<DigglikeSearchItem> listResults = new ArrayList<DigglikeSearchItem>( );
 74  0
         IndexSearcher searcher = null;
 75  
 
 76  
         try
 77  
         {
 78  0
             IndexReader ir = DirectoryReader.open( IndexationService.getDirectoryIndex( ) );
 79  0
             searcher = new IndexSearcher( ir );
 80  
 
 81  0
             Collection<String> queries = new ArrayList<String>( );
 82  0
             Collection<String> fields = new ArrayList<String>( );
 83  0
             Collection<BooleanClause.Occur> flags = new ArrayList<BooleanClause.Occur>( );
 84  
 
 85  
             //filter on content
 86  0
             if ( ( strQuery != null ) && !strQuery.equals( "" ) )
 87  
             {
 88  0
                 Query queryContent = new TermQuery( new Term( DigglikeSearchItem.FIELD_CONTENTS, strQuery ) );
 89  0
                 queries.add( queryContent.toString( ) );
 90  0
                 fields.add( DigglikeSearchItem.FIELD_CONTENTS );
 91  0
                 flags.add( BooleanClause.Occur.MUST );
 92  
             }
 93  
 
 94  
             //filter on content id digg
 95  0
             if ( filter.containsIdDigg( ) )
 96  
             {
 97  0
                 Query queryIdDigg = new TermQuery( new Term( DigglikeSearchItem.FIELD_ID_DIGG, String.valueOf( filter
 98  
                         .getIdDigg( ) ) ) );
 99  0
                 queries.add( queryIdDigg.toString( ) );
 100  0
                 fields.add( DigglikeSearchItem.FIELD_ID_DIGG );
 101  0
                 flags.add( BooleanClause.Occur.MUST );
 102  
             }
 103  
 
 104  
             //filter on digg submit state
 105  0
             if ( filter.containsIdDiggSubmitState( ) )
 106  
             {
 107  0
                 Query queryState = new TermQuery( new Term( DigglikeSearchItem.FIELD_STATE, String.valueOf( filter
 108  
                         .getIdDiggSubmitState( ) ) ) );
 109  0
                 queries.add( queryState.toString( ) );
 110  0
                 fields.add( DigglikeSearchItem.FIELD_STATE );
 111  0
                 flags.add( BooleanClause.Occur.MUST );
 112  
             }
 113  
 
 114  
             //filter on digg type
 115  0
             Query queryTypeDigg = new TermQuery( new Term( DigglikeSearchItem.FIELD_TYPE,
 116  
                     DigglikeIndexer.INDEX_TYPE_DIGG ) );
 117  0
             queries.add( queryTypeDigg.toString( ) );
 118  0
             fields.add( DigglikeSearchItem.FIELD_TYPE );
 119  0
             flags.add( BooleanClause.Occur.MUST );
 120  
 
 121  0
             Query queryMulti = MultiFieldQueryParser.parse( IndexationService.LUCENE_INDEX_VERSION,
 122  
                     (String[]) queries.toArray( new String[queries.size( )] ),
 123  
                     (String[]) fields.toArray( new String[fields.size( )] ),
 124  
                     (BooleanClause.Occur[]) flags.toArray( new BooleanClause.Occur[flags.size( )] ),
 125  
                     IndexationService.getAnalyser( ) );
 126  
 
 127  0
             TopDocs topDocs = searcher.search( queryMulti, LuceneSearchEngine.MAX_RESPONSES );
 128  0
             ScoreDoc[] hits = topDocs.scoreDocs;
 129  
 
 130  0
             for ( int i = 0; i < hits.length; i++ )
 131  
             {
 132  0
                 int docId = hits[i].doc;
 133  0
                 Document document = searcher.doc( docId );
 134  0
                 DigglikeSearchItem si = new DigglikeSearchItem( document );
 135  0
                 listResults.add( si );
 136  
             }
 137  
         }
 138  0
         catch ( Exception e )
 139  
         {
 140  0
             AppLogService.error( e.getMessage( ), e );
 141  0
         }
 142  
 
 143  0
         return listResults;
 144  
     }
 145  
 }