org.apache.lucene.search
Class QueryRewriter

Object
  extended by QueryRewriter
Direct Known Subclasses:
BigramQueryRewriter, XtfQueryRewriter

public abstract class QueryRewriter
extends Object

Utility class for performing external rewriting, or transformation, tasks on Lucene queries. The base class simply provides a framework. Derived classes should override methods for those parts of a query they need to rewrite, and the base will take care of gluing them together properly.


Nested Class Summary
static interface QueryRewriter.SpanClauseJoiner
          Utility class that joins clauses into an Or query, And query, etc.
 
Constructor Summary
QueryRewriter()
           
 
Method Summary
protected  Query combineBoost(Query oldQuery, Query newQuery)
          Combines the boost value from an old query with that of a newly created one.
protected  Query copyBoost(Query oldQuery, Query newQuery)
          Copies the boost value from an old query to a newly created one.
protected  Query copyBoost(Query oldQuery1, Query oldQuery2, Query newQuery)
          Copies the max boost value from two old queries to a newly created one.
 boolean forceRewrite(Query q)
          Can be used to force some or all queries to be rewritten even if no changes.
protected  Query rewrite(BooleanQuery bq)
          Rewrite a BooleanQuery.
protected  Query rewrite(SpanChunkedNotQuery nq)
          Rewrite a span-based NOT query.
protected  Query rewrite(SpanDechunkingQuery nq)
          Rewrite a span dechunking query.
protected  Query rewrite(SpanNearQuery q)
          Rewrite a span NEAR query.
protected  Query rewrite(SpanNotNearQuery nq)
          Rewrite a span-based NOT query.
protected  Query rewrite(SpanNotQuery nq)
          Rewrite a span-based NOT query.
protected  Query rewrite(SpanOrNearQuery q)
          Rewrite a span OR-NEAR query.
protected  Query rewrite(SpanOrQuery q)
          Rewrite a span-based OR query.
protected  Query rewrite(SpanRangeQuery q)
          Rewrite a span range query.
protected  Query rewrite(SpanTermQuery q)
          Rewrite a span term query.
protected  Query rewrite(SpanWildcardQuery q)
          Rewrite a span wildcard query.
protected  Query rewrite(TermQuery q)
          Rewrite a term query.
protected  Query rewriteClauses(Query oldQuery, SpanQuery[] oldClauses, boolean promoteSingle, QueryRewriter.SpanClauseJoiner joiner)
          Utility function that takes care of rewriting a series of span query clauses.
 Query rewriteQuery(Query q)
          Rewrite a query of any supported type.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QueryRewriter

public QueryRewriter()
Method Detail

rewriteQuery

public Query rewriteQuery(Query q)
Rewrite a query of any supported type.

Parameters:
q - Query to rewrite
Returns:
A new query, or 'q' unchanged if no change was needed.

forceRewrite

public boolean forceRewrite(Query q)
Can be used to force some or all queries to be rewritten even if no changes. This is useful for copying queries, or easily making changes to them. The base class always returns false; derived classes should override this method if they want copying behavior.


rewrite

protected Query rewrite(BooleanQuery bq)
Rewrite a BooleanQuery.

Parameters:
bq - The query to rewrite
Returns:
Rewritten version, or 'bq' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanNearQuery q)
Rewrite a span NEAR query.

Parameters:
q - The query to rewrite
Returns:
Rewritten version, or 'q' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanOrNearQuery q)
Rewrite a span OR-NEAR query.

Parameters:
q - The query to rewrite
Returns:
Rewritten version, or 'nq' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanOrQuery q)
Rewrite a span-based OR query.

Parameters:
q - The query to rewrite
Returns:
Rewritten version, or 'oq' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanChunkedNotQuery nq)
Rewrite a span-based NOT query. The procedure in this case is simple: simply rewrite both the include and exclude clauses.

Parameters:
nq - The query to rewrite
Returns:
Rewritten version, or 'nq' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanNotQuery nq)
Rewrite a span-based NOT query. The procedure in this case is simple: simply rewrite both the include and exclude clauses.

Parameters:
nq - The query to rewrite
Returns:
Rewritten version, or 'nq' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanNotNearQuery nq)
Rewrite a span-based NOT query. The procedure in this case is simple: simply rewrite both the include and exclude clauses.

Parameters:
nq - The query to rewrite
Returns:
Rewritten version, or 'nq' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanDechunkingQuery nq)
Rewrite a span dechunking query. If's very simple: simply rewrite the clause the query wraps.

Parameters:
nq - The query to rewrite
Returns:
Rewritten version, or 'nq' unchanged if no changed needed.

rewrite

protected Query rewrite(TermQuery q)
Rewrite a term query. The base class does nothing.

Parameters:
q - The query to rewrite
Returns:
Rewritten version, or 'q' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanTermQuery q)
Rewrite a span term query. The base class does nothing unless rewriting is forced.

Parameters:
q - The query to rewrite
Returns:
Rewritten version, or 'q' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanWildcardQuery q)
Rewrite a span wildcard query. The base class does nothing unless rewriting is forced.

Parameters:
q - The query to rewrite
Returns:
Rewritten version, or 'q' unchanged if no changed needed.

rewrite

protected Query rewrite(SpanRangeQuery q)
Rewrite a span range query. The base class does nothing unless rewriting is forced.

Parameters:
q - The query to rewrite
Returns:
Rewritten version, or 'q' unchanged if no changed needed.

copyBoost

protected Query copyBoost(Query oldQuery,
                          Query newQuery)
Copies the boost value from an old query to a newly created one. Also copies the spanRecording attribute. Returns the new query for ease of chaining.

Parameters:
oldQuery - Query to copy from
newQuery - Query to copy to
Returns:
Value of 'newQuery' (useful for chaining)

copyBoost

protected Query copyBoost(Query oldQuery1,
                          Query oldQuery2,
                          Query newQuery)
Copies the max boost value from two old queries to a newly created one. Also copies the spanRecording attribute. Returns the new query for ease of chaining.

Parameters:
oldQuery1 - First query to copy from
oldQuery2 - Second query to copy from
newQuery - Query to copy to
Returns:
Value of 'newQuery' (useful for chaining)

combineBoost

protected Query combineBoost(Query oldQuery,
                             Query newQuery)
Combines the boost value from an old query with that of a newly created one. Also preserves the spanRecording attribute. Returns the new query for ease of chaining.

Parameters:
oldQuery - Query to combine from
newQuery - Query to combine to
Returns:
Value of 'newQuery' (useful for chaining)

rewriteClauses

protected Query rewriteClauses(Query oldQuery,
                               SpanQuery[] oldClauses,
                               boolean promoteSingle,
                               QueryRewriter.SpanClauseJoiner joiner)
Utility function that takes care of rewriting a series of span query clauses.

Parameters:
oldQuery - Query being rewritten
oldClauses - Clauses to rewrite
promoteSingle - true to allow single-clause result to be returned, false to force wrapping.
joiner - Handles joining new clauses into wrapper query
Returns:
New rewritten query, or 'oldQuery' if no changes.