[MRM-749]

o implement advanced search for nexus search impl
o added test cases


git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-nexus-indexer@740206 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maria Odea B. Ching 2009-02-03 03:39:06 +00:00
parent 61d0936fc0
commit a86557330f
3 changed files with 173 additions and 36 deletions

View File

@ -70,13 +70,7 @@ public class NexusRepositorySearch
throws RepositorySearchException
{
addIndexingContexts( selectedRepos );
// TODO:
// 1. construct query for:
// - regular search
// - searching within search results
// 3. multiple repositories
BooleanQuery q = new BooleanQuery();
if( previousSearchTerms == null || previousSearchTerms.isEmpty() )
{
@ -97,6 +91,59 @@ public class NexusRepositorySearch
q.add( iQuery, Occur.MUST );
}
return search( limits, q );
}
/**
* @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
*/
public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
throws RepositorySearchException
{
if( searchFields.getRepositories() == null )
{
throw new RepositorySearchException( "Repositories cannot be null." );
}
addIndexingContexts( searchFields.getRepositories() );
BooleanQuery q = new BooleanQuery();
if( searchFields.getGroupId() != null && !"".equals( searchFields.getGroupId() ) )
{
q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, searchFields.getGroupId() ), Occur.MUST );
}
if( searchFields.getArtifactId() != null && !"".equals( searchFields.getArtifactId() ) )
{
q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, searchFields.getArtifactId() ), Occur.MUST );
}
if( searchFields.getVersion() != null && !"".equals( searchFields.getVersion() ) )
{
q.add( indexer.constructQuery( ArtifactInfo.VERSION, searchFields.getVersion() ), Occur.MUST );
}
if( searchFields.getPackaging() != null && !"".equals( searchFields.getPackaging() ) )
{
q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, searchFields.getPackaging() ), Occur.MUST );
}
if( searchFields.getClassName() != null && !"".equals( searchFields.getClassName() ) )
{
q.add( indexer.constructQuery( ArtifactInfo.NAMES, searchFields.getClassName() ), Occur.MUST );
}
if( q.getClauses() == null || q.getClauses().length <= 0 )
{
throw new RepositorySearchException( "No search fields set." );
}
return search( limits, q );
}
private SearchResults search( SearchResultLimits limits, BooleanQuery q )
throws RepositorySearchException
{
try
{
FlatSearchRequest request = new FlatSearchRequest( q );
@ -146,16 +193,7 @@ public class NexusRepositorySearch
q.add( indexer.constructQuery( ArtifactInfo.NAMES, term ), Occur.SHOULD );
}
/**
* @see RepositorySearch#search(String, SearchFields, SearchResultLimits)
*/
public SearchResults search( String principal, SearchFields searchFields, SearchResultLimits limits )
throws RepositorySearchException
{
// TODO Auto-generated method stub
return null;
}
private void addIndexingContexts( List<String> selectedRepos )
{
for( String repo : selectedRepos )

View File

@ -1,5 +1,8 @@
package org.apache.archiva.indexer.search;
import java.util.ArrayList;
import java.util.List;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@ -21,20 +24,36 @@ package org.apache.archiva.indexer.search;
public class SearchFields
{
/**
* groupId
*/
private String groupId;
/**
* artifactId
*/
private String artifactId;
/**
* version
*/
private String version;
/**
* packaging (jar, war, pom, etc.)
*/
private String packaging;
/**
* class name or package name
*/
private String className;
private String packageName;
private String repositoryId;
/**
* repositories
*/
private List<String> repositories = new ArrayList<String>();
public String getGroupId()
{
return groupId;
@ -85,23 +104,13 @@ public class SearchFields
this.className = className;
}
public String getPackageName()
public List<String> getRepositories()
{
return packageName;
return repositories;
}
public void setPackageName( String packageName )
public void setRepositories( List<String> repositories )
{
this.packageName = packageName;
}
public String getRepositoryId()
{
return repositoryId;
}
public void setRepositoryId( String repositoryId )
{
this.repositoryId = repositoryId;
this.repositories = repositories;
}
}

View File

@ -330,7 +330,97 @@ public class NexusRepositorySearchTest
public void testAdvancedSearch()
throws Exception
{
List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
"/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
createIndex( TEST_REPO_2, files );
List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
selectedRepos.add( TEST_REPO_2 );
config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
SearchFields searchFields = new SearchFields();
searchFields.setGroupId( "org.apache.archiva" );
searchFields.setVersion( "1.0" );
searchFields.setRepositories( selectedRepos );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
archivaConfigControl.replay();
SearchResults results = search.search( "user", searchFields, null );
archivaConfigControl.verify();
assertNotNull( results );
assertEquals( 2, results.getTotalHits() );
FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
}
public void testAdvancedSearchWithPagination()
throws Exception
{
List<File> files = new ArrayList<File>();
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
"/org/apache/archiva/archiva-search/1.0/archiva-search-1.0.jar" ) );
files.add( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 +
"/org/apache/archiva/archiva-search/1.1/archiva-search-1.1.jar" ) );
createIndex( TEST_REPO_2, files );
List<String> selectedRepos = new ArrayList<String>();
selectedRepos.add( TEST_REPO_1 );
selectedRepos.add( TEST_REPO_2 );
config.addManagedRepository( createRepositoryConfig( TEST_REPO_2 ) );
SearchFields searchFields = new SearchFields();
searchFields.setGroupId( "org.apache.archiva" );
searchFields.setVersion( "1.0" );
searchFields.setRepositories( selectedRepos );
// page 1
SearchResultLimits limits = new SearchResultLimits( 0 );
limits.setPageSize( 1 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
archivaConfigControl.replay();
SearchResults results = search.search( "user", searchFields, limits );
archivaConfigControl.verify();
assertNotNull( results );
assertEquals( 2, results.getTotalHits() );
assertEquals( 1, results.getHits().size() );
// page 2
archivaConfigControl.reset();
limits = new SearchResultLimits( 1 );
limits.setPageSize( 1 );
archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config, 2 );
archivaConfigControl.replay();
results = search.search( "user", searchFields, limits );
archivaConfigControl.verify();
assertNotNull( results );
assertEquals( 2, results.getTotalHits() );
assertEquals( 1, results.getHits().size() );
FileUtils.deleteDirectory( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) );
assertFalse( new File( getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() );
}
}