]> source.dussan.org Git - archiva.git/blob
9d88d3d77eb7a4aac9976847c44edbf10a96169f
[archiva.git] /
1 package org.apache.archiva.indexer.search;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.archiva.indexer.util.SearchUtil;
29 import org.apache.lucene.search.BooleanQuery;
30 import org.apache.lucene.search.BooleanClause.Occur;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.Configuration;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.sonatype.nexus.index.ArtifactInfo;
37 import org.sonatype.nexus.index.FlatSearchRequest;
38 import org.sonatype.nexus.index.FlatSearchResponse;
39 import org.sonatype.nexus.index.NexusIndexer;
40 import org.sonatype.nexus.index.context.IndexingContext;
41 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
42
43 /**
44  * RepositorySearch implementation which uses the Nexus Indexer for searching.
45  */
46 public class NexusRepositorySearch
47     implements RepositorySearch
48 {
49     private static final Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class ); 
50                                                               
51     private NexusIndexer indexer;
52     
53     private ArchivaConfiguration archivaConfig;
54     
55     public NexusRepositorySearch( NexusIndexer indexer, ArchivaConfiguration archivaConfig )
56     {
57         this.indexer = indexer;
58         this.archivaConfig = archivaConfig;
59     }
60
61     /**
62      * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
63      */
64     public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
65                                  List<String> previousSearchTerms )
66         throws RepositorySearchException
67     {   
68         addIndexingContexts( selectedRepos );
69                 
70         BooleanQuery q = new BooleanQuery();
71         if( previousSearchTerms == null || previousSearchTerms.isEmpty() )
72         {            
73             constructQuery( term, q );
74         }
75         else
76         {   
77             for( String previousTerm : previousSearchTerms )
78             {
79                 BooleanQuery iQuery = new BooleanQuery();
80                 constructQuery( previousTerm, iQuery );
81                 
82                 q.add( iQuery, Occur.MUST );
83             }
84             
85             BooleanQuery iQuery = new BooleanQuery();
86             constructQuery( term, iQuery );
87             q.add( iQuery, Occur.MUST );
88         }        
89                     
90         return search( limits, q );
91     }
92     
93     /**
94      * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
95      */
96     public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
97         throws RepositorySearchException
98     {
99         if( searchFields.getRepositories() == null )
100         {
101             throw new RepositorySearchException( "Repositories cannot be null." );
102         }
103         
104         addIndexingContexts( searchFields.getRepositories() );
105         
106         BooleanQuery q = new BooleanQuery();
107         if( searchFields.getGroupId() != null && !"".equals( searchFields.getGroupId() ) )
108         {   
109             q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, searchFields.getGroupId() ), Occur.MUST );
110         }
111         
112         if( searchFields.getArtifactId() != null && !"".equals( searchFields.getArtifactId() ) )
113         {
114             q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, searchFields.getArtifactId() ), Occur.MUST );
115         }
116         
117         if( searchFields.getVersion() != null && !"".equals( searchFields.getVersion() ) )
118         {
119             q.add( indexer.constructQuery( ArtifactInfo.VERSION, searchFields.getVersion() ), Occur.MUST );
120         }
121         
122         if( searchFields.getPackaging() != null && !"".equals( searchFields.getPackaging() ) )
123         {
124             q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, searchFields.getPackaging() ), Occur.MUST );
125         }
126         
127         if( searchFields.getClassName() != null && !"".equals( searchFields.getClassName() ) )
128         {
129             q.add( indexer.constructQuery( ArtifactInfo.NAMES, searchFields.getClassName() ), Occur.MUST );
130         }
131         
132         if( q.getClauses() == null || q.getClauses().length <= 0 )
133         {
134             throw new RepositorySearchException( "No search fields set." );
135         }
136         
137         return search( limits, q );        
138     }
139
140     private SearchResults search( SearchResultLimits limits, BooleanQuery q )
141         throws RepositorySearchException
142     {
143         try
144         {
145             FlatSearchRequest request = new FlatSearchRequest( q );
146             FlatSearchResponse response = indexer.searchFlat( request );
147             
148             if( response == null || response.getTotalHits() == 0 )
149             {
150                 SearchResults results = new SearchResults();
151                 results.setLimits( limits );
152                 return results;
153             }
154             
155             return convertToSearchResults( response, limits );
156         }
157         catch ( IOException e )
158         {
159             throw new RepositorySearchException( e );
160         }
161         finally
162         {
163             Map<String, IndexingContext> indexingContexts = indexer.getIndexingContexts();
164             Set<String> keys = indexingContexts.keySet();
165             for( String key : keys )
166             {
167                 try                
168                 {   
169                     indexer.removeIndexingContext( indexingContexts.get( key ), false );
170                     log.debug( "Indexing context '" + key + "' removed from search." );
171                 }
172                 catch ( IOException e )
173                 {
174                     log.warn( "IOException occurred while removing indexing content '" + key  + "'." );
175                     continue;
176                 }
177             }            
178         }
179     }
180
181     private void constructQuery( String term, BooleanQuery q )
182     {
183         q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, term ), Occur.SHOULD );
184         q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, term ), Occur.SHOULD );
185         q.add( indexer.constructQuery( ArtifactInfo.VERSION, term ), Occur.SHOULD );
186         q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, term ), Occur.SHOULD );
187         q.add( indexer.constructQuery( ArtifactInfo.NAMES, term ), Occur.SHOULD );        
188     }
189        
190     
191     private void addIndexingContexts( List<String> selectedRepos )
192     {
193         for( String repo : selectedRepos )
194         {
195             try
196             {
197                 Configuration config = archivaConfig.getConfiguration();
198                 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo );
199                 
200                 if( repoConfig != null )
201                 {
202                     String indexDir = repoConfig.getIndexDir();
203                     File indexDirectory = null;
204                     if( indexDir != null && !"".equals( indexDir ) )
205                     {
206                         indexDirectory = new File( repoConfig.getIndexDir() );
207                     }
208                     else
209                     {
210                         indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
211                     }
212                     
213                     IndexingContext context =
214                         indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ),
215                                                     indexDirectory, null, null, NexusIndexer.FULL_INDEX );
216                     context.setSearchable( repoConfig.isScanned() );
217                 }
218                 else
219                 {
220                     log.warn( "Repository '" + repo + "' not found in configuration." );
221                 }
222             }
223             catch ( UnsupportedExistingLuceneIndexException e )
224             {                
225                 log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
226                 continue;
227             }
228             catch ( IOException e )
229             {                
230                 log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
231                 continue;
232             }
233         }
234     }
235
236     private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits )
237     {   
238         SearchResults results = new SearchResults();
239         Set<ArtifactInfo> artifactInfos = response.getResults();
240         
241         for ( ArtifactInfo artifactInfo : artifactInfos )
242         {
243             String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId );
244             Map<String, SearchResultHit> hitsMap = results.getHitsMap();
245
246             SearchResultHit hit = hitsMap.get( id );
247             if ( hit != null )
248             {
249                 hit.addVersion( artifactInfo.version );
250             }
251             else
252             {
253                 hit = new SearchResultHit();
254                 hit.setArtifactId( artifactInfo.artifactId );
255                 hit.setGroupId( artifactInfo.groupId );
256                 // do we still need to set the repository id even though we're merging everything?
257                 //hit.setRepositoryId( artifactInfo.repository );
258                 hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname );
259                 if( !hit.getVersions().contains( artifactInfo.version ) )
260                 {
261                     hit.addVersion( artifactInfo.version );
262                 }
263             }
264
265             results.addHit( id, hit );
266         }
267         
268         results.setTotalHits( results.getHitsMap().size() );
269         results.setLimits( limits );
270         
271         if( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
272         {   
273             return results;
274         }
275         else
276         {
277             return paginate( results );            
278         }        
279     }
280
281     private SearchResults paginate( SearchResults results )
282     {
283         SearchResultLimits limits = results.getLimits();
284         SearchResults paginated = new SearchResults();  
285         
286         int fetchCount = limits.getPageSize();
287         int offset = ( limits.getSelectedPage() * limits.getPageSize() );
288         
289         if( fetchCount > results.getTotalHits() )
290         {
291             fetchCount = results.getTotalHits();
292         }
293         
294         // Goto offset.
295         if ( offset < results.getTotalHits() )
296         {
297             // only process if the offset is within the hit count.
298             for ( int i = 0; i < fetchCount; i++ )
299             {
300                 // Stop fetching if we are past the total # of available hits.
301                 if ( offset + i >= results.getHits().size() )
302                 {
303                     break;
304                 }
305                 
306                 SearchResultHit hit = results.getHits().get( ( offset + i ) );
307                 if( hit != null )
308                 {
309                     String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() );
310                     paginated.addHit( id, hit );
311                 }
312                 else
313                 {
314                     break;
315                 }
316             }
317         }            
318         paginated.setTotalHits( results.getTotalHits() );
319         paginated.setLimits( limits );
320         
321         return paginated;
322     }
323 }