ソースを参照

[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
tags/archiva-0.9-alpha-1
Brett Porter 18年前
コミット
1c54e2df16

+ 66
- 1
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

+ 0
- 22
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;
}
}

+ 4
- 4
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 <a href="mailto:brett@apache.org">Brett Porter</a>
*/
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;
}

+ 0
- 5
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;
}

+ 0
- 20
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 );
}
}

+ 0
- 26
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();

読み込み中…
キャンセル
保存