]> source.dussan.org Git - archiva.git/commitdiff
[MRM-151] test dependency index, fix tokenization
authorBrett Porter <brett@apache.org>
Sat, 26 Aug 2006 17:49:46 +0000 (17:49 +0000)
committerBrett Porter <brett@apache.org>
Sat, 26 Aug 2006 17:49:46 +0000 (17:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@437192 13f79535-47bb-0310-9956-ffa450edef68

archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndex.java
archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexSearchTest.java

index c97b468cbde78e8bc9f57d38598976754492847e..533ff33de7c1a7de049c5f6f4bbb4d2746452cf9 100644 (file)
@@ -17,6 +17,8 @@ package org.apache.maven.archiva.indexer.lucene;
  */
 
 import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.CharTokenizer;
+import org.apache.lucene.analysis.TokenStream;
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
@@ -30,9 +32,11 @@ import org.apache.maven.archiva.indexer.RepositoryIndexException;
 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
 import org.apache.maven.archiva.indexer.query.Query;
 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord;
+import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.Reader;
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -130,10 +134,35 @@ public class LuceneRepositoryArtifactIndex
         }
     }
 
-    private Analyzer getAnalyzer()
+    public Analyzer getAnalyzer()
     {
-        // TODO: investigate why changed in original! Probably for MD5 and number querying.
-        return new StandardAnalyzer();
+        return new MyAnalyzer();
+    }
+
+    private static class MyAnalyzer
+        extends Analyzer
+    {
+        private static final Analyzer STANDARD = new StandardAnalyzer();
+
+        public TokenStream tokenStream( String field, final Reader reader )
+        {
+            // do not tokenize field called 'element'
+            if ( StandardIndexRecordFields.DEPENDENCIES.equals( field ) )
+            {
+                return new CharTokenizer( reader )
+                {
+                    protected boolean isTokenChar( char c )
+                    {
+                        return c != '\n';
+                    }
+                };
+            }
+            else
+            {
+                // use standard analyzer
+                return STANDARD.tokenStream( field, reader );
+            }
+        }
     }
 
     public void deleteRecords( Collection records )
index 6f06252657e3444973dde9bdb66c61e21a1cda41..3868cbf82e95298c9488fbcfcf73e67dc5c4159c 100644 (file)
@@ -635,6 +635,23 @@ public class LuceneStandardArtifactIndexSearchTest
         assertTrue( "Check results size", results.isEmpty() );
     }
 
+    public void testExactMatchDependency()
+        throws RepositoryIndexSearchException
+    {
+        Query query = new TermQuery(
+            new Term( StandardIndexRecordFields.DEPENDENCIES, "org.apache.maven:maven-plugin-api:2.0" ) );
+        List results = index.search( new LuceneQuery( query ) );
+
+        assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) );
+        assertEquals( "Check results size", 1, results.size() );
+
+        // test non-match fails
+        query = new TermQuery( new Term( StandardIndexRecordFields.DEPENDENCIES, "foo" ) );
+        results = index.search( new LuceneQuery( query ) );
+
+        assertTrue( "Check results size", results.isEmpty() );
+    }
+
     public void testMatchProjectName()
         throws RepositoryIndexSearchException
     {