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.lucene.search.BooleanQuery;
29 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.indexer.search.SearchResultHit;
32 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
33 import org.apache.maven.archiva.indexer.search.SearchResults;
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.IndexContextInInconsistentStateException;
41 import org.sonatype.nexus.index.context.IndexingContext;
42 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
45 * RepositorySearch implementation which uses the Nexus Indexer for searching.
47 public class NexusRepositorySearch
48 implements RepositorySearch
50 private static final Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class );
52 private NexusIndexer indexer;
54 private ArchivaConfiguration archivaConfig;
56 public NexusRepositorySearch( NexusIndexer indexer, ArchivaConfiguration archivaConfig )
58 this.indexer = indexer;
59 this.archivaConfig = archivaConfig;
62 public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits )
63 throws RepositorySearchException
65 addIndexingContexts( selectedRepos );
68 // 1. construct query for:
70 // - searching within search results
71 // 2. consider pagination
73 BooleanQuery q = new BooleanQuery();
74 // q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
75 // q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
79 FlatSearchRequest request = new FlatSearchRequest( q );
80 FlatSearchResponse response = indexer.searchFlat( request );
82 return convertToSearchResults( response );
84 catch ( IndexContextInInconsistentStateException e )
86 throw new RepositorySearchException( e );
88 catch ( IOException e )
90 throw new RepositorySearchException( e );
94 public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
95 throws RepositorySearchException
97 // TODO Auto-generated method stub
101 private void addIndexingContexts( List<String> selectedRepos )
103 for( String repo : selectedRepos )
107 ManagedRepositoryConfiguration repoConfig = archivaConfig.getConfiguration().findManagedRepositoryById( repo );
108 String indexDir = repoConfig.getIndexDir();
109 File indexDirectory = null;
110 if( indexDir != null && !"".equals( indexDir ) )
112 indexDirectory = new File( repoConfig.getLocation(), repoConfig.getIndexDir() );
116 indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
119 IndexingContext context =
120 indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ),
121 indexDirectory, null, null, NexusIndexer.FULL_INDEX );
122 context.setSearchable( repoConfig.isScanned() );
124 catch ( UnsupportedExistingLuceneIndexException e )
127 log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
130 catch ( IOException e )
133 log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
139 private SearchResults convertToSearchResults( FlatSearchResponse response )
141 SearchResults results = new SearchResults();
142 Set<ArtifactInfo> artifactInfos = response.getResults();
144 for ( ArtifactInfo artifactInfo : artifactInfos )
146 String id = artifactInfo.groupId + ":" + artifactInfo.artifactId;
147 Map<String, SearchResultHit> hitsMap = results.getHitsMap();
149 SearchResultHit hit = hitsMap.get( id );
152 hit.addVersion( artifactInfo.version );
156 hit = new SearchResultHit();
157 hit.setArtifactId( artifactInfo.artifactId );
158 hit.setGroupId( artifactInfo.groupId );
159 hit.setRepositoryId( artifactInfo.repository );
160 hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname );
161 hit.addVersion( artifactInfo.version );
164 results.addHit( id, hit );