diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java index e1a119382..56ae555e9 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java @@ -17,13 +17,9 @@ package org.apache.maven.repository.indexing; */ import org.apache.lucene.document.Document; -import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.ParseException; -import org.apache.lucene.queryParser.QueryParser; -import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.search.TermQuery; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; @@ -32,11 +28,7 @@ import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.repository.indexing.query.CompoundQuery; -import org.apache.maven.repository.indexing.query.CompoundQueryTerm; import org.apache.maven.repository.indexing.query.Query; -import org.apache.maven.repository.indexing.query.RangeQuery; -import org.apache.maven.repository.indexing.query.SinglePhraseQuery; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -135,31 +127,6 @@ public class DefaultRepositoryIndexSearcher return docs; } - /** - * Method to create a lucene Query object from a single query phrase - * - * @param field the index field name to search into - * @param value the index field value to match the field with - * @return a lucene Query object representing the query phrase field = value - * @throws ParseException - */ - private org.apache.lucene.search.Query createLuceneQuery( String field, String value ) - throws ParseException - { - org.apache.lucene.search.Query qry; - if ( index.isKeywordField( field ) ) - { - Term term = new Term( field, value ); - qry = new TermQuery( term ); - } - else - { - QueryParser parser = new QueryParser( field, index.getAnalyzer() ); - qry = parser.parse( value ); - } - return qry; - } - /** * Method to create a lucene Query object by converting a prepared Query object * @@ -170,44 +137,7 @@ public class DefaultRepositoryIndexSearcher private org.apache.lucene.search.Query createLuceneQuery( Query query ) throws ParseException { - org.apache.lucene.search.Query retVal; - - if ( query instanceof CompoundQuery ) - { - BooleanQuery booleanQuery = new BooleanQuery(); - CompoundQuery compoundQuery = (CompoundQuery) query; - List queries = compoundQuery.getQueries(); - for ( Iterator i = queries.iterator(); i.hasNext(); ) - { - CompoundQueryTerm subquery = (CompoundQueryTerm) i.next(); - - org.apache.lucene.search.Query luceneQuery = createLuceneQuery( subquery.getQuery() ); - - booleanQuery.add( luceneQuery, subquery.isRequired(), subquery.isProhibited() ); - } - retVal = booleanQuery; - } - else if ( query instanceof RangeQuery ) - { - RangeQuery rq = (RangeQuery) query; - List queries = rq.getQueries(); - Iterator iter = queries.iterator(); - Term begin = null, end = null; - if ( queries.size() == 2 ) - { - SinglePhraseQuery qry = (SinglePhraseQuery) iter.next(); - begin = new Term( qry.getField(), qry.getValue() ); - qry = (SinglePhraseQuery) iter.next(); - end = new Term( qry.getField(), qry.getValue() ); - } - retVal = new org.apache.lucene.search.RangeQuery( begin, end, rq.isInclusive() ); - } - else - { - SinglePhraseQuery singlePhraseQuery = (SinglePhraseQuery) query; - retVal = createLuceneQuery( singlePhraseQuery.getField(), singlePhraseQuery.getValue() ); - } - return retVal; + return query.createLuceneQuery( index ); } /** diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQuery.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQuery.java index 0480ea55c..72d2476c2 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQuery.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQuery.java @@ -16,7 +16,12 @@ package org.apache.maven.repository.indexing.query; * limitations under the License. */ +import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.search.BooleanQuery; +import org.apache.maven.repository.indexing.RepositoryIndex; + import java.util.ArrayList; +import java.util.Iterator; import java.util.List; /** @@ -79,4 +84,20 @@ public class CompoundQuery { return queries; } + + public org.apache.lucene.search.Query createLuceneQuery( RepositoryIndex index ) + throws ParseException + { + BooleanQuery booleanQuery = new BooleanQuery(); + List queries = this.queries; + for ( Iterator i = queries.iterator(); i.hasNext(); ) + { + CompoundQueryTerm subquery = (CompoundQueryTerm) i.next(); + + org.apache.lucene.search.Query luceneQuery = subquery.getQuery().createLuceneQuery( index ); + + booleanQuery.add( luceneQuery, subquery.isRequired(), subquery.isProhibited() ); + } + return booleanQuery; + } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/Query.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/Query.java index d5a70d133..e86e3ec15 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/Query.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/Query.java @@ -1,5 +1,8 @@ package org.apache.maven.repository.indexing.query; +import org.apache.lucene.queryParser.ParseException; +import org.apache.maven.repository.indexing.RepositoryIndex; + /* * Copyright 2001-2005 The Apache Software Foundation. * @@ -24,4 +27,6 @@ package org.apache.maven.repository.indexing.query; */ public interface Query { + org.apache.lucene.search.Query createLuceneQuery( RepositoryIndex index ) + throws ParseException; } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RangeQuery.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RangeQuery.java index 84b355a28..2037a9ade 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RangeQuery.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RangeQuery.java @@ -16,7 +16,11 @@ package org.apache.maven.repository.indexing.query; * limitations under the License. */ +import org.apache.lucene.index.Term; +import org.apache.maven.repository.indexing.RepositoryIndex; + import java.util.ArrayList; +import java.util.Iterator; import java.util.List; /** @@ -27,7 +31,7 @@ import java.util.List; public class RangeQuery implements Query { - List queries = new ArrayList(); + private List queries = new ArrayList(); private boolean inclusive; @@ -50,4 +54,20 @@ public class RangeQuery { return inclusive; } + + public org.apache.lucene.search.Query createLuceneQuery( RepositoryIndex index ) + { + List queries = this.queries; + Iterator iter = queries.iterator(); + Term begin = null; + Term end = null; + if ( queries.size() == 2 ) + { + SinglePhraseQuery qry = (SinglePhraseQuery) iter.next(); + begin = new Term( qry.getField(), qry.getValue() ); + qry = (SinglePhraseQuery) iter.next(); + end = new Term( qry.getField(), qry.getValue() ); + } + return new org.apache.lucene.search.RangeQuery( begin, end, this.inclusive ); + } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/SinglePhraseQuery.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/SinglePhraseQuery.java index 6d25a7e65..d41ca82fb 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/SinglePhraseQuery.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/SinglePhraseQuery.java @@ -1,5 +1,11 @@ package org.apache.maven.repository.indexing.query; +import org.apache.lucene.index.Term; +import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.queryParser.QueryParser; +import org.apache.lucene.search.TermQuery; +import org.apache.maven.repository.indexing.RepositoryIndex; + /* * Copyright 2001-2005 The Apache Software Foundation. * @@ -60,4 +66,21 @@ public class SinglePhraseQuery { return value; } + + public org.apache.lucene.search.Query createLuceneQuery( RepositoryIndex index ) + throws ParseException + { + org.apache.lucene.search.Query qry; + if ( index.isKeywordField( this.field ) ) + { + Term term = new Term( this.field, this.value ); + qry = new TermQuery( term ); + } + else + { + QueryParser parser = new QueryParser( this.field, index.getAnalyzer() ); + qry = parser.parse( this.value ); + } + return qry; + } }