1 package org.apache.maven.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 java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.queryParser.MultiFieldQueryParser;
28 import org.apache.lucene.queryParser.ParseException;
29 import org.apache.lucene.queryParser.QueryParser;
30 import org.apache.lucene.search.BooleanClause;
31 import org.apache.lucene.search.BooleanFilter;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.lucene.search.DuplicateFilter;
34 import org.apache.lucene.search.Filter;
35 import org.apache.lucene.search.FilterClause;
36 import org.apache.lucene.search.Hits;
37 import org.apache.lucene.search.MultiSearcher;
38 import org.apache.lucene.search.Query;
39 import org.apache.lucene.search.QueryWrapperFilter;
40 import org.apache.lucene.search.Searchable;
41 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
42 import org.apache.maven.archiva.configuration.ConfigurationNames;
43 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
44 import org.apache.maven.archiva.indexer.ArtifactKeys;
45 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
46 import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory;
47 import org.apache.maven.archiva.indexer.RepositoryIndexException;
48 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
49 import org.apache.maven.archiva.indexer.bytecode.BytecodeHandlers;
50 import org.apache.maven.archiva.indexer.bytecode.BytecodeKeys;
51 import org.apache.maven.archiva.indexer.filecontent.FileContentHandlers;
52 import org.apache.maven.archiva.indexer.filecontent.FileContentKeys;
53 import org.apache.maven.archiva.indexer.hashcodes.HashcodesHandlers;
54 import org.apache.maven.archiva.indexer.hashcodes.HashcodesKeys;
55 import org.apache.maven.archiva.indexer.lucene.LuceneEntryConverter;
56 import org.apache.maven.archiva.indexer.lucene.LuceneQuery;
57 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
58 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
59 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
60 import org.codehaus.plexus.registry.Registry;
61 import org.codehaus.plexus.registry.RegistryListener;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * DefaultCrossRepositorySearch
69 * @plexus.component role="org.apache.maven.archiva.indexer.search.CrossRepositorySearch" role-hint="default"
71 public class DefaultCrossRepositorySearch
72 implements CrossRepositorySearch, RegistryListener, Initializable
74 private Logger log = LoggerFactory.getLogger( DefaultCrossRepositorySearch.class );
77 * @plexus.requirement role-hint="lucene"
79 private RepositoryContentIndexFactory indexFactory;
84 private ArchivaConfiguration configuration;
86 private final List<ManagedRepositoryConfiguration> localIndexedRepositories = new ArrayList<ManagedRepositoryConfiguration>();
88 public SearchResults executeFilteredSearch( String principal, List<String> selectedRepos, String groupId,
89 String artifactId, String version, String className,
90 SearchResultLimits limits )
92 List<RepositoryContentIndex> indexes = getBytecodeIndexes( principal, selectedRepos );
93 SearchResults results = new SearchResults();
94 List<String> fieldsList = new ArrayList<String>();
95 List<String> termsList = new ArrayList<String>();
96 List<BooleanClause.Occur> flagsList = new ArrayList<BooleanClause.Occur>();
98 if( groupId != null && !"".equals( groupId.trim() ) )
100 fieldsList.add( ArtifactKeys.GROUPID );
101 termsList.add( groupId );
102 flagsList.add( BooleanClause.Occur.MUST );
105 if( artifactId != null && !"".equals( artifactId.trim() ) )
107 fieldsList.add( ArtifactKeys.ARTIFACTID );
108 termsList.add( artifactId );
109 flagsList.add( BooleanClause.Occur.MUST );
112 if( version != null && !"".equals( version.trim() ) )
114 fieldsList.add( ArtifactKeys.VERSION );
115 termsList.add( version );
116 flagsList.add( BooleanClause.Occur.MUST );
119 if( className != null && !"".equals( className.trim() ) )
121 fieldsList.add( BytecodeKeys.CLASSES );
122 fieldsList.add( BytecodeKeys.FILES );
123 fieldsList.add( BytecodeKeys.METHODS );
124 termsList.add( className.trim() );
125 termsList.add( className.trim() );
126 termsList.add( className.trim() );
127 flagsList.add( BooleanClause.Occur.SHOULD );
128 flagsList.add( BooleanClause.Occur.SHOULD );
129 flagsList.add( BooleanClause.Occur.SHOULD );
134 String[] fieldsArr = new String[ fieldsList.size() ];
135 String[] queryArr = new String[ termsList.size() ];
136 BooleanClause.Occur[] flagsArr = new BooleanClause.Occur[ flagsList.size() ];
139 MultiFieldQueryParser.parse( termsList.toArray( queryArr ), fieldsList.toArray( fieldsArr ),
140 flagsList.toArray( flagsArr ), new BytecodeHandlers().getAnalyzer() );
142 LuceneQuery query = new LuceneQuery( fieldsQuery );
143 results = searchAll( query, limits, indexes, null );
144 results.getRepositories().add( this.localIndexedRepositories );
146 catch ( ParseException e )
148 log.warn( "Unable to parse advanced search fields and query terms." );
154 public SearchResults searchForChecksum( String principal, List<String> selectedRepos, String checksum,
155 SearchResultLimits limits )
157 List<RepositoryContentIndex> indexes = getHashcodeIndexes( principal, selectedRepos );
161 QueryParser parser = new MultiFieldQueryParser( new String[]{HashcodesKeys.MD5, HashcodesKeys.SHA1},
162 new HashcodesHandlers().getAnalyzer() );
163 LuceneQuery query = new LuceneQuery( parser.parse( checksum ) );
164 SearchResults results = searchAll( query, limits, indexes, null );
165 results.getRepositories().addAll( this.localIndexedRepositories );
169 catch ( ParseException e )
171 log.warn( "Unable to parse query [" + checksum + "]: " + e.getMessage(), e );
175 return new SearchResults();
178 public SearchResults searchForBytecode( String principal, List<String> selectedRepos, String term, SearchResultLimits limits )
180 List<RepositoryContentIndex> indexes = getBytecodeIndexes( principal, selectedRepos );
184 QueryParser parser = new BytecodeHandlers().getQueryParser();
185 LuceneQuery query = new LuceneQuery( parser.parse( term ) );
186 SearchResults results = searchAll( query, limits, indexes, null );
187 results.getRepositories().addAll( this.localIndexedRepositories );
191 catch ( ParseException e )
193 log.warn( "Unable to parse query [" + term + "]: " + e.getMessage(), e );
197 return new SearchResults();
200 public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term, SearchResultLimits limits )
202 return searchForTerm( principal, selectedRepos, term, limits, null );
205 public SearchResults searchForTerm( String principal, List<String> selectedRepos, String term,
206 SearchResultLimits limits, List<String> previousSearchTerms )
208 List<RepositoryContentIndex> indexes = getFileContentIndexes( principal, selectedRepos );
212 QueryParser parser = new FileContentHandlers().getQueryParser();
213 LuceneQuery query = null;
214 SearchResults results = null;
215 if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
217 query = new LuceneQuery( parser.parse( term ) );
218 results = searchAll( query, limits, indexes, null );
222 // AND the previous search terms
223 BooleanQuery booleanQuery = new BooleanQuery();
224 for ( String previousSearchTerm : previousSearchTerms )
226 booleanQuery.add( parser.parse( previousSearchTerm ), BooleanClause.Occur.MUST );
229 query = new LuceneQuery( booleanQuery );
230 Filter filter = new QueryWrapperFilter( parser.parse( term ) );
231 results = searchAll( query, limits, indexes, filter );
233 results.getRepositories().addAll( this.localIndexedRepositories );
237 catch ( ParseException e )
239 log.warn( "Unable to parse query [" + term + "]: " + e.getMessage(), e );
243 return new SearchResults();
246 private SearchResults searchAll( LuceneQuery luceneQuery, SearchResultLimits limits, List<RepositoryContentIndex> indexes, Filter filter )
248 org.apache.lucene.search.Query specificQuery = luceneQuery.getLuceneQuery();
250 SearchResults results = new SearchResults();
252 if ( indexes.isEmpty() )
254 // No point going any further.
258 // Setup the converter
259 LuceneEntryConverter converter = null;
260 RepositoryContentIndex index = indexes.get( 0 );
261 converter = index.getEntryConverter();
263 // Process indexes into an array of Searchables.
264 List<Searchable> searchableList = toSearchables( indexes );
266 Searchable searchables[] = new Searchable[searchableList.size()];
267 searchableList.toArray( searchables );
269 MultiSearcher searcher = null;
273 // Create a multi-searcher for looking up the information.
274 searcher = new MultiSearcher( searchables );
276 BooleanFilter booleanFilter = new BooleanFilter();
277 DuplicateFilter artifactIdDuplicateFilter = new DuplicateFilter(FileContentKeys.ARTIFACTID_EXACT);
278 booleanFilter.add(new FilterClause(artifactIdDuplicateFilter, BooleanClause.Occur.MUST));
279 DuplicateFilter groupIdDuplicateFilter = new DuplicateFilter(FileContentKeys.GROUPID_EXACT);
280 booleanFilter.add(new FilterClause(groupIdDuplicateFilter, BooleanClause.Occur.MUST));
282 // Perform the search.
284 if ( filter != null )
286 booleanFilter.add(new FilterClause(filter, BooleanClause.Occur.MUST));
287 hits = searcher.search( specificQuery, booleanFilter );
291 hits = searcher.search( specificQuery, booleanFilter );
294 int hitCount = hits.length();
296 // Now process the limits.
297 results.setLimits( limits );
298 results.setTotalHits( hitCount );
300 int fetchCount = limits.getPageSize();
301 int offset = ( limits.getSelectedPage() * limits.getPageSize() );
303 if ( limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
305 fetchCount = hitCount;
310 if ( offset < hitCount )
312 // only process if the offset is within the hit count.
313 for ( int i = 0; i < fetchCount; i++ )
315 // Stop fetching if we are past the total # of available hits.
316 if ( offset + i >= hitCount )
323 Document doc = hits.doc( offset + i );
324 LuceneRepositoryContentRecord record = converter.convert( doc );
325 results.addHit( record );
327 catch ( java.text.ParseException e )
329 log.warn( "Unable to parse document into record: " + e.getMessage(), e );
335 catch ( IOException e )
337 log.error( "Unable to setup multi-search: " + e.getMessage(), e );
343 if ( searcher != null )
348 catch ( IOException ie )
350 log.error( "Unable to close index searcher: " + ie.getMessage(), ie );
357 private List<Searchable> toSearchables( List<RepositoryContentIndex> indexes )
359 List<Searchable> searchableList = new ArrayList<Searchable>();
360 for ( RepositoryContentIndex contentIndex : indexes )
364 searchableList.add( contentIndex.getSearchable() );
366 catch ( RepositoryIndexSearchException e )
368 log.warn( "Unable to get searchable for index [" + contentIndex.getId() + "] :"
369 + e.getMessage(), e );
372 return searchableList;
375 public List<RepositoryContentIndex> getBytecodeIndexes( String principal, List<String> selectedRepos )
377 List<RepositoryContentIndex> ret = new ArrayList<RepositoryContentIndex>();
379 for ( ManagedRepositoryConfiguration repoConfig : localIndexedRepositories )
381 // Only used selected repo
382 if ( selectedRepos.contains( repoConfig.getId() ) )
384 RepositoryContentIndex index = indexFactory.createBytecodeIndex( repoConfig );
386 if ( indexExists( index ) )
396 public List<RepositoryContentIndex> getFileContentIndexes( String principal, List<String> selectedRepos )
398 List<RepositoryContentIndex> ret = new ArrayList<RepositoryContentIndex>();
400 for ( ManagedRepositoryConfiguration repoConfig : localIndexedRepositories )
402 // Only used selected repo
403 if ( selectedRepos.contains( repoConfig.getId() ) )
405 RepositoryContentIndex index = indexFactory.createFileContentIndex( repoConfig );
407 if ( indexExists( index ) )
417 public List<RepositoryContentIndex> getHashcodeIndexes( String principal, List<String> selectedRepos )
419 List<RepositoryContentIndex> ret = new ArrayList<RepositoryContentIndex>();
421 for ( ManagedRepositoryConfiguration repoConfig : localIndexedRepositories )
423 // Only used selected repo
424 if ( selectedRepos.contains( repoConfig.getId() ) )
426 RepositoryContentIndex index = indexFactory.createHashcodeIndex( repoConfig );
428 if ( indexExists( index ) )
438 private boolean indexExists( RepositoryContentIndex index )
442 return index.exists();
444 catch ( RepositoryIndexException e )
447 "Repository Content Index [" + index.getId() + "] for repository ["
448 + index.getRepository().getId() + "] does not exist yet in ["
449 + index.getIndexDirectory().getAbsolutePath() + "]." );
454 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
456 if ( ConfigurationNames.isManagedRepositories( propertyName ) )
462 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
464 /* Nothing to do here */
467 private void initRepositories()
469 synchronized ( this.localIndexedRepositories )
471 this.localIndexedRepositories.clear();
473 List<ManagedRepositoryConfiguration> repos = configuration.getConfiguration().getManagedRepositories();
474 for ( ManagedRepositoryConfiguration repo : repos )
476 if ( repo.isScanned() )
478 localIndexedRepositories.add( repo );
484 public void initialize()
485 throws InitializationException
488 configuration.addChangeListener( this );