]> source.dussan.org Git - archiva.git/commitdiff
refactor queries
authorBrett Porter <brett@apache.org>
Wed, 7 Jun 2006 04:32:17 +0000 (04:32 +0000)
committerBrett Porter <brett@apache.org>
Wed, 7 Jun 2006 04:32:17 +0000 (04:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@412288 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/CompoundQuery.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/Query.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RangeQuery.java
maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/SinglePhraseQuery.java

index e1a119382e7d5136bdb330e5b21c8d6da49d8399..56ae555e96e12418808f563a16033f58ab9d2391 100644 (file)
@@ -17,13 +17,9 @@ package org.apache.maven.repository.indexing;
  */\r
 \r
 import org.apache.lucene.document.Document;\r
-import org.apache.lucene.index.Term;\r
 import org.apache.lucene.queryParser.ParseException;\r
-import org.apache.lucene.queryParser.QueryParser;\r
-import org.apache.lucene.search.BooleanQuery;\r
 import org.apache.lucene.search.Hits;\r
 import org.apache.lucene.search.IndexSearcher;\r
-import org.apache.lucene.search.TermQuery;\r
 import org.apache.maven.artifact.Artifact;\r
 import org.apache.maven.artifact.factory.ArtifactFactory;\r
 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;\r
@@ -32,11 +28,7 @@ import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;\r
 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;\r
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;\r
-import org.apache.maven.repository.indexing.query.CompoundQuery;\r
-import org.apache.maven.repository.indexing.query.CompoundQueryTerm;\r
 import org.apache.maven.repository.indexing.query.Query;\r
-import org.apache.maven.repository.indexing.query.RangeQuery;\r
-import org.apache.maven.repository.indexing.query.SinglePhraseQuery;\r
 import org.codehaus.plexus.logging.AbstractLogEnabled;\r
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;\r
 \r
@@ -135,31 +127,6 @@ public class DefaultRepositoryIndexSearcher
         return docs;\r
     }\r
 \r
-    /**\r
-     * Method to create a lucene Query object from a single query phrase\r
-     *\r
-     * @param field the index field name to search into\r
-     * @param value the index field value to match the field with\r
-     * @return a lucene Query object representing the query phrase field = value\r
-     * @throws ParseException\r
-     */\r
-    private org.apache.lucene.search.Query createLuceneQuery( String field, String value )\r
-        throws ParseException\r
-    {\r
-        org.apache.lucene.search.Query qry;\r
-        if ( index.isKeywordField( field ) )\r
-        {\r
-            Term term = new Term( field, value );\r
-            qry = new TermQuery( term );\r
-        }\r
-        else\r
-        {\r
-            QueryParser parser = new QueryParser( field, index.getAnalyzer() );\r
-            qry = parser.parse( value );\r
-        }\r
-        return qry;\r
-    }\r
-\r
     /**\r
      * Method to create a lucene Query object by converting a prepared Query object\r
      *\r
@@ -170,44 +137,7 @@ public class DefaultRepositoryIndexSearcher
     private org.apache.lucene.search.Query createLuceneQuery( Query query )\r
         throws ParseException\r
     {\r
-        org.apache.lucene.search.Query retVal;\r
-\r
-        if ( query instanceof CompoundQuery )\r
-        {\r
-            BooleanQuery booleanQuery = new BooleanQuery();\r
-            CompoundQuery compoundQuery = (CompoundQuery) query;\r
-            List queries = compoundQuery.getQueries();\r
-            for ( Iterator i = queries.iterator(); i.hasNext(); )\r
-            {\r
-                CompoundQueryTerm subquery = (CompoundQueryTerm) i.next();\r
-\r
-                org.apache.lucene.search.Query luceneQuery = createLuceneQuery( subquery.getQuery() );\r
-\r
-                booleanQuery.add( luceneQuery, subquery.isRequired(), subquery.isProhibited() );\r
-            }\r
-            retVal = booleanQuery;\r
-        }\r
-        else if ( query instanceof RangeQuery )\r
-        {\r
-            RangeQuery rq = (RangeQuery) query;\r
-            List queries = rq.getQueries();\r
-            Iterator iter = queries.iterator();\r
-            Term begin = null, end = null;\r
-            if ( queries.size() == 2 )\r
-            {\r
-                SinglePhraseQuery qry = (SinglePhraseQuery) iter.next();\r
-                begin = new Term( qry.getField(), qry.getValue() );\r
-                qry = (SinglePhraseQuery) iter.next();\r
-                end = new Term( qry.getField(), qry.getValue() );\r
-            }\r
-            retVal = new org.apache.lucene.search.RangeQuery( begin, end, rq.isInclusive() );\r
-        }\r
-        else\r
-        {\r
-            SinglePhraseQuery singlePhraseQuery = (SinglePhraseQuery) query;\r
-            retVal = createLuceneQuery( singlePhraseQuery.getField(), singlePhraseQuery.getValue() );\r
-        }\r
-        return retVal;\r
+        return query.createLuceneQuery( index );\r
     }\r
 \r
     /**\r
index 0480ea55ce4cdbee97df66793c44c17e972264f0..72d2476c2b19e7fadd0b8289111ec2b722fb2eae 100644 (file)
@@ -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;
+    }
 }
index d5a70d133c24c639937fb35f65bcd7a85d997a11..e86e3ec156440de4d4435897be9f0d1ac965dd39 100644 (file)
@@ -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;
 }
index 84b355a28251d80ac2f353feef5f04bb602cc09b..2037a9ade9550917ae88b4f9221c854d6cebc559 100644 (file)
@@ -16,7 +16,11 @@ package org.apache.maven.repository.indexing.query;
  * limitations under the License.\r
  */\r
 \r
+import org.apache.lucene.index.Term;\r
+import org.apache.maven.repository.indexing.RepositoryIndex;\r
+\r
 import java.util.ArrayList;\r
+import java.util.Iterator;\r
 import java.util.List;\r
 \r
 /**\r
@@ -27,7 +31,7 @@ import java.util.List;
 public class RangeQuery\r
     implements Query\r
 {\r
-    List queries = new ArrayList();\r
+    private List queries = new ArrayList();\r
 \r
     private boolean inclusive;\r
 \r
@@ -50,4 +54,20 @@ public class RangeQuery
     {\r
         return inclusive;\r
     }\r
+\r
+    public org.apache.lucene.search.Query createLuceneQuery( RepositoryIndex index )\r
+    {\r
+        List queries = this.queries;\r
+        Iterator iter = queries.iterator();\r
+        Term begin = null;\r
+        Term end = null;\r
+        if ( queries.size() == 2 )\r
+        {\r
+            SinglePhraseQuery qry = (SinglePhraseQuery) iter.next();\r
+            begin = new Term( qry.getField(), qry.getValue() );\r
+            qry = (SinglePhraseQuery) iter.next();\r
+            end = new Term( qry.getField(), qry.getValue() );\r
+        }\r
+        return new org.apache.lucene.search.RangeQuery( begin, end, this.inclusive );\r
+    }\r
 }\r
index 6d25a7e6588954624180479cf53af676946534f7..d41ca82fb6d362cb8eedb06afa5291bcf0ae6740 100644 (file)
@@ -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;
+    }
 }