FileDocCategorySizeDatePackage
CustomScoreQuery.javaAPI DocApache Lucene 2.2.012791Sat Jun 16 22:20:34 BST 2007org.apache.lucene.search.function

CustomScoreQuery

public class CustomScoreQuery extends Query
Query that sets document score as a programmatic function of (up to) two (sub) scores.
  1. the score of its subQuery (any query)
  2. (optional) the score of its ValueSourtceQuery, for most simple/convineient use case this query would be a {@link org.apache.lucene.search.function.FieldScoreQuery FieldScoreQuery}
Subclasses can modify the computation by overriding {@link #customScore(int, float, float)}.

WARNING: The status of the search.function package is experimental. The APIs introduced here might change in the future and will not be supported anymore in such a case.

Fields Summary
private Query
subQuery
private ValueSourceQuery
valSrcQuery
private boolean
strict
Constructors Summary
public CustomScoreQuery(Query subQuery)
Create a CustomScoreQuery over input subQuery.

param
subQuery the sub query whose scored is being customed. Must not be null.

 // if true, valueSource part of query does not take part in weights normalization.  
  
                          
     
    this(subQuery,null);
  
public CustomScoreQuery(Query subQuery, ValueSourceQuery valSrcQuery)
Create a CustomScoreQuery over input subQuery and a {@link ValueSourceQuery}.

param
subQuery the sub query whose score is being customed. Must not be null.
param
valSrcQuery a value source query whose scores are used in the custom score computation. For most simple/convineient use case this would be a {@link org.apache.lucene.search.function.FieldScoreQuery FieldScoreQuery}. This parameter is optional - it can be null.

    super();
    this.subQuery = subQuery;
    this.valSrcQuery = valSrcQuery;
    if (subQuery == null) throw new IllegalArgumentException("<subqyery> must not be null!");
  
Methods Summary
public java.lang.Objectclone()

    CustomScoreQuery clone = (CustomScoreQuery)super.clone();
    clone.subQuery = (Query) subQuery.clone();
    if (valSrcQuery!=null) {
      clone.valSrcQuery = (ValueSourceQuery) valSrcQuery.clone();
    }
    return clone;
  
protected org.apache.lucene.search.WeightcreateWeight(org.apache.lucene.search.Searcher searcher)

    return new CustomWeight(searcher);
  
public org.apache.lucene.search.ExplanationcustomExplain(int doc, org.apache.lucene.search.Explanation subQueryExpl, org.apache.lucene.search.Explanation valSrcExpl)
Explain the custom score. Whenever overriding {@link #customScore(int, float, float)}, this method should also be overriden to provide the correct explanation for the part of the custom scoring.

param
doc doc being explained.
param
subQueryExpl explanation for the sub-query part.
param
valSrcExpl explanation for the value source part.
return
an explanation for the custom score

    float valSrcScore = valSrcExpl==null ? 1 : valSrcExpl.getValue();
    Explanation exp = new Explanation( valSrcScore * subQueryExpl.getValue(), "custom score: product of:");
    exp.addDetail(subQueryExpl);
    if (valSrcExpl != null) {
      exp.addDetail(valSrcExpl);
    }
    return exp;
  
public floatcustomScore(int doc, float subQueryScore, float valSrcScore)
Compute a custom score by the subQuery score and the ValueSourceQuery score.

Subclasses can override this method to modify the custom score.

The default computation herein is:

ModifiedScore = valSrcScore * subQueryScore.

param
doc id of scored doc.
param
subQueryScore score of that doc by the subQuery.
param
valSrcScore score of that doc by the ValueSourceQuery.
return
custom score.

    return valSrcScore * subQueryScore;
  
public booleanequals(java.lang.Object o)
Returns true if o is equal to this.

    if (getClass() != o.getClass()) {
      return false;
    }
    CustomScoreQuery other = (CustomScoreQuery)o;
    return this.getBoost() == other.getBoost()
           && this.subQuery.equals(other.subQuery)
           && (this.valSrcQuery==null ? other.valSrcQuery==null 
               : this.valSrcQuery.equals(other.valSrcQuery));
  
public voidextractTerms(java.util.Set terms)

    subQuery.extractTerms(terms);
    if (valSrcQuery!=null) {
      valSrcQuery.extractTerms(terms);
    }
  
public inthashCode()
Returns a hash code value for this object.

    int valSrcHash = valSrcQuery==null ? 0 : valSrcQuery.hashCode();
    return (getClass().hashCode() + subQuery.hashCode() + valSrcHash) ^ Float.floatToIntBits(getBoost());
  
public booleanisStrict()
Checks if this is strict custom scoring. In strict custom scoring, the ValueSource part of does not participate in weight normalization. This may be useful when one wants full control over how scores are modified, and does not care about normalizing by the ValueSource part. One particular case where this is useful if for testing this query.

Note: only has effect when the ValueSource part is not null.

    return strict;
  
public java.lang.Stringname()
A short name of this query, used in {@link #toString(String)}.

    return "custom";
  
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.index.IndexReader reader)

    subQuery = subQuery.rewrite(reader);
    if (valSrcQuery!=null) {
      valSrcQuery = (ValueSourceQuery) valSrcQuery.rewrite(reader);
    }
    return this;
  
public voidsetStrict(boolean strict)
Set the strict mode of this query.

param
strict The strict mode to set.
see
#isStrict()

    this.strict = strict;
  
public java.lang.StringtoString(java.lang.String field)

    StringBuffer sb = new StringBuffer(name()).append("(");
    sb.append(subQuery.toString(field));
    if (valSrcQuery!=null) {
      sb.append(", ").append(valSrcQuery.toString(field));
    }
    sb.append(")");
    sb.append(strict?" STRICT" : "");
    return sb.toString() + ToStringUtils.boost(getBoost());