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.PlexusSisuBridgeException;
28 import org.apache.archiva.indexer.util.SearchUtil;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.index.ArtifactInfo;
31 import org.apache.maven.index.FlatSearchRequest;
32 import org.apache.maven.index.FlatSearchResponse;
33 import org.apache.maven.index.MAVEN;
34 import org.apache.maven.index.NexusIndexer;
35 import org.apache.maven.index.OSGI;
36 import org.apache.maven.index.QueryCreator;
37 import org.apache.maven.index.SearchType;
38 import org.apache.maven.index.context.IndexCreator;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.expr.SearchExpression;
41 import org.apache.maven.index.expr.SearchTyped;
42 import org.apache.maven.index.expr.SourcedSearchExpression;
43 import org.apache.maven.index.expr.UserInputSearchExpression;
44 import org.apache.maven.index.shaded.lucene.search.BooleanClause;
45 import org.apache.maven.index.shaded.lucene.search.BooleanClause.Occur;
46 import org.apache.maven.index.shaded.lucene.search.BooleanQuery;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Service;
51 import javax.inject.Inject;
52 import java.io.IOException;
53 import java.util.ArrayList;
54 import java.util.Collection;
55 import java.util.Collections;
56 import java.util.HashSet;
57 import java.util.List;
62 * RepositorySearch implementation which uses the Maven Indexer for searching.
64 @Service( "repositorySearch#maven" )
65 public class MavenRepositorySearch
66 implements RepositorySearch
68 private Logger log = LoggerFactory.getLogger( getClass() );
70 private NexusIndexer indexer;
72 private QueryCreator queryCreator;
74 private ManagedRepositoryAdmin managedRepositoryAdmin;
76 private ProxyConnectorAdmin proxyConnectorAdmin;
78 protected MavenRepositorySearch()
84 public MavenRepositorySearch( NexusIndexer nexusIndexer, ManagedRepositoryAdmin managedRepositoryAdmin,
85 ProxyConnectorAdmin proxyConnectorAdmin, QueryCreator queryCreator )
86 throws PlexusSisuBridgeException
88 this.indexer = nexusIndexer;
89 this.queryCreator = queryCreator;
90 this.managedRepositoryAdmin = managedRepositoryAdmin;
91 this.proxyConnectorAdmin = proxyConnectorAdmin;
95 * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
98 public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
99 List<String> previousSearchTerms )
100 throws RepositorySearchException
102 List<String> indexingContextIds = addIndexingContexts( selectedRepos );
104 // since upgrade to nexus 2.0.0, query has changed from g:[QUERIED TERM]* to g:*[QUERIED TERM]*
105 // resulting to more wildcard searches so we need to increase max clause count
106 BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE );
107 BooleanQuery q = new BooleanQuery();
109 if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
111 constructQuery( term, q );
115 for ( String previousTerm : previousSearchTerms )
117 BooleanQuery iQuery = new BooleanQuery();
118 constructQuery( previousTerm, iQuery );
120 q.add( iQuery, BooleanClause.Occur.MUST );
123 BooleanQuery iQuery = new BooleanQuery();
124 constructQuery( term, iQuery );
125 q.add( iQuery, BooleanClause.Occur.MUST );
128 // we retun only artifacts without classifier in quick search, olamy cannot find a way to say with this field empty
129 // FIXME cannot find a way currently to setup this in constructQuery !!!
130 return search( limits, q, indexingContextIds, NoClassifierArtifactInfoFilter.LIST, selectedRepos, true );
135 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
138 public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
139 throws RepositorySearchException
141 if ( searchFields.getRepositories() == null )
143 throw new RepositorySearchException( "Repositories cannot be null." );
146 List<String> indexingContextIds = addIndexingContexts( searchFields.getRepositories() );
148 // if no index found in the specified ones return an empty search result instead of doing a search on all index
149 // olamy: IMHO doesn't make sense
150 if ( !searchFields.getRepositories().isEmpty() && ( indexingContextIds == null
151 || indexingContextIds.isEmpty() ) )
153 return new SearchResults();
156 BooleanQuery q = new BooleanQuery();
157 if ( StringUtils.isNotBlank( searchFields.getGroupId() ) )
159 q.add( indexer.constructQuery( MAVEN.GROUP_ID, searchFields.isExactSearch() ? new SourcedSearchExpression(
160 searchFields.getGroupId() ) : new UserInputSearchExpression( searchFields.getGroupId() ) ),
161 BooleanClause.Occur.MUST );
164 if ( StringUtils.isNotBlank( searchFields.getArtifactId() ) )
166 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
167 searchFields.isExactSearch()
168 ? new SourcedSearchExpression( searchFields.getArtifactId() )
169 : new UserInputSearchExpression( searchFields.getArtifactId() ) ),
170 BooleanClause.Occur.MUST );
173 if ( StringUtils.isNotBlank( searchFields.getVersion() ) )
175 q.add( indexer.constructQuery( MAVEN.VERSION, searchFields.isExactSearch() ? new SourcedSearchExpression(
176 searchFields.getVersion() ) : new SourcedSearchExpression( searchFields.getVersion() ) ),
177 BooleanClause.Occur.MUST );
180 if ( StringUtils.isNotBlank( searchFields.getPackaging() ) )
182 q.add( indexer.constructQuery( MAVEN.PACKAGING, searchFields.isExactSearch() ? new SourcedSearchExpression(
183 searchFields.getPackaging() ) : new UserInputSearchExpression( searchFields.getPackaging() ) ),
184 BooleanClause.Occur.MUST );
187 if ( StringUtils.isNotBlank( searchFields.getClassName() ) )
189 q.add( indexer.constructQuery( MAVEN.CLASSNAMES,
190 new UserInputSearchExpression( searchFields.getClassName() ) ),
191 BooleanClause.Occur.MUST );
194 if ( StringUtils.isNotBlank( searchFields.getBundleSymbolicName() ) )
196 q.add( indexer.constructQuery( OSGI.SYMBOLIC_NAME,
197 new UserInputSearchExpression( searchFields.getBundleSymbolicName() ) ),
198 BooleanClause.Occur.MUST );
201 if ( StringUtils.isNotBlank( searchFields.getBundleVersion() ) )
203 q.add( indexer.constructQuery( OSGI.VERSION,
204 new UserInputSearchExpression( searchFields.getBundleVersion() ) ),
205 BooleanClause.Occur.MUST );
208 if ( StringUtils.isNotBlank( searchFields.getBundleExportPackage() ) )
210 q.add( indexer.constructQuery( OSGI.EXPORT_PACKAGE,
211 new UserInputSearchExpression( searchFields.getBundleExportPackage() ) ),
215 if ( StringUtils.isNotBlank( searchFields.getBundleExportService() ) )
217 q.add( indexer.constructQuery( OSGI.EXPORT_SERVICE,
218 new UserInputSearchExpression( searchFields.getBundleExportService() ) ),
222 if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
224 q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
225 new UserInputSearchExpression( searchFields.getBundleImportPackage() ) ),
229 if ( StringUtils.isNotBlank( searchFields.getBundleName() ) )
231 q.add( indexer.constructQuery( OSGI.NAME, new UserInputSearchExpression( searchFields.getBundleName() ) ),
235 if ( StringUtils.isNotBlank( searchFields.getBundleImportPackage() ) )
237 q.add( indexer.constructQuery( OSGI.IMPORT_PACKAGE,
238 new UserInputSearchExpression( searchFields.getBundleImportPackage() ) ),
242 if ( StringUtils.isNotBlank( searchFields.getBundleRequireBundle() ) )
244 q.add( indexer.constructQuery( OSGI.REQUIRE_BUNDLE,
245 new UserInputSearchExpression( searchFields.getBundleRequireBundle() ) ),
249 if ( StringUtils.isNotBlank( searchFields.getClassifier() ) )
251 q.add( indexer.constructQuery( MAVEN.CLASSIFIER, searchFields.isExactSearch() ? new SourcedSearchExpression(
252 searchFields.getClassifier() ) : new UserInputSearchExpression( searchFields.getClassifier() ) ),
255 else if ( searchFields.isExactSearch() )
257 //TODO improvement in case of exact search and no classifier we must query for classifier with null value
258 // currently it's done in DefaultSearchService with some filtering
261 if ( q.getClauses() == null || q.getClauses().length <= 0 )
263 throw new RepositorySearchException( "No search fields set." );
266 return search( limits, q, indexingContextIds, Collections.<ArtifactInfoFilter>emptyList(),
267 searchFields.getRepositories(), searchFields.isIncludePomArtifacts() );
270 private static class NullSearch
271 implements SearchTyped, SearchExpression
273 private static final NullSearch INSTANCE = new NullSearch();
276 public String getStringValue()
278 return "[[NULL_VALUE]]";
282 public SearchType getSearchType()
284 return SearchType.EXACT;
288 private SearchResults search( SearchResultLimits limits, BooleanQuery q, List<String> indexingContextIds,
289 List<? extends ArtifactInfoFilter> filters, List<String> selectedRepos,
290 boolean includePoms )
291 throws RepositorySearchException
296 FlatSearchRequest request = new FlatSearchRequest( q );
298 request.setContexts( getIndexingContexts( indexingContextIds ) );
299 if ( limits != null )
301 // we apply limits only when first page asked
302 if ( limits.getSelectedPage() == 0 )
304 request.setCount( limits.getPageSize() * ( Math.max( 1, limits.getSelectedPage() ) ) );
308 FlatSearchResponse response = indexer.searchFlat( request );
310 if ( response == null || response.getTotalHits() == 0 )
312 SearchResults results = new SearchResults();
313 results.setLimits( limits );
317 return convertToSearchResults( response, limits, filters, selectedRepos, includePoms );
319 catch ( IOException e )
321 throw new RepositorySearchException( e.getMessage(), e );
323 catch ( RepositoryAdminException e )
325 throw new RepositorySearchException( e.getMessage(), e );
330 private List<IndexingContext> getIndexingContexts( List<String> ids )
332 List<IndexingContext> contexts = new ArrayList<>( ids.size() );
334 for ( String id : ids )
336 IndexingContext context = indexer.getIndexingContexts().get( id );
337 if ( context != null )
339 contexts.add( context );
343 log.warn( "context with id {} not exists", id );
350 private void constructQuery( String term, BooleanQuery q )
352 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ), Occur.SHOULD );
353 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new UserInputSearchExpression( term ) ), Occur.SHOULD );
354 q.add( indexer.constructQuery( MAVEN.VERSION, new UserInputSearchExpression( term ) ), Occur.SHOULD );
355 q.add( indexer.constructQuery( MAVEN.PACKAGING, new UserInputSearchExpression( term ) ), Occur.SHOULD );
356 q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new UserInputSearchExpression( term ) ), Occur.SHOULD );
359 // new WildcardQuery( new Term( MAVEN.CLASSNAMES.getFieldName(), "*" ) );
360 //q.add( query, Occur.MUST_NOT );
361 // olamy IMHO we could set this option as at least one must match
362 //q.setMinimumNumberShouldMatch( 1 );
367 * @param selectedRepos
368 * @return indexing contextId used
370 private List<String> addIndexingContexts( List<String> selectedRepos )
372 Set<String> indexingContextIds = new HashSet<>();
373 for ( String repo : selectedRepos )
377 ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repo );
379 if ( repoConfig != null )
382 IndexingContext context = managedRepositoryAdmin.createIndexContext( repoConfig );
383 if ( context.isSearchable() )
385 indexingContextIds.addAll( getRemoteIndexingContextIds( repo ) );
386 indexingContextIds.add( context.getId() );
390 log.warn( "indexingContext with id {} not searchable", repoConfig.getId() );
396 log.warn( "Repository '{}' not found in configuration.", repo );
399 catch ( RepositoryAdminException e )
401 log.warn( "RepositoryAdminException occured while accessing index of repository '{}' : {}", repo,
407 return new ArrayList<>( indexingContextIds );
412 public Set<String> getRemoteIndexingContextIds( String managedRepoId )
413 throws RepositoryAdminException
415 Set<String> ids = new HashSet<>();
417 List<ProxyConnector> proxyConnectors = proxyConnectorAdmin.getProxyConnectorAsMap().get( managedRepoId );
419 if ( proxyConnectors == null || proxyConnectors.isEmpty() )
424 for ( ProxyConnector proxyConnector : proxyConnectors )
426 String remoteId = "remote-" + proxyConnector.getTargetRepoId();
427 IndexingContext context = indexer.getIndexingContexts().get( remoteId );
428 if ( context != null && context.isSearchable() )
438 public Collection<String> getAllGroupIds( String principal, List<String> selectedRepos )
439 throws RepositorySearchException
441 List<IndexingContext> indexContexts = getIndexingContexts( selectedRepos );
443 if ( indexContexts == null || indexContexts.isEmpty() )
445 return Collections.emptyList();
450 Set<String> allGroupIds = new HashSet<>();
451 for ( IndexingContext indexingContext : indexContexts )
453 allGroupIds.addAll( indexingContext.getAllGroups() );
457 catch ( IOException e )
459 throw new RepositorySearchException( e.getMessage(), e );
464 private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits,
465 List<? extends ArtifactInfoFilter> artifactInfoFilters,
466 List<String> selectedRepos, boolean includePoms )
467 throws RepositoryAdminException
469 SearchResults results = new SearchResults();
470 Set<ArtifactInfo> artifactInfos = response.getResults();
472 for ( ArtifactInfo artifactInfo : artifactInfos )
474 if ( StringUtils.equalsIgnoreCase( "pom", artifactInfo.getFileExtension() ) && !includePoms )
478 String id = SearchUtil.getHitId( artifactInfo.getGroupId(), //
479 artifactInfo.getArtifactId(), //
480 artifactInfo.getClassifier(), //
481 artifactInfo.getPackaging() );
482 Map<String, SearchResultHit> hitsMap = results.getHitsMap();
484 if ( !applyArtifactInfoFilters( artifactInfo, artifactInfoFilters, hitsMap ) )
489 SearchResultHit hit = hitsMap.get( id );
492 if ( !hit.getVersions().contains( artifactInfo.getVersion() ) )
494 hit.addVersion( artifactInfo.getVersion() );
499 hit = new SearchResultHit();
500 hit.setArtifactId( artifactInfo.getArtifactId() );
501 hit.setGroupId( artifactInfo.getGroupId() );
502 hit.setRepositoryId( artifactInfo.getRepository() );
503 hit.addVersion( artifactInfo.getVersion() );
504 hit.setBundleExportPackage( artifactInfo.getBundleExportPackage() );
505 hit.setBundleExportService( artifactInfo.getBundleExportService() );
506 hit.setBundleSymbolicName( artifactInfo.getBundleSymbolicName() );
507 hit.setBundleVersion( artifactInfo.getBundleVersion() );
508 hit.setBundleDescription( artifactInfo.getBundleDescription() );
509 hit.setBundleDocUrl( artifactInfo.getBundleDocUrl() );
510 hit.setBundleRequireBundle( artifactInfo.getBundleRequireBundle() );
511 hit.setBundleImportPackage( artifactInfo.getBundleImportPackage() );
512 hit.setBundleLicense( artifactInfo.getBundleLicense() );
513 hit.setBundleName( artifactInfo.getBundleName() );
514 hit.setContext( artifactInfo.getContext() );
515 hit.setGoals( artifactInfo.getGoals() );
516 hit.setPrefix( artifactInfo.getPrefix() );
517 hit.setPackaging( artifactInfo.getPackaging() );
518 hit.setClassifier( artifactInfo.getClassifier() );
519 hit.setFileExtension( artifactInfo.getFileExtension() );
520 hit.setUrl( getBaseUrl( artifactInfo, selectedRepos ) );
523 results.addHit( id, hit );
526 results.setTotalHits( response.getTotalHitsCount() );
527 results.setTotalHitsMapSize( results.getHitsMap().values().size() );
528 results.setReturnedHitsCount( response.getReturnedHitsCount() );
529 results.setLimits( limits );
531 if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
537 return paginate( results );
542 * calculate baseUrl without the context and base Archiva Url
544 * @param artifactInfo
547 protected String getBaseUrl( ArtifactInfo artifactInfo, List<String> selectedRepos )
548 throws RepositoryAdminException
550 StringBuilder sb = new StringBuilder();
551 if ( StringUtils.startsWith( artifactInfo.getContext(), "remote-" ) )
553 // it's a remote index result we search a managed which proxying this remote and on which
554 // current user has read karma
555 String managedRepoId =
556 getManagedRepoId( StringUtils.substringAfter( artifactInfo.getContext(), "remote-" ), selectedRepos );
557 if ( managedRepoId != null )
559 sb.append( '/' ).append( managedRepoId );
560 artifactInfo.setContext( managedRepoId );
565 sb.append( '/' ).append( artifactInfo.getContext() );
568 sb.append( '/' ).append( StringUtils.replaceChars( artifactInfo.getGroupId(), '.', '/' ) );
569 sb.append( '/' ).append( artifactInfo.getArtifactId() );
570 sb.append( '/' ).append( artifactInfo.getVersion() );
571 sb.append( '/' ).append( artifactInfo.getArtifactId() );
572 sb.append( '-' ).append( artifactInfo.getVersion() );
573 if ( StringUtils.isNotBlank( artifactInfo.getClassifier() ) )
575 sb.append( '-' ).append( artifactInfo.getClassifier() );
577 // maven-plugin packaging is a jar
578 if ( StringUtils.equals( "maven-plugin", artifactInfo.getPackaging() ) )
584 sb.append( '.' ).append( artifactInfo.getPackaging() );
587 return sb.toString();
591 * return a managed repo for a remote result
594 * @param selectedRepos
596 * @throws RepositoryAdminException
598 private String getManagedRepoId( String remoteRepo, List<String> selectedRepos )
599 throws RepositoryAdminException
601 Map<String, List<ProxyConnector>> proxyConnectorMap = proxyConnectorAdmin.getProxyConnectorAsMap();
602 if ( proxyConnectorMap == null || proxyConnectorMap.isEmpty() )
606 if ( selectedRepos != null && !selectedRepos.isEmpty() )
608 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
610 if ( selectedRepos.contains( entry.getKey() ) )
612 for ( ProxyConnector proxyConnector : entry.getValue() )
614 if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
616 return proxyConnector.getSourceRepoId();
623 // we don't find in search selected repos so return the first one
624 for ( Map.Entry<String, List<ProxyConnector>> entry : proxyConnectorMap.entrySet() )
627 for ( ProxyConnector proxyConnector : entry.getValue() )
629 if ( StringUtils.equals( remoteRepo, proxyConnector.getTargetRepoId() ) )
631 return proxyConnector.getSourceRepoId();
639 private boolean applyArtifactInfoFilters( ArtifactInfo artifactInfo,
640 List<? extends ArtifactInfoFilter> artifactInfoFilters,
641 Map<String, SearchResultHit> currentResult )
643 if ( artifactInfoFilters == null || artifactInfoFilters.isEmpty() )
648 for ( ArtifactInfoFilter filter : artifactInfoFilters )
650 if ( !filter.addArtifactInResult( artifactInfo, currentResult ) )
658 protected SearchResults paginate( SearchResults results )
660 SearchResultLimits limits = results.getLimits();
661 SearchResults paginated = new SearchResults();
663 // ( limits.getPageSize() * ( Math.max( 1, limits.getSelectedPage() ) ) );
665 int fetchCount = limits.getPageSize();
666 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
668 if ( fetchCount > results.getTotalHits() )
670 fetchCount = results.getTotalHits();
674 if ( offset < results.getTotalHits() )
676 // only process if the offset is within the hit count.
677 for ( int i = 0; i < fetchCount; i++ )
679 // Stop fetching if we are past the total # of available hits.
680 if ( offset + i >= results.getHits().size() )
685 SearchResultHit hit = results.getHits().get( ( offset + i ) );
688 String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId(), hit.getClassifier(),
689 hit.getPackaging() );
690 paginated.addHit( id, hit );
698 paginated.setTotalHits( results.getTotalHits() );
699 paginated.setReturnedHitsCount( paginated.getHits().size() );
700 paginated.setTotalHitsMapSize( results.getTotalHitsMapSize() );
701 paginated.setLimits( limits );