[MRM-981]

-fix query construction for advanced search


git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@707291 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maria Odea B. Ching 2008-10-23 06:31:39 +00:00
parent fb7574abc1
commit b86b71e28f
4 changed files with 88 additions and 52 deletions

View File

@ -83,49 +83,69 @@ public class DefaultCrossRepositorySearch
private ArchivaConfiguration configuration; private ArchivaConfiguration configuration;
private List<ManagedRepositoryConfiguration> localIndexedRepositories = new ArrayList<ManagedRepositoryConfiguration>(); private List<ManagedRepositoryConfiguration> localIndexedRepositories = new ArrayList<ManagedRepositoryConfiguration>();
public SearchResults executeFilteredSearch( String principal, List<String> selectedRepos, String groupId, public SearchResults executeFilteredSearch( String principal, List<String> selectedRepos, String groupId,
String artifactId, String version, String className, String artifactId, String version, String className,
SearchResultLimits limits ) SearchResultLimits limits )
{ {
List<RepositoryContentIndex> indexes = getBytecodeIndexes( principal, selectedRepos ); List<RepositoryContentIndex> indexes = getBytecodeIndexes( principal, selectedRepos );
SearchResults results = new SearchResults(); SearchResults results = new SearchResults();
BooleanQuery booleanQuery = new BooleanQuery(); List<String> fieldsList = new ArrayList<String>();
List<String> termsList = new ArrayList<String>();
if ( groupId != null && groupId.length() > 0 ) List<BooleanClause.Occur> flagsList = new ArrayList<BooleanClause.Occur>();
if( groupId != null && !"".equals( groupId.trim() ) )
{ {
parseAndAdd( booleanQuery, ArtifactKeys.GROUPID, groupId, "\\.|-" ); fieldsList.add( ArtifactKeys.GROUPID );
termsList.add( groupId );
flagsList.add( BooleanClause.Occur.MUST );
} }
if ( artifactId != null && artifactId.length() > 0 ) if( artifactId != null && !"".equals( artifactId.trim() ) )
{ {
parseAndAdd( booleanQuery, ArtifactKeys.ARTIFACTID, artifactId, "\\.|-" ); fieldsList.add( ArtifactKeys.ARTIFACTID );
termsList.add( artifactId );
flagsList.add( BooleanClause.Occur.MUST );
} }
if ( version != null && version.length() > 0 ) if( version != null && !"".equals( version.trim() ) )
{ {
parseAndAdd( booleanQuery, ArtifactKeys.VERSION, version, "\\.|-" ); fieldsList.add( ArtifactKeys.VERSION );
termsList.add( version );
flagsList.add( BooleanClause.Occur.MUST );
} }
if ( className != null && className.length() > 0 ) if( className != null && !"".equals( className.trim() ) )
{
fieldsList.add( BytecodeKeys.CLASSES );
fieldsList.add( BytecodeKeys.FILES );
fieldsList.add( BytecodeKeys.METHODS );
termsList.add( className.trim() );
termsList.add( className.trim() );
termsList.add( className.trim() );
flagsList.add( BooleanClause.Occur.SHOULD );
flagsList.add( BooleanClause.Occur.SHOULD );
flagsList.add( BooleanClause.Occur.SHOULD );
}
try
{ {
String[] fieldsArr = new String[ fieldsList.size() ];
try String[] queryArr = new String[ termsList.size() ];
{ BooleanClause.Occur[] flagsArr = new BooleanClause.Occur[ flagsList.size() ];
QueryParser parser =
new MultiFieldQueryParser( new String[] { BytecodeKeys.CLASSES, BytecodeKeys.FILES, Query fieldsQuery =
BytecodeKeys.METHODS }, new BytecodeHandlers().getAnalyzer() ); MultiFieldQueryParser.parse( termsList.toArray( queryArr ), fieldsList.toArray( fieldsArr ),
booleanQuery.add( parser.parse( className ), BooleanClause.Occur.MUST ); flagsList.toArray( flagsArr ), new BytecodeHandlers().getAnalyzer() );
}
catch ( ParseException e ) LuceneQuery query = new LuceneQuery( fieldsQuery );
{ results = searchAll( query, limits, indexes, null );
results.getRepositories().add( this.localIndexedRepositories );
}
} }
catch ( ParseException e )
LuceneQuery query = new LuceneQuery( booleanQuery ); {
results = searchAll( query, limits, indexes, null ); log.warn( "Unable to parse advanced search fields and query terms." );
results.getRepositories().add( this.localIndexedRepositories ); }
return results; return results;
} }
@ -263,8 +283,8 @@ public class DefaultCrossRepositorySearch
hits = searcher.search( specificQuery ); hits = searcher.search( specificQuery );
} }
int hitCount = hits.length(); int hitCount = hits.length();
// Now process the limits. // Now process the limits.
results.setLimits( limits ); results.setLimits( limits );
results.setTotalHits( hitCount ); results.setTotalHits( hitCount );
@ -453,26 +473,6 @@ public class DefaultCrossRepositorySearch
} }
} }
private void parseAndAdd( BooleanQuery query, String key, String value, String delimiter )
{
if ( value != null && value.length() > 0 )
{
String[] terms = value.split( delimiter );
for ( int i = 0; i < terms.length; i++ )
{
Term valueTerm = new Term( key, terms[i] );
Query valueQuery = new TermQuery( valueTerm );
query.add( valueQuery, BooleanClause.Occur.MUST );
}
}
else
{
Term valueTerm = new Term( key, value );
Query valueQuery = new TermQuery( valueTerm );
query.add( valueQuery, BooleanClause.Occur.MUST );
}
}
public void initialize() public void initialize()
throws InitializationException throws InitializationException
{ {

View File

@ -0,0 +1,7 @@
FILENAME|a-common5-1.0.jar
SIZE|8516
HASH_MD5|a5d0d280ce83133432d8fed8f2ce3474
HASH_SHA1|c2635a1b38bd4520a6604664c04b2b3c32330864
HASH_BYTECODE|2868f6661c55afda5a3b62859fbc8b1beb021b6e
JDK|1.4
CLASS|a.common5.package.ACommonTestFile

View File

@ -75,6 +75,9 @@ public class BytecodeIndexPopulator
// wagon-provider-api-1.0-beta-3-20070209.213958-2.jar.txt // wagon-provider-api-1.0-beta-3-20070209.213958-2.jar.txt
dumps.put( "wagon-provider-api", createArchivaArtifact( "org.apache.maven.wagon", "wagon-provider-api", dumps.put( "wagon-provider-api", createArchivaArtifact( "org.apache.maven.wagon", "wagon-provider-api",
"1.0-beta-3-20070209.213958-2", "", "jar" ) ); "1.0-beta-3-20070209.213958-2", "", "jar" ) );
// a-common5
dumps.put( "a-common5", createArchivaArtifact( "a", "a-common5", "1.0", "", "jar" ) );
return dumps; return dumps;

View File

@ -267,6 +267,32 @@ public class DefaultCrossRepositorySearchTest
"archiva-common", "1.0", null, 30 ); "archiva-common", "1.0", null, 30 );
} }
// MRM-981 - artifactIds with numeric characters aren't found in advanced search
public void testFilteredSearchArtifactIdHasNumericChar()
throws Exception
{
CrossRepositorySearch search = lookupCrossRepositorySearch();
String expectedRepos[] = new String[] { TEST_DEFAULT_REPO_ID };
String expectedResults[] = new String[] { "a-common5" };
assertFilteredSearchResults( expectedRepos, expectedResults, search, null, "a-common5", null, null, 30 );
assertFilteredSearchResults( expectedRepos, expectedResults, search, "a", "a-common5", null, null, 30 );
assertFilteredSearchResults( expectedRepos, expectedResults, search, "a", "a-common5", "1.0", null, 30 );
assertFilteredSearchResults( expectedRepos, expectedResults, search, "a", "a-common5", "1.0", "ACommonTestFile", 30 );
assertFilteredSearchResults( expectedRepos, expectedResults, search, "a", "a-common5", "1.0", "a.common5.package.", 30 );
String noHitsExpectedResults[] = new String[] {};
assertFilteredSearchResults( expectedRepos, noHitsExpectedResults, search, "org.apache.maven.archiva",
"a-common5", null, null, 30 );
}
private void assertFilteredSearchResults ( String expectedRepos[], String expectedResults[], CrossRepositorySearch search, private void assertFilteredSearchResults ( String expectedRepos[], String expectedResults[], CrossRepositorySearch search,
String groupId, String artifactId, String version, String className , int rowCount ) String groupId, String artifactId, String version, String className , int rowCount )
{ {