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.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
28 import org.apache.archiva.indexer.util.SearchUtil;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.lucene.search.BooleanClause.Occur;
31 import org.apache.lucene.search.BooleanQuery;
32 import org.apache.maven.index.ArtifactInfo;
33 import org.apache.maven.index.FlatSearchRequest;
34 import org.apache.maven.index.FlatSearchResponse;
35 import org.apache.maven.index.MAVEN;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.OSGI;
38 import org.apache.maven.index.context.IndexCreator;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
41 import org.apache.maven.index.expr.StringSearchExpression;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.stereotype.Service;
46 import javax.inject.Inject;
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.List;
56 * RepositorySearch implementation which uses the Nexus Indexer for searching.
58 @Service( "nexusSearch" )
59 public class NexusRepositorySearch
60 implements RepositorySearch
62 private Logger log = LoggerFactory.getLogger( getClass() );
64 private NexusIndexer indexer;
66 private ManagedRepositoryAdmin managedRepositoryAdmin;
68 private MavenIndexerUtils mavenIndexerUtils;
71 public NexusRepositorySearch( PlexusSisuBridge plexusSisuBridge, ManagedRepositoryAdmin managedRepositoryAdmin,
72 MavenIndexerUtils mavenIndexerUtils )
73 throws PlexusSisuBridgeException
75 this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
76 this.managedRepositoryAdmin = managedRepositoryAdmin;
77 this.mavenIndexerUtils = mavenIndexerUtils;
82 * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
84 public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
85 List<String> previousSearchTerms )
86 throws RepositorySearchException
88 List<String> indexingContextIds = addIndexingContexts( selectedRepos );
90 // since upgrade to nexus 2.0.0, query has changed from g:[QUERIED TERM]* to g:*[QUERIED TERM]*
91 // resulting to more wildcard searches so we need to increase max clause count
92 BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE );
93 BooleanQuery q = new BooleanQuery();
95 if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
97 constructQuery( term, q );
101 for ( String previousTerm : previousSearchTerms )
103 BooleanQuery iQuery = new BooleanQuery();
104 constructQuery( previousTerm, iQuery );
106 q.add( iQuery, Occur.MUST );
109 BooleanQuery iQuery = new BooleanQuery();
110 constructQuery( term, iQuery );
111 q.add( iQuery, Occur.MUST );
114 // we retun only artifacts without classifier in quick search, olamy cannot find a way to say with this field empty
115 // FIXME cannot find a way currently to setup this in constructQuery !!!
116 return search( limits, q, indexingContextIds, NoClassifierArtifactInfoFiler.LIST );
121 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
123 public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
124 throws RepositorySearchException
126 if ( searchFields.getRepositories() == null )
128 throw new RepositorySearchException( "Repositories cannot be null." );
131 List<String> indexingContextIds = addIndexingContexts( searchFields.getRepositories() );
133 BooleanQuery q = new BooleanQuery();
134 if ( StringUtils.isNotBlank( searchFields.getGroupId() ) )
136 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( searchFields.getGroupId() ) ),
140 if ( StringUtils.isNotBlank( searchFields.getArtifactId() ) )
143 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( searchFields.getArtifactId() ) ),
147 if ( StringUtils.isNotBlank( searchFields.getVersion() ) )
149 q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( searchFields.getVersion() ) ),
153 if ( StringUtils.isNotBlank( searchFields.getPackaging() ) )
155 q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( searchFields.getPackaging() ) ),
159 if ( StringUtils.isNotBlank( searchFields.getClassName() ) )
162 indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( searchFields.getClassName() ) ),
166 if ( StringUtils.isNotBlank( searchFields.getBundleSymbolicName() ) )
168 q.add( indexer.constructQuery( OSGI.SYMBOLIC_NAME,
169 new StringSearchExpression( searchFields.getBundleSymbolicName() ) ),
173 if ( StringUtils.isNotBlank( searchFields.getBundleVersion() ) )
176 indexer.constructQuery( OSGI.VERSION, new StringSearchExpression( searchFields.getBundleVersion() ) ),
180 if ( StringUtils.isNotBlank( searchFields.getBundleExportPackage() ) )
182 q.add( indexer.constructQuery( OSGI.EXPORT_PACKAGE,
183 new StringSearchExpression( searchFields.getBundleExportPackage() ) ),
187 if ( StringUtils.isNotBlank( searchFields.getBundleExportService() ) )
189 q.add( indexer.constructQuery( OSGI.EXPORT_SERVICE,
190 new StringSearchExpression( searchFields.getBundleExportService() ) ),
194 if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
196 q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
197 new StringSearchExpression( searchFields.getBundleImportPackage() ) ),
201 if ( StringUtils.isNotBlank( searchFields.getBundleName() ) )
203 q.add( indexer.constructQuery( OSGI.NAME, new StringSearchExpression( searchFields.getBundleName() ) ),
207 if ( StringUtils.isNotBlank( searchFields.getClassifier() ) )
210 indexer.constructQuery( MAVEN.CLASSIFIER, new StringSearchExpression( searchFields.getClassifier() ) ),
214 if ( q.getClauses() == null || q.getClauses().length <= 0 )
216 throw new RepositorySearchException( "No search fields set." );
219 return search( limits, q, indexingContextIds, Collections.<ArtifactInfoFiler>emptyList() );
222 private SearchResults search( SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds,
223 List<? extends ArtifactInfoFiler> filters )
224 throws RepositorySearchException
229 FlatSearchRequest request = new FlatSearchRequest( q );
230 request.setContexts( getIndexingContexts( indexingContextIds ) );
231 FlatSearchResponse response = indexer.searchFlat( request );
233 if ( response == null || response.getTotalHits() == 0 )
235 SearchResults results = new SearchResults();
236 results.setLimits( limits );
240 return convertToSearchResults( response, limits, filters );
242 catch ( IOException e )
244 throw new RepositorySearchException( e );
247 olamy : don't understand why this ?? it remove content from index ??
248 comment until someone explain WTF ?? :-))
251 Map<String, IndexingContext> indexingContexts = indexer.getIndexingContexts();
253 for ( Map.Entry<String, IndexingContext> entry : indexingContexts.entrySet() )
257 indexer.removeIndexingContext( entry.getValue(), false );
258 log.debug( "Indexing context '{}' removed from search.", entry.getKey() );
260 catch ( IOException e )
262 log.warn( "IOException occurred while removing indexing content '" + entry.getKey() + "'." );
269 private List<IndexingContext> getIndexingContexts( List<String> ids )
271 List<IndexingContext> contexts = new ArrayList<IndexingContext>( ids.size() );
273 for ( String id : ids )
275 IndexingContext context = indexer.getIndexingContexts().get( id );
276 if ( context != null )
278 contexts.add( context );
282 log.warn( "context with id {} not exists", id );
289 private void constructQuery( String term, BooleanQuery q )
291 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( term ) ), Occur.SHOULD );
292 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( term ) ), Occur.SHOULD );
293 q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( term ) ), Occur.SHOULD );
294 q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( term ) ), Occur.SHOULD );
295 q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( term ) ), Occur.SHOULD );
298 // new WildcardQuery( new Term( MAVEN.CLASSNAMES.getFieldName(), "*" ) );
299 //q.add( query, Occur.MUST_NOT );
300 // olamy IMHO we could set this option as at least one must match
301 //q.setMinimumNumberShouldMatch( 1 );
306 * @param selectedRepos
307 * @return indexing contextId used
309 private List<String> addIndexingContexts( List<String> selectedRepos )
311 List<String> indexingContextIds = new ArrayList<String>();
312 for ( String repo : selectedRepos )
316 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repo );
318 if ( repoConfig != null )
320 String indexDir = repoConfig.getIndexDirectory();
321 File indexDirectory = null;
322 if ( indexDir != null && !"".equals( indexDir ) )
324 indexDirectory = new File( repoConfig.getIndexDirectory() );
328 indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
331 IndexingContext context = indexer.getIndexingContexts().get( repoConfig.getId() );
332 if ( context != null )
334 // alreday here so no need to record it again
335 log.debug( "index with id {} already exists skip adding it", repoConfig.getId() );
336 // set searchable flag
337 context.setSearchable( repoConfig.isScanned() );
338 indexingContextIds.add( context.getId() );
342 context = indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(),
343 new File( repoConfig.getLocation() ), indexDirectory, null,
344 null, getAllIndexCreators() );
345 context.setSearchable( repoConfig.isScanned() );
346 if ( context.isSearchable() )
348 indexingContextIds.add( context.getId() );
352 log.warn( "indexingContext with id {} not searchable", repoConfig.getId() );
358 log.warn( "Repository '" + repo + "' not found in configuration." );
361 catch ( UnsupportedExistingLuceneIndexException e )
363 log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
366 catch ( IOException e )
368 log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
371 catch ( RepositoryAdminException e )
373 log.warn( "RepositoryAdminException occured while accessing index of repository '" + repo + "' : "
378 return indexingContextIds;
382 protected List<? extends IndexCreator> getAllIndexCreators()
384 return mavenIndexerUtils.getAllIndexCreators();
388 private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits,
389 List<? extends ArtifactInfoFiler> artifactInfoFilers )
391 SearchResults results = new SearchResults();
392 Set<ArtifactInfo> artifactInfos = response.getResults();
394 for ( ArtifactInfo artifactInfo : artifactInfos )
396 String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId, artifactInfo.classifier,
397 artifactInfo.packaging );
398 Map<String, SearchResultHit> hitsMap = results.getHitsMap();
400 if ( !applyArtifactInfoFilters( artifactInfo, artifactInfoFilers, hitsMap ) )
405 SearchResultHit hit = hitsMap.get( id );
408 if ( !hit.getVersions().contains( artifactInfo.version ) )
410 hit.addVersion( artifactInfo.version );
415 hit = new SearchResultHit();
416 hit.setArtifactId( artifactInfo.artifactId );
417 hit.setGroupId( artifactInfo.groupId );
418 hit.setRepositoryId( artifactInfo.repository );
419 // FIXME archiva url ??
420 hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname );
421 hit.addVersion( artifactInfo.version );
422 hit.setBundleExportPackage( artifactInfo.bundleExportPackage );
423 hit.setBundleExportService( artifactInfo.bundleExportService );
424 hit.setBundleSymbolicName( artifactInfo.bundleSymbolicName );
425 hit.setBundleVersion( artifactInfo.bundleVersion );
426 hit.setBundleDescription( artifactInfo.bundleDescription );
427 hit.setBundleDocUrl( artifactInfo.bundleDocUrl );
429 hit.setBundleRequireBundle( artifactInfo.bundleRequireBundle );
430 hit.setBundleImportPackage( artifactInfo.bundleImportPackage );
431 hit.setBundleLicense( artifactInfo.bundleLicense );
432 hit.setBundleName( artifactInfo.bundleName );
433 hit.setContext( artifactInfo.context );
434 hit.setGoals( artifactInfo.goals );
435 hit.setPrefix( artifactInfo.prefix );
436 hit.setPackaging( artifactInfo.packaging );
437 hit.setClassifier( artifactInfo.classifier );
439 hit.setUrl( artifactInfo.remoteUrl );
442 results.addHit( id, hit );
445 results.setTotalHits( response.getTotalHitsCount() );
446 results.setReturnedHitsCount( response.getReturnedHitsCount() );
447 results.setLimits( limits );
449 if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
455 return paginate( results );
459 private boolean applyArtifactInfoFilters( ArtifactInfo artifactInfo,
460 List<? extends ArtifactInfoFiler> artifactInfoFilers,
461 Map<String, SearchResultHit> currentResult )
463 if ( artifactInfoFilers == null || artifactInfoFilers.isEmpty() )
468 for ( ArtifactInfoFiler filter : artifactInfoFilers )
470 if ( !filter.addArtifactInResult( artifactInfo, currentResult ) )
478 private SearchResults paginate( SearchResults results )
480 SearchResultLimits limits = results.getLimits();
481 SearchResults paginated = new SearchResults();
483 int fetchCount = limits.getPageSize();
484 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
486 if ( fetchCount > results.getTotalHits() )
488 fetchCount = results.getTotalHits();
492 if ( offset < results.getTotalHits() )
494 // only process if the offset is within the hit count.
495 for ( int i = 0; i < fetchCount; i++ )
497 // Stop fetching if we are past the total # of available hits.
498 if ( offset + i >= results.getHits().size() )
503 SearchResultHit hit = results.getHits().get( ( offset + i ) );
506 String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(),
507 hit.getPackaging() );
508 paginated.addHit( id, hit );
516 paginated.setTotalHits( results.getTotalHits() );
517 paginated.setReturnedHitsCount( paginated.getHits().size() );
518 paginated.setLimits( limits );