]> source.dussan.org Git - archiva.git/commitdiff
[MRM-981]
authorMaria Odea B. Ching <oching@apache.org>
Thu, 23 Oct 2008 06:31:39 +0000 (06:31 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Thu, 23 Oct 2008 06:31:39 +0000 (06:31 +0000)
-fix query construction for advanced search

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@707291 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearch.java
archiva-modules/archiva-base/archiva-indexer/src/test/artifact-dumps/a-common5-1.0.jar.txt [new file with mode: 0644]
archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/BytecodeIndexPopulator.java
archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/search/DefaultCrossRepositorySearchTest.java

index a21c4fa6b7831ebb89a4add446f2d49d04d5b795..3fa8044d900f487839b2e1024cf99e2a92eeb90f 100644 (file)
@@ -83,49 +83,69 @@ public class DefaultCrossRepositorySearch
     private ArchivaConfiguration configuration;
 
     private List<ManagedRepositoryConfiguration> localIndexedRepositories = new ArrayList<ManagedRepositoryConfiguration>();
-
+    
     public SearchResults executeFilteredSearch( String principal, List<String> selectedRepos, String groupId,
                                                 String artifactId, String version, String className,
                                                 SearchResultLimits limits )
     {
         List<RepositoryContentIndex> indexes = getBytecodeIndexes( principal, selectedRepos );
-        SearchResults results = new SearchResults();
-        BooleanQuery booleanQuery = new BooleanQuery();
-
-        if ( groupId != null && groupId.length() > 0 )
+        SearchResults results = new SearchResults();        
+        List<String> fieldsList = new ArrayList<String>();
+        List<String> termsList = new ArrayList<String>();
+        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
         {
-
-            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 (file)
index 0000000..69d2bc9
--- /dev/null
@@ -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
index 4587ef7a70b4988d7123d945de82fab6d42bc6e9..92cfb931f59316e998fb84241b5f054fd25071c9 100644 (file)
@@ -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;
 
index 005e3e7194af755e9889f1173d717c99dbaf3970..8d0455014c62c9a14f43b9785c2f29de35089e91 100644 (file)
@@ -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 )
     {