From 1c54e2df1650a583a6a8a993ba8e6f481730f7dc Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Thu, 27 Jul 2006 07:09:38 +0000 Subject: [PATCH] [MRM-127] clean up query objects by removing Lucene specifics (refactoring will continue towards something simpler) git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@425984 13f79535-47bb-0310-9956-ffa450edef68 --- .../DefaultRepositoryIndexSearcher.java | 67 ++++++++++++++++++- .../indexing/query/CompoundQuery.java | 22 ------ .../indexing/query/CompoundQueryTerm.java | 8 +-- .../repository/indexing/query/Query.java | 5 -- .../repository/indexing/query/RangeQuery.java | 20 ------ .../indexing/query/SingleTermQuery.java | 26 ------- 6 files changed, 70 insertions(+), 78 deletions(-) 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 5525ed57d..c636dbb8e 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,9 +17,14 @@ 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.BooleanClause; +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.ArtifactRepository; @@ -31,7 +36,11 @@ import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryM import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; import org.apache.maven.model.Model; 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.SingleTermQuery; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -44,6 +53,7 @@ import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.StringTokenizer; @@ -68,7 +78,7 @@ public class DefaultRepositoryIndexSearcher org.apache.lucene.search.Query luceneQuery; try { - luceneQuery = query.createLuceneQuery( index ); + luceneQuery = createLuceneQuery( query, index ); } catch ( ParseException e ) { @@ -118,6 +128,61 @@ public class DefaultRepositoryIndexSearcher return docs; } + private org.apache.lucene.search.Query createLuceneQuery( Query query, RepositoryIndex index ) + throws ParseException + { + org.apache.lucene.search.Query luceneQuery = null; + // TODO: hacked in temporarily + if ( query instanceof CompoundQuery ) + { + BooleanQuery booleanQuery = new BooleanQuery(); + List queries = ( (CompoundQuery) query ).getCompoundQueryTerms(); + for ( Iterator i = queries.iterator(); i.hasNext(); ) + { + CompoundQueryTerm queryTerm = (CompoundQueryTerm) i.next(); + + booleanQuery.add( createLuceneQuery( queryTerm.getQuery(), index ), queryTerm.isRequired() + ? BooleanClause.Occur.MUST + : queryTerm.isProhibited() ? BooleanClause.Occur.MUST_NOT : BooleanClause.Occur.SHOULD ); + } + luceneQuery = booleanQuery; + } + else if ( query instanceof SingleTermQuery ) + { + org.apache.lucene.search.Query qry; + if ( index.isKeywordField( ( (SingleTermQuery) query ).getField() ) ) + { + qry = new TermQuery( + new Term( ( (SingleTermQuery) query ).getField(), ( (SingleTermQuery) query ).getValue() ) ); + } + else + { + // TODO: doesn't seem like the right place for this here! + QueryParser parser = new QueryParser( ( (SingleTermQuery) query ).getField(), index.getAnalyzer() ); + qry = parser.parse( ( (SingleTermQuery) query ).getValue() ); + } + luceneQuery = qry; + } + else if ( query instanceof RangeQuery ) + { + Term beginTerm = null; + if ( ( (RangeQuery) query ).getBegin() != null ) + { + beginTerm = new Term( ( (RangeQuery) query ).getBegin().getField(), + ( (RangeQuery) query ).getBegin().getValue() ); + } + Term endTerm = null; + if ( ( (RangeQuery) query ).getEnd() != null ) + { + endTerm = + new Term( ( (RangeQuery) query ).getEnd().getField(), ( (RangeQuery) query ).getEnd().getValue() ); + } + luceneQuery = + new org.apache.lucene.search.RangeQuery( beginTerm, endTerm, ( (RangeQuery) query ).isInclusive() ); + } + return luceneQuery; + } + private RepositoryIndexSearchHit createSearchedObjectFromIndexDocument( Document doc, ArtifactRepository repository ) throws RepositoryIndexSearchException 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 6de8559a8..85760b621 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,13 +16,7 @@ package org.apache.maven.repository.indexing.query; * limitations under the License. */ -import org.apache.lucene.queryParser.ParseException; -import org.apache.lucene.search.BooleanClause; -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; /** @@ -108,20 +102,4 @@ public class CompoundQuery return compoundQueryTerms; } - // TODO! relocate - public org.apache.lucene.search.Query createLuceneQuery( RepositoryIndex index ) - throws ParseException - { - BooleanQuery booleanQuery = new BooleanQuery(); - List queries = this.compoundQueryTerms; - for ( Iterator i = queries.iterator(); i.hasNext(); ) - { - CompoundQueryTerm queryTerm = (CompoundQueryTerm) i.next(); - - booleanQuery.add( queryTerm.getQuery().createLuceneQuery( index ), queryTerm.isRequired() - ? BooleanClause.Occur.MUST - : queryTerm.isProhibited() ? BooleanClause.Occur.MUST_NOT : BooleanClause.Occur.SHOULD ); - } - return booleanQuery; - } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQueryTerm.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQueryTerm.java index 46507375f..abc99bd13 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQueryTerm.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQueryTerm.java @@ -21,7 +21,7 @@ package org.apache.maven.repository.indexing.query; * * @author Brett Porter */ -class CompoundQueryTerm +public class CompoundQueryTerm { /** * The query to add to the compound query. @@ -57,7 +57,7 @@ class CompoundQueryTerm * * @return true if this Query is a search requirement, otherwise returns false */ - boolean isRequired() + public boolean isRequired() { return required; } @@ -67,7 +67,7 @@ class CompoundQueryTerm * * @return true if this Query is prohibited in the search result */ - boolean isProhibited() + public boolean isProhibited() { return prohibited; } @@ -78,7 +78,7 @@ class CompoundQueryTerm * * @return the query */ - Query getQuery() + public Query getQuery() { return query; } 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 515524121..2c6ac6137 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 @@ -16,9 +16,6 @@ package org.apache.maven.repository.indexing.query; * limitations under the License. */ -import org.apache.lucene.queryParser.ParseException; -import org.apache.maven.repository.indexing.RepositoryIndex; - /** * Interface to label the query classes * @@ -26,6 +23,4 @@ import org.apache.maven.repository.indexing.RepositoryIndex; */ 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 632934a32..68e6d4a6a 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,9 +16,6 @@ package org.apache.maven.repository.indexing.query; * limitations under the License. */ -import org.apache.lucene.index.Term; -import org.apache.maven.repository.indexing.RepositoryIndex; - /** * Query object that handles range queries (presently used for dates). * @@ -150,21 +147,4 @@ public class RangeQuery return inclusive; } - /** - * @todo! this seems like the wrong place for this (it's back to front - create the query from the index - */ - public org.apache.lucene.search.Query createLuceneQuery( RepositoryIndex index ) - { - Term beginTerm = null; - if ( begin != null ) - { - beginTerm = new Term( begin.getField(), begin.getValue() ); - } - Term endTerm = null; - if ( end != null ) - { - endTerm = new Term( end.getField(), end.getValue() ); - } - return new org.apache.lucene.search.RangeQuery( beginTerm, endTerm, inclusive ); - } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/SingleTermQuery.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/SingleTermQuery.java index 607b6a84e..f7ae019fa 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/SingleTermQuery.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/SingleTermQuery.java @@ -16,12 +16,6 @@ package org.apache.maven.repository.indexing.query; * limitations under the License. */ -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; - /** * Query for a single term. * @@ -56,26 +50,6 @@ public class SingleTermQuery this.term = new QueryTerm( field, value ); } - /** - * @todo! this seems like the wrong place for this (it's back to front - create the query from the index - */ - public org.apache.lucene.search.Query createLuceneQuery( RepositoryIndex index ) - throws ParseException - { - org.apache.lucene.search.Query qry; - if ( index.isKeywordField( term.getField() ) ) - { - qry = new TermQuery( new Term( term.getField(), term.getValue() ) ); - } - else - { - // TODO: doesn't seem like the right place for this here! - QueryParser parser = new QueryParser( term.getField(), index.getAnalyzer() ); - qry = parser.parse( term.getValue() ); - } - return qry; - } - public String getField() { return term.getField(); -- 2.39.5