You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NexusRepositorySearch.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package org.apache.archiva.indexer.search;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
  21. import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
  22. import org.apache.archiva.indexer.util.SearchUtil;
  23. import org.apache.lucene.search.BooleanClause.Occur;
  24. import org.apache.lucene.search.BooleanQuery;
  25. import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil;
  26. import org.apache.maven.archiva.configuration.ArchivaConfiguration;
  27. import org.apache.maven.archiva.configuration.Configuration;
  28. import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
  29. import org.apache.maven.index.ArtifactInfo;
  30. import org.apache.maven.index.Field;
  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.context.IndexingContext;
  36. import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
  37. import org.apache.maven.index.expr.StringSearchExpression;
  38. import org.slf4j.Logger;
  39. import org.slf4j.LoggerFactory;
  40. import org.springframework.context.annotation.Scope;
  41. import org.springframework.stereotype.Service;
  42. import javax.inject.Inject;
  43. import java.io.File;
  44. import java.io.IOException;
  45. import java.util.List;
  46. import java.util.Map;
  47. import java.util.Set;
  48. /**
  49. * RepositorySearch implementation which uses the Nexus Indexer for searching.
  50. */
  51. @Service( "nexusSearch" )
  52. @Scope("prototype")
  53. public class NexusRepositorySearch
  54. implements RepositorySearch
  55. {
  56. private Logger log = LoggerFactory.getLogger( NexusRepositorySearch.class );
  57. private NexusIndexer indexer;
  58. private ArchivaConfiguration archivaConfig;
  59. @Inject
  60. public NexusRepositorySearch( PlexusSisuBridge plexusSisuBridge, ArchivaConfiguration archivaConfig )
  61. throws PlexusSisuBridgeException
  62. {
  63. this.indexer = plexusSisuBridge.lookup( NexusIndexer.class );
  64. this.archivaConfig = archivaConfig;
  65. }
  66. /**
  67. * @see RepositorySearch#search(String, List, String, SearchResultLimits, List)
  68. */
  69. public SearchResults search( String principal, List<String> selectedRepos, String term, SearchResultLimits limits,
  70. List<String> previousSearchTerms )
  71. throws RepositorySearchException
  72. {
  73. addIndexingContexts( selectedRepos );
  74. // since upgrade to nexus 2.0.0, query has changed from g:[QUERIED TERM]* to g:*[QUERIED TERM]*
  75. // resulting to more wildcard searches so we need to increase max clause count
  76. BooleanQuery.setMaxClauseCount( Integer.MAX_VALUE );
  77. BooleanQuery q = new BooleanQuery();
  78. if ( previousSearchTerms == null || previousSearchTerms.isEmpty() )
  79. {
  80. constructQuery( term, q );
  81. }
  82. else
  83. {
  84. for ( String previousTerm : previousSearchTerms )
  85. {
  86. BooleanQuery iQuery = new BooleanQuery();
  87. constructQuery( previousTerm, iQuery );
  88. q.add( iQuery, Occur.MUST );
  89. }
  90. BooleanQuery iQuery = new BooleanQuery();
  91. constructQuery( term, iQuery );
  92. q.add( iQuery, Occur.MUST );
  93. }
  94. return search( limits, q );
  95. }
  96. /**
  97. * @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
  98. */
  99. public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
  100. throws RepositorySearchException
  101. {
  102. if ( searchFields.getRepositories() == null )
  103. {
  104. throw new RepositorySearchException( "Repositories cannot be null." );
  105. }
  106. addIndexingContexts( searchFields.getRepositories() );
  107. BooleanQuery q = new BooleanQuery();
  108. if ( searchFields.getGroupId() != null && !"".equals( searchFields.getGroupId() ) )
  109. {
  110. q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( searchFields.getGroupId() ) ), Occur.MUST );
  111. }
  112. if ( searchFields.getArtifactId() != null && !"".equals( searchFields.getArtifactId() ) )
  113. {
  114. q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( searchFields.getArtifactId() ) ), Occur.MUST );
  115. }
  116. if ( searchFields.getVersion() != null && !"".equals( searchFields.getVersion() ) )
  117. {
  118. q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( searchFields.getVersion() ) ), Occur.MUST );
  119. }
  120. if ( searchFields.getPackaging() != null && !"".equals( searchFields.getPackaging() ) )
  121. {
  122. q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( searchFields.getPackaging() ) ), Occur.MUST );
  123. }
  124. if ( searchFields.getClassName() != null && !"".equals( searchFields.getClassName() ) )
  125. {
  126. q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( searchFields.getClassName( ) ) ), Occur.MUST );
  127. }
  128. if ( q.getClauses() == null || q.getClauses().length <= 0 )
  129. {
  130. throw new RepositorySearchException( "No search fields set." );
  131. }
  132. return search( limits, q );
  133. }
  134. private SearchResults search( SearchResultLimits limits, BooleanQuery q )
  135. throws RepositorySearchException
  136. {
  137. try
  138. {
  139. FlatSearchRequest request = new FlatSearchRequest( q );
  140. FlatSearchResponse response = indexer.searchFlat( request );
  141. if ( response == null || response.getTotalHits() == 0 )
  142. {
  143. SearchResults results = new SearchResults();
  144. results.setLimits( limits );
  145. return results;
  146. }
  147. return convertToSearchResults( response, limits );
  148. }
  149. catch ( IOException e )
  150. {
  151. throw new RepositorySearchException( e );
  152. }
  153. finally
  154. {
  155. Map<String, IndexingContext> indexingContexts = indexer.getIndexingContexts();
  156. for ( Map.Entry<String, IndexingContext> entry : indexingContexts.entrySet() )
  157. {
  158. try
  159. {
  160. indexer.removeIndexingContext( entry.getValue(), false );
  161. log.debug( "Indexing context '{}' removed from search.", entry.getKey() );
  162. }
  163. catch ( IOException e )
  164. {
  165. log.warn( "IOException occurred while removing indexing content '" + entry.getKey() + "'." );
  166. continue;
  167. }
  168. }
  169. }
  170. }
  171. private void constructQuery( String term, BooleanQuery q )
  172. {
  173. q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( term ) ), Occur.SHOULD );
  174. q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( term ) ), Occur.SHOULD );
  175. q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( term ) ), Occur.SHOULD );
  176. q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( term ) ), Occur.SHOULD );
  177. q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( term ) ), Occur.SHOULD );
  178. }
  179. private void addIndexingContexts( List<String> selectedRepos )
  180. {
  181. for ( String repo : selectedRepos )
  182. {
  183. try
  184. {
  185. Configuration config = archivaConfig.getConfiguration();
  186. ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo );
  187. if ( repoConfig != null )
  188. {
  189. String indexDir = repoConfig.getIndexDir();
  190. File indexDirectory = null;
  191. if ( indexDir != null && !"".equals( indexDir ) )
  192. {
  193. indexDirectory = new File( repoConfig.getIndexDir() );
  194. }
  195. else
  196. {
  197. indexDirectory = new File( repoConfig.getLocation(), ".indexer" );
  198. }
  199. IndexingContext context = indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(),
  200. new File( repoConfig.getLocation() ),
  201. indexDirectory, null, null,
  202. ArchivaNexusIndexerUtil.FULL_INDEX );
  203. context.setSearchable( repoConfig.isScanned() );
  204. }
  205. else
  206. {
  207. log.warn( "Repository '" + repo + "' not found in configuration." );
  208. }
  209. }
  210. catch ( UnsupportedExistingLuceneIndexException e )
  211. {
  212. log.warn( "Error accessing index of repository '" + repo + "' : " + e.getMessage() );
  213. continue;
  214. }
  215. catch ( IOException e )
  216. {
  217. log.warn( "IO error occured while accessing index of repository '" + repo + "' : " + e.getMessage() );
  218. continue;
  219. }
  220. }
  221. }
  222. private SearchResults convertToSearchResults( FlatSearchResponse response, SearchResultLimits limits )
  223. {
  224. SearchResults results = new SearchResults();
  225. Set<ArtifactInfo> artifactInfos = response.getResults();
  226. for ( ArtifactInfo artifactInfo : artifactInfos )
  227. {
  228. String id = SearchUtil.getHitId( artifactInfo.groupId, artifactInfo.artifactId );
  229. Map<String, SearchResultHit> hitsMap = results.getHitsMap();
  230. SearchResultHit hit = hitsMap.get( id );
  231. if ( hit != null )
  232. {
  233. hit.addVersion( artifactInfo.version );
  234. }
  235. else
  236. {
  237. hit = new SearchResultHit();
  238. hit.setArtifactId( artifactInfo.artifactId );
  239. hit.setGroupId( artifactInfo.groupId );
  240. // do we still need to set the repository id even though we're merging everything?
  241. //hit.setRepositoryId( artifactInfo.repository );
  242. hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname );
  243. if ( !hit.getVersions().contains( artifactInfo.version ) )
  244. {
  245. hit.addVersion( artifactInfo.version );
  246. }
  247. }
  248. results.addHit( id, hit );
  249. }
  250. results.setTotalHits( results.getHitsMap().size() );
  251. results.setLimits( limits );
  252. if ( limits == null || limits.getSelectedPage() == SearchResultLimits.ALL_PAGES )
  253. {
  254. return results;
  255. }
  256. else
  257. {
  258. return paginate( results );
  259. }
  260. }
  261. private SearchResults paginate( SearchResults results )
  262. {
  263. SearchResultLimits limits = results.getLimits();
  264. SearchResults paginated = new SearchResults();
  265. int fetchCount = limits.getPageSize();
  266. int offset = ( limits.getSelectedPage() * limits.getPageSize() );
  267. if ( fetchCount > results.getTotalHits() )
  268. {
  269. fetchCount = results.getTotalHits();
  270. }
  271. // Goto offset.
  272. if ( offset < results.getTotalHits() )
  273. {
  274. // only process if the offset is within the hit count.
  275. for ( int i = 0; i < fetchCount; i++ )
  276. {
  277. // Stop fetching if we are past the total # of available hits.
  278. if ( offset + i >= results.getHits().size() )
  279. {
  280. break;
  281. }
  282. SearchResultHit hit = results.getHits().get( ( offset + i ) );
  283. if ( hit != null )
  284. {
  285. String id = SearchUtil.getHitId( hit.getGroupId(), hit.getArtifactId() );
  286. paginated.addHit( id, hit );
  287. }
  288. else
  289. {
  290. break;
  291. }
  292. }
  293. }
  294. paginated.setTotalHits( results.getTotalHits() );
  295. paginated.setLimits( limits );
  296. return paginated;
  297. }
  298. }