From b86b71e28f1f0314c0446f625db5d3898b3a4a72 Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Thu, 23 Oct 2008 06:31:39 +0000 Subject: [PATCH] [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 --- .../search/DefaultCrossRepositorySearch.java | 104 +++++++++--------- .../test/artifact-dumps/a-common5-1.0.jar.txt | 7 ++ .../search/BytecodeIndexPopulator.java | 3 + .../DefaultCrossRepositorySearchTest.java | 26 +++++ 4 files changed, 88 insertions(+), 52 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-indexer/src/test/artifact-dumps/a-common5-1.0.jar.txt diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java index a21c4fa6b..3fa8044d9 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java @@ -83,49 +83,69 @@ public class DefaultCrossRepositorySearch private ArchivaConfiguration configuration; private List localIndexedRepositories = new ArrayList(); - + public SearchResults executeFilteredSearch( String principal, List selectedRepos, String groupId, String artifactId, String version, String className, SearchResultLimits limits ) { List indexes = getBytecodeIndexes( principal, selectedRepos ); - SearchResults results = new SearchResults(); - BooleanQuery booleanQuery = new BooleanQuery(); - - if ( groupId != null && groupId.length() > 0 ) + SearchResults results = new SearchResults(); + List fieldsList = new ArrayList(); + List termsList = new ArrayList(); + List flagsList = new ArrayList(); + + 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 { - - try - { - QueryParser parser = - new MultiFieldQueryParser( new String[] { BytecodeKeys.CLASSES, BytecodeKeys.FILES, - BytecodeKeys.METHODS }, new BytecodeHandlers().getAnalyzer() ); - booleanQuery.add( parser.parse( className ), BooleanClause.Occur.MUST ); - } - catch ( ParseException e ) - { - - } + String[] fieldsArr = new String[ fieldsList.size() ]; + String[] queryArr = new String[ termsList.size() ]; + BooleanClause.Occur[] flagsArr = new BooleanClause.Occur[ flagsList.size() ]; + + Query fieldsQuery = + MultiFieldQueryParser.parse( termsList.toArray( queryArr ), fieldsList.toArray( fieldsArr ), + flagsList.toArray( flagsArr ), new BytecodeHandlers().getAnalyzer() ); + + LuceneQuery query = new LuceneQuery( fieldsQuery ); + results = searchAll( query, limits, indexes, null ); + results.getRepositories().add( this.localIndexedRepositories ); } - - LuceneQuery query = new LuceneQuery( booleanQuery ); - results = searchAll( query, limits, indexes, null ); - results.getRepositories().add( this.localIndexedRepositories ); + catch ( ParseException e ) + { + log.warn( "Unable to parse advanced search fields and query terms." ); + } return results; } @@ -263,8 +283,8 @@ public class DefaultCrossRepositorySearch hits = searcher.search( specificQuery ); } - int hitCount = hits.length(); - + int hitCount = hits.length(); + // Now process the limits. results.setLimits( limits ); 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() throws InitializationException { diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/artifact-dumps/a-common5-1.0.jar.txt b/archiva-modules/archiva-base/archiva-indexer/src/test/artifact-dumps/a-common5-1.0.jar.txt new file mode 100644 index 000000000..69d2bc910 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/artifact-dumps/a-common5-1.0.jar.txt @@ -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 diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/BytecodeIndexPopulator.java b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/BytecodeIndexPopulator.java index 4587ef7a7..92cfb931f 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/BytecodeIndexPopulator.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/BytecodeIndexPopulator.java @@ -75,6 +75,9 @@ public class BytecodeIndexPopulator // 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", "1.0-beta-3-20070209.213958-2", "", "jar" ) ); + + // a-common5 + dumps.put( "a-common5", createArchivaArtifact( "a", "a-common5", "1.0", "", "jar" ) ); return dumps; diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearchTest.java b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearchTest.java index 005e3e719..8d0455014 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearchTest.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearchTest.java @@ -267,6 +267,32 @@ public class DefaultCrossRepositorySearchTest "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, String groupId, String artifactId, String version, String className , int rowCount ) { -- 2.39.5