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.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;
44 * RepositorySearch implementation which uses the Nexus Indexer for searching.
46 public class NexusRepositorySearch
47 implements RepositorySearch
49 private static final Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class );
51 private NexusIndexer indexer;
53 private ArchivaConfiguration archivaConfig;
55 public NexusRepositorySearch( NexusIndexer indexer, ArchivaConfiguration archivaConfig )
57 this.indexer = indexer;
58 this.archivaConfig = archivaConfig;
62 * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
64 public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
65 List<String> previousSearchTerms )
66 throws RepositorySearchException
68 addIndexingContexts( selectedRepos );
70 BooleanQuery q = new BooleanQuery();
71 if( previousSearchTerms == null || previousSearchTerms.isEmpty() )
73 constructQuery( term, q );
77 for( String previousTerm : previousSearchTerms )
79 BooleanQuery iQuery = new BooleanQuery();
80 constructQuery( previousTerm, iQuery );
82 q.add( iQuery, Occur.MUST );
85 BooleanQuery iQuery = new BooleanQuery();
86 constructQuery( term, iQuery );
87 q.add( iQuery, Occur.MUST );
90 return search( limits, q );
94 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
96 public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
97 throws RepositorySearchException
99 if( searchFields.getRepositories() == null )
101 throw new RepositorySearchException( "Repositories cannot be null." );
104 addIndexingContexts( searchFields.getRepositories() );
106 BooleanQuery q = new BooleanQuery();
107 if( searchFields.getGroupId() != null && !"".equals( searchFields.getGroupId() ) )
109 q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, searchFields.getGroupId() ), Occur.MUST );
112 if( searchFields.getArtifactId() != null && !"".equals( searchFields.getArtifactId() ) )
114 q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, searchFields.getArtifactId() ), Occur.MUST );
117 if( searchFields.getVersion() != null && !"".equals( searchFields.getVersion() ) )
119 q.add( indexer.constructQuery( ArtifactInfo.VERSION, searchFields.getVersion() ), Occur.MUST );
122 if( searchFields.getPackaging() != null && !"".equals( searchFields.getPackaging() ) )
124 q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, searchFields.getPackaging() ), Occur.MUST );
127 if( searchFields.getClassName() != null && !"".equals( searchFields.getClassName() ) )
129 q.add( indexer.constructQuery( ArtifactInfo.NAMES, searchFields.getClassName() ), Occur.MUST );
132 if( q.getClauses() == null || q.getClauses().length <= 0 )
134 throw new RepositorySearchException( "No search fields set." );
137 return search( limits, q );
140 private SearchResults search( SearchResultLimits limits, BooleanQuery q )
141 throws RepositorySearchException
145 FlatSearchRequest request = new FlatSearchRequest( q );
146 FlatSearchResponse response = indexer.searchFlat( request );
148 if( response == null || response.getTotalHits() == 0 )
150 SearchResults results = new SearchResults();
151 results.setLimits( limits );
155 return convertToSearchResults( response, limits );
157 catch ( IOException e )
159 throw new RepositorySearchException( e );
163 Map<String, IndexingContext> indexingContexts = indexer.getIndexingContexts();
164 Set<String> keys = indexingContexts.keySet();
165 for( String key : keys )
169 indexer.removeIndexingContext( indexingContexts.get( key ), false );
170 log.debug( "Indexing context '" + key + "' removed from search." );
172 catch ( IOException e )
174 log.warn( "IOException occurred while removing indexing content '" + key + "'." );
181 private void constructQuery( String term, BooleanQuery q )
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 );
191 private void addIndexingContexts( List<String> selectedRepos )
193 for( String repo : selectedRepos )
197 Configuration config = archivaConfig.getConfiguration();
198 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo );
200 if( repoConfig != null )
202 String indexDir = repoConfig.getIndexDir();
203 File indexDirectory = null;
204 if( indexDir != null && !"".equals( indexDir ) )
206 indexDirectory = new File( repoConfig.getIndexDir() );
210 indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
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() );
220 log.warn( "Repository '" + repo + "' not found in configuration." );
223 catch ( UnsupportedExistingLuceneIndexException e )
225 log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
228 catch ( IOException e )
230 log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
236 private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits )
238 SearchResults results = new SearchResults();
239 Set<ArtifactInfo> artifactInfos = response.getResults();
241 for ( ArtifactInfo artifactInfo : artifactInfos )
243 String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId );
244 Map<String, SearchResultHit> hitsMap = results.getHitsMap();
246 SearchResultHit hit = hitsMap.get( id );
249 hit.addVersion( artifactInfo.version );
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 ) )
261 hit.addVersion( artifactInfo.version );
265 results.addHit( id, hit );
268 results.setTotalHits( results.getHitsMap().size() );
269 results.setLimits( limits );
271 if( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
277 return paginate( results );
281 private SearchResults paginate( SearchResults results )
283 SearchResultLimits limits = results.getLimits();
284 SearchResults paginated = new SearchResults();
286 int fetchCount = limits.getPageSize();
287 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
289 if( fetchCount > results.getTotalHits() )
291 fetchCount = results.getTotalHits();
295 if ( offset < results.getTotalHits() )
297 // only process if the offset is within the hit count.
298 for ( int i = 0; i < fetchCount; i++ )
300 // Stop fetching if we are past the total # of available hits.
301 if ( offset + i >= results.getHits().size() )
306 SearchResultHit hit = results.getHits().get( ( offset + i ) );
309 String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() );
310 paginated.addHit( id, hit );
318 paginated.setTotalHits( results.getTotalHits() );
319 paginated.setLimits( limits );