From: Brett Porter Date: Sat, 26 Aug 2006 17:49:46 +0000 (+0000) Subject: [MRM-151] test dependency index, fix tokenization X-Git-Tag: archiva-0.9-alpha-1~641 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4abbe9f5cac0a3f37df1d67fa2931ccbed410abf;p=archiva.git [MRM-151] test dependency index, fix tokenization git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@437192 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndex.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndex.java index c97b468cb..533ff33de 100644 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndex.java +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndex.java @@ -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 ) diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexSearchTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexSearchTest.java index 6f0625265..3868cbf82 100644 --- a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexSearchTest.java +++ b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexSearchTest.java @@ -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 {