mirror of https://github.com/apache/archiva.git
refactor queries
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@412288 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
24873267f7
commit
179af2ebd5
|
@ -17,13 +17,9 @@ package org.apache.maven.repository.indexing;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.index.Term;
|
|
||||||
import org.apache.lucene.queryParser.ParseException;
|
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.Hits;
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
import org.apache.lucene.search.TermQuery;
|
|
||||||
import org.apache.maven.artifact.Artifact;
|
import org.apache.maven.artifact.Artifact;
|
||||||
import org.apache.maven.artifact.factory.ArtifactFactory;
|
import org.apache.maven.artifact.factory.ArtifactFactory;
|
||||||
import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
|
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.SnapshotArtifactRepositoryMetadata;
|
||||||
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
|
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
|
||||||
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
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.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.logging.AbstractLogEnabled;
|
||||||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
|
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
|
||||||
|
|
||||||
|
@ -135,31 +127,6 @@ public class DefaultRepositoryIndexSearcher
|
||||||
return docs;
|
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
|
* 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 )
|
private org.apache.lucene.search.Query createLuceneQuery( Query query )
|
||||||
throws ParseException
|
throws ParseException
|
||||||
{
|
{
|
||||||
org.apache.lucene.search.Query retVal;
|
return query.createLuceneQuery( index );
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -16,7 +16,12 @@ package org.apache.maven.repository.indexing.query;
|
||||||
* limitations under the License.
|
* 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.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,4 +84,20 @@ public class CompoundQuery
|
||||||
{
|
{
|
||||||
return queries;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package org.apache.maven.repository.indexing.query;
|
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.
|
* Copyright 2001-2005 The Apache Software Foundation.
|
||||||
*
|
*
|
||||||
|
@ -24,4 +27,6 @@ package org.apache.maven.repository.indexing.query;
|
||||||
*/
|
*/
|
||||||
public interface Query
|
public interface Query
|
||||||
{
|
{
|
||||||
|
org.apache.lucene.search.Query createLuceneQuery( RepositoryIndex index )
|
||||||
|
throws ParseException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,11 @@ package org.apache.maven.repository.indexing.query;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import org.apache.lucene.index.Term;
|
||||||
|
import org.apache.maven.repository.indexing.RepositoryIndex;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +31,7 @@ import java.util.List;
|
||||||
public class RangeQuery
|
public class RangeQuery
|
||||||
implements Query
|
implements Query
|
||||||
{
|
{
|
||||||
List queries = new ArrayList();
|
private List queries = new ArrayList();
|
||||||
|
|
||||||
private boolean inclusive;
|
private boolean inclusive;
|
||||||
|
|
||||||
|
@ -50,4 +54,20 @@ public class RangeQuery
|
||||||
{
|
{
|
||||||
return inclusive;
|
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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
package org.apache.maven.repository.indexing.query;
|
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.
|
* Copyright 2001-2005 The Apache Software Foundation.
|
||||||
*
|
*
|
||||||
|
@ -60,4 +66,21 @@ public class SinglePhraseQuery
|
||||||
{
|
{
|
||||||
return value;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue