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)
68 public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits )
69 throws RepositorySearchException
71 addIndexingContexts( selectedRepos );
74 // 1. construct query for:
76 // - searching within search results
77 // 3. multiple repositories
79 BooleanQuery q = new BooleanQuery();
80 q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, term ), Occur.SHOULD );
81 q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, term ), Occur.SHOULD );
82 q.add( indexer.constructQuery( ArtifactInfo.VERSION, term ), Occur.SHOULD );
83 q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, term ), Occur.SHOULD );
85 // TODO: what about class & package?
89 FlatSearchRequest request = new FlatSearchRequest( q );
90 FlatSearchResponse response = indexer.searchFlat( request );
92 if( response == null || response.getTotalHits() == 0 )
94 return new SearchResults();
97 return convertToSearchResults( response, limits );
99 catch ( IndexContextInInconsistentStateException e )
101 throw new RepositorySearchException( e );
103 catch ( IOException e )
105 throw new RepositorySearchException( e );
109 Map<String, IndexingContext> indexingContexts = indexer.getIndexingContexts();
110 Set<String> keys = indexingContexts.keySet();
111 for( String key : keys )
115 indexer.removeIndexingContext( indexingContexts.get( key ), false );
116 log.debug( "Indexing context '" + key + "' removed from search." );
118 catch ( IOException e )
120 log.warn( "IOException occurred while removing indexing content '" + key + "'." );
128 * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
130 public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
131 throws RepositorySearchException
133 // TODO Auto-generated method stub
137 private void addIndexingContexts( List<String> selectedRepos )
139 for( String repo : selectedRepos )
143 Configuration config = archivaConfig.getConfiguration();
144 ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo );
146 if( repoConfig != null )
148 String indexDir = repoConfig.getIndexDir();
149 File indexDirectory = null;
150 if( indexDir != null && !"".equals( indexDir ) )
152 indexDirectory = new File( repoConfig.getIndexDir() );
156 indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
159 IndexingContext context =
160 indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ),
161 indexDirectory, null, null, NexusIndexer.FULL_INDEX );
162 context.setSearchable( repoConfig.isScanned() );
166 log.warn( "Repository '" + repo + "' not found in configuration." );
169 catch ( UnsupportedExistingLuceneIndexException e )
171 log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
174 catch ( IOException e )
176 log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
182 private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits )
184 SearchResults results = new SearchResults();
185 Set<ArtifactInfo> artifactInfos = response.getResults();
187 for ( ArtifactInfo artifactInfo : artifactInfos )
189 String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId );
190 Map<String, SearchResultHit> hitsMap = results.getHitsMap();
192 SearchResultHit hit = hitsMap.get( id );
195 hit.addVersion( artifactInfo.version );
199 hit = new SearchResultHit();
200 hit.setArtifactId( artifactInfo.artifactId );
201 hit.setGroupId( artifactInfo.groupId );
202 // do we still need to set the repository id even though we're merging everything?
203 //hit.setRepositoryId( artifactInfo.repository );
204 hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname );
205 if( !hit.getVersions().contains( artifactInfo.version ) )
207 hit.addVersion( artifactInfo.version );
211 results.addHit( id, hit );
214 results.setTotalHits( results.getHitsMap().size() );
216 if( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
222 return paginate( limits, results );
226 private SearchResults paginate( SearchResultLimits limits, SearchResults results )
228 SearchResults paginated = new SearchResults();
229 int fetchCount = limits.getPageSize();
230 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
232 if( fetchCount > results.getTotalHits() )
234 fetchCount = results.getTotalHits();
238 if ( offset < results.getTotalHits() )
240 // only process if the offset is within the hit count.
241 for ( int i = 0; i < fetchCount; i++ )
243 // Stop fetching if we are past the total # of available hits.
244 if ( offset + i > results.getTotalHits() )
249 SearchResultHit hit = results.getHits().get( ( offset + i ) );
252 String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() );
253 paginated.addHit( id, hit );
261 paginated.setTotalHits( paginated.getHitsMap().size() );