o added quick search implementation in NexusRepositorySearch o added tests git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-nexus-indexer@738766 13f79535-47bb-0310-9956-ffa450edef68archiva-nexus-indexer
@@ -105,7 +105,8 @@ public class NexusIndexerConsumer | |||
File indexDirectory = null; | |||
if( indexDir != null && !"".equals( indexDir ) ) | |||
{ | |||
indexDirectory = new File( managedRepository, repository.getIndexDir() ); | |||
//indexDirectory = new File( managedRepository, repository.getIndexDir() ); | |||
indexDirectory = new File( repository.getIndexDir() ); | |||
} | |||
else | |||
{ |
@@ -26,7 +26,9 @@ import java.util.Map; | |||
import java.util.Set; | |||
import org.apache.lucene.search.BooleanQuery; | |||
import org.apache.lucene.search.BooleanClause.Occur; | |||
import org.apache.maven.archiva.configuration.ArchivaConfiguration; | |||
import org.apache.maven.archiva.configuration.Configuration; | |||
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; | |||
import org.apache.maven.archiva.indexer.search.SearchResultHit; | |||
import org.apache.maven.archiva.indexer.search.SearchResultLimits; | |||
@@ -69,15 +71,25 @@ public class NexusRepositorySearch | |||
// - regular search | |||
// - searching within search results | |||
// 2. consider pagination | |||
// 3. multiple repositories | |||
BooleanQuery q = new BooleanQuery(); | |||
// q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD ); | |||
// q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD ); | |||
q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, term ), Occur.SHOULD ); | |||
q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, term ), Occur.SHOULD ); | |||
q.add( indexer.constructQuery( ArtifactInfo.VERSION, term ), Occur.SHOULD ); | |||
q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, term ), Occur.SHOULD ); | |||
// TODO: what about class & package? | |||
try | |||
{ | |||
FlatSearchRequest request = new FlatSearchRequest( q ); | |||
FlatSearchResponse response = indexer.searchFlat( request ); | |||
if( response == null ) | |||
{ | |||
return new SearchResults(); | |||
} | |||
return convertToSearchResults( response ); | |||
} | |||
@@ -104,22 +116,31 @@ public class NexusRepositorySearch | |||
{ | |||
try | |||
{ | |||
ManagedRepositoryConfiguration repoConfig = archivaConfig.getConfiguration().findManagedRepositoryById( repo ); | |||
String indexDir = repoConfig.getIndexDir(); | |||
File indexDirectory = null; | |||
if( indexDir != null && !"".equals( indexDir ) ) | |||
Configuration config = archivaConfig.getConfiguration(); | |||
ManagedRepositoryConfiguration repoConfig = config.findManagedRepositoryById( repo ); | |||
if( repoConfig != null ) | |||
{ | |||
indexDirectory = new File( repoConfig.getLocation(), repoConfig.getIndexDir() ); | |||
String indexDir = repoConfig.getIndexDir(); | |||
File indexDirectory = null; | |||
if( indexDir != null && !"".equals( indexDir ) ) | |||
{ | |||
indexDirectory = new File( repoConfig.getIndexDir() ); | |||
} | |||
else | |||
{ | |||
indexDirectory = new File( repoConfig.getLocation(), ".indexer" ); | |||
} | |||
IndexingContext context = | |||
indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ), | |||
indexDirectory, null, null, NexusIndexer.FULL_INDEX ); | |||
context.setSearchable( repoConfig.isScanned() ); | |||
} | |||
else | |||
{ | |||
indexDirectory = new File( repoConfig.getLocation(), ".indexer" ); | |||
log.warn( "Repository '" + repo + "' not found in configuration." ); | |||
} | |||
IndexingContext context = | |||
indexer.addIndexingContext( repoConfig.getId(), repoConfig.getId(), new File( repoConfig.getLocation() ), | |||
indexDirectory, null, null, NexusIndexer.FULL_INDEX ); | |||
context.setSearchable( repoConfig.isScanned() ); | |||
} | |||
catch ( UnsupportedExistingLuceneIndexException e ) | |||
{ | |||
@@ -138,12 +159,14 @@ public class NexusRepositorySearch | |||
private SearchResults convertToSearchResults( FlatSearchResponse response ) | |||
{ | |||
// TODO: paginate! | |||
SearchResults results = new SearchResults(); | |||
Set<ArtifactInfo> artifactInfos = response.getResults(); | |||
for ( ArtifactInfo artifactInfo : artifactInfos ) | |||
{ | |||
String id = artifactInfo.groupId + ":" + artifactInfo.artifactId; | |||
String id = artifactInfo.groupId + ":" + artifactInfo.artifactId; | |||
Map<String, SearchResultHit> hitsMap = results.getHitsMap(); | |||
SearchResultHit hit = hitsMap.get( id ); | |||
@@ -158,12 +181,17 @@ public class NexusRepositorySearch | |||
hit.setGroupId( artifactInfo.groupId ); | |||
hit.setRepositoryId( artifactInfo.repository ); | |||
hit.setUrl( artifactInfo.repository + "/" + artifactInfo.fname ); | |||
hit.addVersion( artifactInfo.version ); | |||
if( !hit.getVersions().contains( artifactInfo.version ) ) | |||
{ | |||
hit.addVersion( artifactInfo.version ); | |||
} | |||
} | |||
results.addHit( id, hit ); | |||
} | |||
results.setTotalHits( results.getHitsMap().size() ); | |||
return results; | |||
} | |||
@@ -44,6 +44,7 @@ public class SearchResultHit | |||
// Advanced hit, reference to artifactId. | |||
private String artifactId; | |||
// TODO: remove/deprecate this field! | |||
private String version = ""; | |||
private String repositoryId = ""; |
@@ -0,0 +1,10 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<beans xmlns="http://www.springframework.org/schema/beans" | |||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> | |||
<bean id="nexusSearch" class="org.apache.archiva.indexer.search.NexusRepositorySearch"> | |||
<constructor-arg ref="nexusIndexer"/> | |||
<constructor-arg ref="archivaConfiguration"/> | |||
</bean> | |||
</beans> |
@@ -19,9 +19,25 @@ package org.apache.archiva.indexer.search; | |||
* under the License. | |||
*/ | |||
import java.io.File; | |||
import java.io.IOException; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import org.apache.commons.io.FileUtils; | |||
import org.apache.maven.archiva.configuration.ArchivaConfiguration; | |||
import org.apache.maven.archiva.configuration.Configuration; | |||
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; | |||
import org.apache.maven.archiva.indexer.search.SearchResultHit; | |||
import org.apache.maven.archiva.indexer.search.SearchResults; | |||
import org.codehaus.plexus.spring.PlexusInSpringTestCase; | |||
import org.easymock.MockControl; | |||
import org.sonatype.nexus.index.ArtifactContext; | |||
import org.sonatype.nexus.index.ArtifactContextProducer; | |||
import org.sonatype.nexus.index.NexusIndexer; | |||
import org.sonatype.nexus.index.context.IndexingContext; | |||
import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; | |||
import org.sonatype.nexus.index.creator.IndexerEngine; | |||
public class NexusRepositorySearchTest | |||
extends PlexusInSpringTestCase | |||
@@ -32,20 +48,144 @@ public class NexusRepositorySearchTest | |||
private NexusIndexer indexer; | |||
private IndexingContext context; | |||
private IndexerEngine indexerEngine; | |||
private ArtifactContextProducer artifactContextProducer; | |||
private MockControl archivaConfigControl; | |||
private Configuration config; | |||
private final static String TEST_REPO = "nexus-search-test-repo"; | |||
@Override | |||
protected void setUp() | |||
throws Exception | |||
{ | |||
super.setUp(); | |||
indexer = ( NexusIndexer )lookup( NexusIndexer.class ); | |||
indexer = ( NexusIndexer ) lookup( NexusIndexer.class ); | |||
archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class ); | |||
archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock(); | |||
search = new NexusRepositorySearch( indexer, archivaConfig ); | |||
indexerEngine = ( IndexerEngine ) lookup( IndexerEngine.class ); | |||
artifactContextProducer = ( ArtifactContextProducer ) lookup( ArtifactContextProducer.class ); | |||
config = new Configuration(); | |||
ManagedRepositoryConfiguration repositoryConfig = new ManagedRepositoryConfiguration(); | |||
repositoryConfig.setId( TEST_REPO ); | |||
repositoryConfig.setLocation( getBasedir() + "/target/test-classes/" + TEST_REPO ); | |||
repositoryConfig.setLayout( "default" ); | |||
repositoryConfig.setName( "Nexus Search Test Repository" ); | |||
repositoryConfig.setScanned( true ); | |||
repositoryConfig.setSnapshots( false ); | |||
repositoryConfig.setReleases( true ); | |||
config.addManagedRepository( repositoryConfig ); | |||
createIndex(); | |||
} | |||
@Override | |||
protected void tearDown() | |||
throws Exception | |||
{ | |||
FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/"+ TEST_REPO + "/.indexer" ) ); | |||
assertFalse( new File( getBasedir(), "/target/test-classes/"+ TEST_REPO + "/.indexer" ).exists() ); | |||
super.tearDown(); | |||
} | |||
private void createIndex() | |||
throws IOException, UnsupportedExistingLuceneIndexException | |||
{ | |||
context = | |||
indexer.addIndexingContext( TEST_REPO, TEST_REPO, new File( getBasedir(), "/target/test-classes/" + TEST_REPO ), | |||
new File( getBasedir(), "/target/test-classes/" + TEST_REPO + "/.indexer"), null, null, NexusIndexer.FULL_INDEX ); | |||
context.setSearchable( true ); | |||
indexerEngine.beginIndexing( context ); | |||
File artifactFile = | |||
new File( getBasedir(), | |||
"/target/test-classes/" + TEST_REPO + "/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ); | |||
ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile ); | |||
indexerEngine.index( context, ac ); | |||
artifactFile = | |||
new File( getBasedir(), | |||
"/target/test-classes/" + TEST_REPO + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ); | |||
ac = artifactContextProducer.getArtifactContext( context, artifactFile ); | |||
indexerEngine.index( context, ac ); | |||
artifactFile = | |||
new File( getBasedir(), | |||
"/target/test-classes/" + TEST_REPO + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ); | |||
ac = artifactContextProducer.getArtifactContext( context, artifactFile ); | |||
indexerEngine.index( context, ac ); | |||
indexerEngine.endIndexing( context ); | |||
assertTrue( new File( getBasedir(), "/target/test-classes/"+ TEST_REPO + "/.indexer" ).exists() ); | |||
} | |||
public void testQuickSearch() | |||
throws Exception | |||
{ | |||
List<String> selectedRepos = new ArrayList<String>(); | |||
selectedRepos.add( TEST_REPO ); | |||
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); | |||
archivaConfigControl.replay(); | |||
SearchResults results = search.search( "user", selectedRepos, "archiva-search", null ); | |||
archivaConfigControl.verify(); | |||
assertNotNull( results ); | |||
assertEquals( 1, results.getTotalHits() ); | |||
SearchResultHit hit = results.getHits().get( 0 ); | |||
assertEquals( "org.apache.archiva", hit.getGroupId() ); | |||
assertEquals( "archiva-search", hit.getArtifactId() ); | |||
assertEquals( "1.0", hit.getVersions().get( 0 ) ); | |||
assertEquals( "nexus-search-test-repo", hit.getRepositoryId() ); | |||
archivaConfigControl.reset(); | |||
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); | |||
archivaConfigControl.replay(); | |||
results = search.search( "user", selectedRepos, "org.apache.archiva", null ); | |||
archivaConfigControl.verify(); | |||
assertNotNull( results ); | |||
assertEquals( 2, results.getTotalHits() ); | |||
//TODO: search for class & package names | |||
} | |||
public void testArtifactFoundInMultipleRepositories() | |||
throws Exception | |||
{ | |||
// there should be no duplicates in the search result hit | |||
// TODO: [BROWSE] in artifact info from browse, display all the repositories where the artifact is found | |||
} | |||
public void testNoMatchFound() | |||
throws Exception | |||
{ | |||
} | |||
@@ -0,0 +1,28 @@ | |||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |||
<modelVersion>4.0.0</modelVersion> | |||
<groupId>org.apache.archiva</groupId> | |||
<artifactId>archiva-search</artifactId> | |||
<packaging>jar</packaging> | |||
<version>1.0</version> | |||
<name>Archiva Search</name> | |||
<url>http://archiva.apache.org</url> | |||
<dependencies> | |||
<dependency> | |||
<groupId>junit</groupId> | |||
<artifactId>junit</artifactId> | |||
<version>3.8.1</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>commons-lang</groupId> | |||
<artifactId>commons-lang</artifactId> | |||
<version>2.3</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>log4j</groupId> | |||
<artifactId>log4j</artifactId> | |||
<version>1.2.8</version> | |||
</dependency> | |||
</dependencies> | |||
</project> |
@@ -0,0 +1,23 @@ | |||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |||
<modelVersion>4.0.0</modelVersion> | |||
<groupId>org.apache.archiva</groupId> | |||
<artifactId>archiva-test</artifactId> | |||
<packaging>jar</packaging> | |||
<version>1.0</version> | |||
<name>archiva-test</name> | |||
<url>http://maven.apache.org</url> | |||
<dependencies> | |||
<dependency> | |||
<groupId>junit</groupId> | |||
<artifactId>junit</artifactId> | |||
<version>3.8.1</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>commons-lang</groupId> | |||
<artifactId>commons-lang</artifactId> | |||
<version>2.3</version> | |||
</dependency> | |||
</dependencies> | |||
</project> |
@@ -0,0 +1,28 @@ | |||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |||
<modelVersion>4.0.0</modelVersion> | |||
<groupId>org.apache.archiva</groupId> | |||
<artifactId>archiva-test</artifactId> | |||
<packaging>jar</packaging> | |||
<version>2.0</version> | |||
<name>Archiva Test</name> | |||
<url>http://archiva.apache.org</url> | |||
<dependencies> | |||
<dependency> | |||
<groupId>junit</groupId> | |||
<artifactId>junit</artifactId> | |||
<version>3.8.1</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>commons-lang</groupId> | |||
<artifactId>commons-lang</artifactId> | |||
<version>2.3</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>commons-io</groupId> | |||
<artifactId>commons-io</artifactId> | |||
<version>1.4</version> | |||
</dependency> | |||
</dependencies> | |||
</project> |