1 package org.apache.archiva.indexer.search;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.io.IOException;
24 import java.util.List;
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.apache.maven.archiva.indexer.search.SearchResultHit;
35 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
36 import org.apache.maven.archiva.indexer.search.SearchResults;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.sonatype.nexus.index.ArtifactInfo;
40 import org.sonatype.nexus.index.FlatSearchRequest;
41 import org.sonatype.nexus.index.FlatSearchResponse;
42 import org.sonatype.nexus.index.NexusIndexer;
43 import org.sonatype.nexus.index.context.IndexContextInInconsistentStateException;
44 import org.sonatype.nexus.index.context.IndexingContext;
45 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
48 * RepositorySearch implementation which uses the Nexus Indexer for searching.
50 public class NexusRepositorySearch
51 implements RepositorySearch
53 private static final Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class );
55 private NexusIndexer indexer;
57 private ArchivaConfiguration archivaConfig;
59 public NexusRepositorySearch( NexusIndexer indexer, ArchivaConfiguration archivaConfig )
61 this.indexer = indexer;
62 this.archivaConfig = archivaConfig;
66 * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
68 public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
69 List<String> previousSearchTerms )
70 throws RepositorySearchException
72 addIndexingContexts( selectedRepos );
74 BooleanQuery q = new BooleanQuery();
75 if( previousSearchTerms == null || previousSearchTerms.isEmpty() )
77 constructQuery( term, q );
81 for( String previousTerm : previousSearchTerms )
83 BooleanQuery iQuery = new BooleanQuery();
84 constructQuery( previousTerm, iQuery );
86 q.add( iQuery, Occur.MUST );
89 BooleanQuery iQuery = new BooleanQuery();
90 constructQuery( term, iQuery );
91 q.add( iQuery, Occur.MUST );
94 return search( limits, q );
98 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
100 public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
101 throws RepositorySearchException
103 if( searchFields.getRepositories() == null )
105 throw new RepositorySearchException( "Repositories cannot be null." );
108 addIndexingContexts( searchFields.getRepositories() );
110 BooleanQuery q = new BooleanQuery();
111 if( searchFields.getGroupId() != null && !"".equals( searchFields.getGroupId() ) )
113 q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, searchFields.getGroupId() ), Occur.MUST );
116 if( searchFields.getArtifactId() != null && !"".equals( searchFields.getArtifactId() ) )
118 q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, searchFields.getArtifactId() ), Occur.MUST );
121 if( searchFields.getVersion() != null && !"".equals( searchFields.getVersion() ) )
123 q.add( indexer.constructQuery( ArtifactInfo.VERSION, searchFields.getVersion() ), Occur.MUST );
126 if( searchFields.getPackaging() != null && !"".equals( searchFields.getPackaging() ) )
128 q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, searchFields.getPackaging() ), Occur.MUST );
131 if( searchFields.getClassName() != null && !"".equals( searchFields.getClassName() ) )
133 q.add( indexer.constructQuery( ArtifactInfo.NAMES, searchFields.getClassName() ), Occur.MUST );
136 if( q.getClauses() == null || q.getClauses().length <= 0 )
138 throw new RepositorySearchException( "No search fields set." );
141 return search( limits, q );
144 private SearchResults search( SearchResultLimits limits, BooleanQuery q )
145 throws RepositorySearchException
149 FlatSearchRequest request = new FlatSearchRequest( q );
150 FlatSearchResponse response = indexer.searchFlat( request );
152 if( response == null || response.getTotalHits() == 0 )
154 SearchResults results = new SearchResults();
155 results.setLimits( limits );
159 return convertToSearchResults( response, limits );
161 catch ( IndexContextInInconsistentStateException e )
163 throw new RepositorySearchException( e );
165 catch ( IOException e )
167 throw new RepositorySearchException( e );
171 Map<String, IndexingContext> indexingContexts = indexer.getIndexingContexts();
172 Set<String> keys = indexingContexts.keySet();
173 for( String key : keys )
177 indexer.removeIndexingContext( indexingContexts.get( key ), false );
178 log.debug( "Indexing context '" + key + "' removed from search." );
180 catch ( IOException e )
182 log.warn( "IOException occurred while removing indexing content '" + key + "'." );
189 private void constructQuery( String term, BooleanQuery q )
191 q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, term ), Occur.SHOULD );
192 q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, term ), Occur.SHOULD );
193 q.add( indexer.constructQuery( ArtifactInfo.VERSION, term ), Occur.SHOULD );
194 q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, term ), Occur.SHOULD );
195 q.add( indexer.constructQuery( ArtifactInfo.NAMES, term ), Occur.SHOULD );
199 private void addIndexingContexts( List<String> selectedRepos )
201 for( String repo : selectedRepos )
205 Configuration config = archivaConfig.getConfiguration();
206 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo );
208 if( repoConfig != null )
210 String indexDir = repoConfig.getIndexDir();
211 File indexDirectory = null;
212 if( indexDir != null && !"".equals( indexDir ) )
214 indexDirectory = new File( repoConfig.getIndexDir() );
218 indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
221 IndexingContext context =
222 indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ),
223 indexDirectory, null, null, NexusIndexer.FULL_INDEX );
224 context.setSearchable( repoConfig.isScanned() );
228 log.warn( "Repository '" + repo + "' not found in configuration." );
231 catch ( UnsupportedExistingLuceneIndexException e )
233 log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
236 catch ( IOException e )
238 log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
244 private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits )
246 SearchResults results = new SearchResults();
247 Set<ArtifactInfo> artifactInfos = response.getResults();
249 for ( ArtifactInfo artifactInfo : artifactInfos )
251 String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId );
252 Map<String, SearchResultHit> hitsMap = results.getHitsMap();
254 SearchResultHit hit = hitsMap.get( id );
257 hit.addVersion( artifactInfo.version );
261 hit = new SearchResultHit();
262 hit.setArtifactId( artifactInfo.artifactId );
263 hit.setGroupId( artifactInfo.groupId );
264 // do we still need to set the repository id even though we're merging everything?
265 //hit.setRepositoryId( artifactInfo.repository );
266 hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname );
267 if( !hit.getVersions().contains( artifactInfo.version ) )
269 hit.addVersion( artifactInfo.version );
273 results.addHit( id, hit );
276 results.setTotalHits( results.getHitsMap().size() );
277 results.setLimits( limits );
279 if( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
285 return paginate( results );
289 private SearchResults paginate( SearchResults results )
291 SearchResultLimits limits = results.getLimits();
292 SearchResults paginated = new SearchResults();
294 int fetchCount = limits.getPageSize();
295 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
297 if( fetchCount > results.getTotalHits() )
299 fetchCount = results.getTotalHits();
303 if ( offset < results.getTotalHits() )
305 // only process if the offset is within the hit count.
306 for ( int i = 0; i < fetchCount; i++ )
308 // Stop fetching if we are past the total # of available hits.
309 if ( offset + i >= results.getHits().size() )
314 SearchResultHit hit = results.getHits().get( ( offset + i ) );
317 String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() );
318 paginated.addHit( id, hit );
326 paginated.setTotalHits( results.getTotalHits() );
327 paginated.setLimits( limits );