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
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.beans.ProxyConnector;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
27 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
28 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
29 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
30 import org.apache.archiva.indexer.util.SearchUtil;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.lucene.search.BooleanClause.Occur;
33 import org.apache.lucene.search.BooleanQuery;
34 import org.apache.maven.index.ArtifactInfo;
35 import org.apache.maven.index.FlatSearchRequest;
36 import org.apache.maven.index.FlatSearchResponse;
37 import org.apache.maven.index.MAVEN;
38 import org.apache.maven.index.NexusIndexer;
39 import org.apache.maven.index.OSGI;
40 import org.apache.maven.index.context.IndexCreator;
41 import org.apache.maven.index.context.IndexingContext;
42 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
43 import org.apache.maven.index.expr.StringSearchExpression;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.stereotype.Service;
48 import javax.inject.Inject;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.HashSet;
54 import java.util.List;
59 * RepositorySearch implementation which uses the Nexus Indexer for searching.
61 @Service( "nexusSearch" )
62 public class NexusRepositorySearch
63 implements RepositorySearch
65 private Logger log = LoggerFactory.getLogger( getClass() );
67 private NexusIndexer indexer;
69 private ManagedRepositoryAdmin managedRepositoryAdmin;
71 private ProxyConnectorAdmin proxyConnectorAdmin;
73 private MavenIndexerUtils mavenIndexerUtils;
76 public NexusRepositorySearch( PlexusSisuBridge plexusSisuBridge, ManagedRepositoryAdmin managedRepositoryAdmin,
77 MavenIndexerUtils mavenIndexerUtils, ProxyConnectorAdmin proxyConnectorAdmin )
78 throws PlexusSisuBridgeException
80 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
81 this.managedRepositoryAdmin = managedRepositoryAdmin;
82 this.mavenIndexerUtils = mavenIndexerUtils;
83 this.proxyConnectorAdmin = proxyConnectorAdmin;
87 * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
89 public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
90 List<String> previousSearchTerms )
91 throws RepositorySearchException
93 List<String> indexingContextIds = addIndexingContexts( selectedRepos );
95 // since upgrade to nexus 2.0.0, query has changed from g:[QUERIED TERM]* to g:*[QUERIED TERM]*
96 // resulting to more wildcard searches so we need to increase max clause count
97 BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE );
98 BooleanQuery q = new BooleanQuery();
100 if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
102 constructQuery( term, q );
106 for ( String previousTerm : previousSearchTerms )
108 BooleanQuery iQuery = new BooleanQuery();
109 constructQuery( previousTerm, iQuery );
111 q.add( iQuery, Occur.MUST );
114 BooleanQuery iQuery = new BooleanQuery();
115 constructQuery( term, iQuery );
116 q.add( iQuery, Occur.MUST );
119 // we retun only artifacts without classifier in quick search, olamy cannot find a way to say with this field empty
120 // FIXME cannot find a way currently to setup this in constructQuery !!!
121 return search( limits, q, indexingContextIds, NoClassifierArtifactInfoFiler.LIST );
126 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
128 public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
129 throws RepositorySearchException
131 if ( searchFields.getRepositories() == null )
133 throw new RepositorySearchException( "Repositories cannot be null." );
136 List<String> indexingContextIds = addIndexingContexts( searchFields.getRepositories() );
138 BooleanQuery q = new BooleanQuery();
139 if ( StringUtils.isNotBlank( searchFields.getGroupId() ) )
141 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( searchFields.getGroupId() ) ),
145 if ( StringUtils.isNotBlank( searchFields.getArtifactId() ) )
148 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( searchFields.getArtifactId() ) ),
152 if ( StringUtils.isNotBlank( searchFields.getVersion() ) )
154 q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( searchFields.getVersion() ) ),
158 if ( StringUtils.isNotBlank( searchFields.getPackaging() ) )
160 q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( searchFields.getPackaging() ) ),
164 if ( StringUtils.isNotBlank( searchFields.getClassName() ) )
167 indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( searchFields.getClassName() ) ),
171 if ( StringUtils.isNotBlank( searchFields.getBundleSymbolicName() ) )
173 q.add( indexer.constructQuery( OSGI.SYMBOLIC_NAME,
174 new StringSearchExpression( searchFields.getBundleSymbolicName() ) ),
178 if ( StringUtils.isNotBlank( searchFields.getBundleVersion() ) )
181 indexer.constructQuery( OSGI.VERSION, new StringSearchExpression( searchFields.getBundleVersion() ) ),
185 if ( StringUtils.isNotBlank( searchFields.getBundleExportPackage() ) )
187 q.add( indexer.constructQuery( OSGI.EXPORT_PACKAGE,
188 new StringSearchExpression( searchFields.getBundleExportPackage() ) ),
192 if ( StringUtils.isNotBlank( searchFields.getBundleExportService() ) )
194 q.add( indexer.constructQuery( OSGI.EXPORT_SERVICE,
195 new StringSearchExpression( searchFields.getBundleExportService() ) ),
199 if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
201 q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
202 new StringSearchExpression( searchFields.getBundleImportPackage() ) ),
206 if ( StringUtils.isNotBlank( searchFields.getBundleName() ) )
208 q.add( indexer.constructQuery( OSGI.NAME, new StringSearchExpression( searchFields.getBundleName() ) ),
212 if ( StringUtils.isNotBlank( searchFields.getClassifier() ) )
215 indexer.constructQuery( MAVEN.CLASSIFIER, new StringSearchExpression( searchFields.getClassifier() ) ),
219 if ( q.getClauses() == null || q.getClauses().length <= 0 )
221 throw new RepositorySearchException( "No search fields set." );
224 return search( limits, q, indexingContextIds, Collections.<ArtifactInfoFiler>emptyList() );
227 private SearchResults search( SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds,
228 List<? extends ArtifactInfoFiler> filters )
229 throws RepositorySearchException
234 FlatSearchRequest request = new FlatSearchRequest( q );
235 request.setContexts( getIndexingContexts( indexingContextIds ) );
236 FlatSearchResponse response = indexer.searchFlat( request );
238 if ( response == null || response.getTotalHits() == 0 )
240 SearchResults results = new SearchResults();
241 results.setLimits( limits );
245 return convertToSearchResults( response, limits, filters );
247 catch ( IOException e )
249 throw new RepositorySearchException( e );
254 private List<IndexingContext> getIndexingContexts( List<String> ids )
256 List<IndexingContext> contexts = new ArrayList<IndexingContext>( ids.size() );
258 for ( String id : ids )
260 IndexingContext context = indexer.getIndexingContexts().get( id );
261 if ( context != null )
263 contexts.add( context );
267 log.warn( "context with id {} not exists", id );
274 private void constructQuery( String term, BooleanQuery q )
276 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( term ) ), Occur.SHOULD );
277 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( term ) ), Occur.SHOULD );
278 q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( term ) ), Occur.SHOULD );
279 q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( term ) ), Occur.SHOULD );
280 q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( term ) ), Occur.SHOULD );
283 // new WildcardQuery( new Term( MAVEN.CLASSNAMES.getFieldName(), "*" ) );
284 //q.add( query, Occur.MUST_NOT );
285 // olamy IMHO we could set this option as at least one must match
286 //q.setMinimumNumberShouldMatch( 1 );
291 * @param selectedRepos
292 * @return indexing contextId used
294 private List<String> addIndexingContexts( List<String> selectedRepos )
296 Set<String> indexingContextIds = new HashSet<String>();
297 for ( String repo : selectedRepos )
301 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repo );
303 if ( repoConfig != null )
305 String indexDir = repoConfig.getIndexDirectory();
306 File indexDirectory = null;
307 if ( indexDir != null && !"".equals( indexDir ) )
309 indexDirectory = new File( repoConfig.getIndexDirectory() );
313 indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
316 IndexingContext context = indexer.getIndexingContexts().get( repoConfig.getId() );
317 if ( context != null )
319 // alreday here so no need to record it again
320 log.debug( "index with id {} already exists skip adding it", repoConfig.getId() );
321 // set searchable flag
322 context.setSearchable( repoConfig.isScanned() );
323 indexingContextIds.add( context.getId() );
324 indexingContextIds.addAll( getRemoteIndexingContextIds( repo ) );
328 context = indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(),
329 new File( repoConfig.getLocation() ), indexDirectory, null,
330 null, getAllIndexCreators() );
331 context.setSearchable( repoConfig.isScanned() );
332 if ( context.isSearchable() )
334 indexingContextIds.addAll( getRemoteIndexingContextIds( repo ) );
335 indexingContextIds.add( context.getId() );
339 log.warn( "indexingContext with id {} not searchable", repoConfig.getId() );
345 log.warn( "Repository '" + repo + "' not found in configuration." );
348 catch ( UnsupportedExistingLuceneIndexException e )
350 log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
353 catch ( IOException e )
355 log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
358 catch ( RepositoryAdminException e )
360 log.warn( "RepositoryAdminException occured while accessing index of repository '" + repo + "' : "
366 return new ArrayList<String>( indexingContextIds );
370 private Set<String> getRemoteIndexingContextIds( String managedRepoId )
371 throws RepositoryAdminException
373 Set<String> ids = new HashSet<String>();
375 List<ProxyConnector> proxyConnectors = proxyConnectorAdmin.getProxyConnectorAsMap().get( managedRepoId );
377 if ( proxyConnectors == null || proxyConnectors.isEmpty() )
382 for ( ProxyConnector proxyConnector : proxyConnectors )
384 String remoteId = "remote-" + proxyConnector.getTargetRepoId();
385 IndexingContext context = indexer.getIndexingContexts().get( remoteId );
386 if ( context != null && context.isSearchable() )
396 protected List<? extends IndexCreator> getAllIndexCreators()
398 return mavenIndexerUtils.getAllIndexCreators();
402 private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits,
403 List<? extends ArtifactInfoFiler> artifactInfoFilers )
405 SearchResults results = new SearchResults();
406 Set<ArtifactInfo> artifactInfos = response.getResults();
408 for ( ArtifactInfo artifactInfo : artifactInfos )
410 String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId, artifactInfo.classifier,
411 artifactInfo.packaging );
412 Map<String, SearchResultHit> hitsMap = results.getHitsMap();
414 if ( !applyArtifactInfoFilters( artifactInfo, artifactInfoFilers, hitsMap ) )
419 SearchResultHit hit = hitsMap.get( id );
422 if ( !hit.getVersions().contains( artifactInfo.version ) )
424 hit.addVersion( artifactInfo.version );
429 hit = new SearchResultHit();
430 hit.setArtifactId( artifactInfo.artifactId );
431 hit.setGroupId( artifactInfo.groupId );
432 hit.setRepositoryId( artifactInfo.repository );
433 hit.addVersion( artifactInfo.version );
434 hit.setBundleExportPackage( artifactInfo.bundleExportPackage );
435 hit.setBundleExportService( artifactInfo.bundleExportService );
436 hit.setBundleSymbolicName( artifactInfo.bundleSymbolicName );
437 hit.setBundleVersion( artifactInfo.bundleVersion );
438 hit.setBundleDescription( artifactInfo.bundleDescription );
439 hit.setBundleDocUrl( artifactInfo.bundleDocUrl );
440 hit.setBundleRequireBundle( artifactInfo.bundleRequireBundle );
441 hit.setBundleImportPackage( artifactInfo.bundleImportPackage );
442 hit.setBundleLicense( artifactInfo.bundleLicense );
443 hit.setBundleName( artifactInfo.bundleName );
444 hit.setContext( artifactInfo.context );
445 hit.setGoals( artifactInfo.goals );
446 hit.setPrefix( artifactInfo.prefix );
447 hit.setPackaging( artifactInfo.packaging );
448 hit.setClassifier( artifactInfo.classifier );
449 hit.setUrl( getBaseUrl( artifactInfo ) );
452 results.addHit( id, hit );
455 results.setTotalHits( response.getTotalHitsCount() );
456 results.setReturnedHitsCount( response.getReturnedHitsCount() );
457 results.setLimits( limits );
459 if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
465 return paginate( results );
470 * calculate baseUrl without the context and base Archiva Url
471 * @param artifactInfo
474 protected String getBaseUrl( ArtifactInfo artifactInfo )
476 StringBuilder sb = new StringBuilder( );
478 sb.append( '/' ).append( StringUtils.replaceChars( artifactInfo.groupId, '.', '/' ) );
479 sb.append( '/' ).append( artifactInfo.artifactId );
480 sb.append( '/' ).append( artifactInfo.version );
481 sb.append( '/' ).append( artifactInfo.artifactId );
482 sb.append( '-' ).append( artifactInfo.version );
483 if ( StringUtils.isNotBlank( artifactInfo.classifier ) )
485 sb.append( '-' ).append( artifactInfo.classifier );
487 sb.append( '.' ).append( artifactInfo.packaging );
489 return sb.toString();
492 private boolean applyArtifactInfoFilters( ArtifactInfo artifactInfo,
493 List<? extends ArtifactInfoFiler> artifactInfoFilers,
494 Map<String, SearchResultHit> currentResult )
496 if ( artifactInfoFilers == null || artifactInfoFilers.isEmpty() )
501 for ( ArtifactInfoFiler filter : artifactInfoFilers )
503 if ( !filter.addArtifactInResult( artifactInfo, currentResult ) )
511 private SearchResults paginate( SearchResults results )
513 SearchResultLimits limits = results.getLimits();
514 SearchResults paginated = new SearchResults();
516 int fetchCount = limits.getPageSize();
517 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
519 if ( fetchCount > results.getTotalHits() )
521 fetchCount = results.getTotalHits();
525 if ( offset < results.getTotalHits() )
527 // only process if the offset is within the hit count.
528 for ( int i = 0; i < fetchCount; i++ )
530 // Stop fetching if we are past the total # of available hits.
531 if ( offset + i >= results.getHits().size() )
536 SearchResultHit hit = results.getHits().get( ( offset + i ) );
539 String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(),
540 hit.getPackaging() );
541 paginated.addHit( id, hit );
549 paginated.setTotalHits( results.getTotalHits() );
550 paginated.setReturnedHitsCount( paginated.getHits().size() );
551 paginated.setLimits( limits );