FileDocCategorySizeDatePackage
DisjunctionMaxQuery.javaAPI DocApache Lucene 2.0.010538Fri May 26 09:54:18 BST 2006org.apache.lucene.search

DisjunctionMaxQuery

public class DisjunctionMaxQuery extends Query
A query that generates the union of the documents produced by its subqueries, and that scores each document as the maximum score for that document produced by any subquery plus a tie breaking increment for any additional matching subqueries. This is useful to search for a word in multiple fields with different boost factors (so that the fields cannot be combined equivalently into a single search field). We want the primary score to be the one associated with the highest boost, not the sum of the field scores (as BooleanQuery would give). If the query is "albino elephant" this ensures that "albino" matching one field and "elephant" matching another gets a higher score than "albino" matching both fields. To get this result, use both BooleanQuery and DisjunctionMaxQuery: for each term a DisjunctionMaxQuery searches for it in each field, while the set of these DisjunctionMaxQuery's is combined into a BooleanQuery. The tie breaker capability allows results that include the same term in multiple fields to be judged better than results that include this term in only the best of those multiple fields, without confusing this with the better case of two different terms in the multiple fields.
author
Chuck Williams

Fields Summary
private ArrayList
disjuncts
private float
tieBreakerMultiplier
Constructors Summary
public DisjunctionMaxQuery(float tieBreakerMultiplier)
Creates a new empty DisjunctionMaxQuery. Use add() to add the subqueries.

param
tieBreakerMultiplier this score of each non-maximum disjunct for a document is multiplied by this weight and added into the final score. If non-zero, the value should be small, on the order of 0.1, which says that 10 occurrences of word in a lower-scored field that is also in a higher scored field is just as good as a unique word in the lower scored field (i.e., one that is not in any higher scored field.


                                                                                                                   
     
    this.tieBreakerMultiplier = tieBreakerMultiplier;
  
public DisjunctionMaxQuery(Collection disjuncts, float tieBreakerMultiplier)
Creates a new DisjunctionMaxQuery

param
disjuncts a Collection of all the disjuncts to add
param
tieBreakerMultiplier the weight to give to each matching non-maximum disjunct

    this.tieBreakerMultiplier = tieBreakerMultiplier;
    add(disjuncts);
  
Methods Summary
public voidadd(org.apache.lucene.search.Query query)
Add a subquery to this disjunction

param
query the disjunct added

    disjuncts.add(query);
  
public voidadd(java.util.Collection disjuncts)
Add a collection of disjuncts to this disjunction via Iterable

    this.disjuncts.addAll(disjuncts);
  
public java.lang.Objectclone()
Create a shallow copy of us -- used in rewriting if necessary

return
a copy of us (but reuse, don't copy, our subqueries)

    DisjunctionMaxQuery clone = (DisjunctionMaxQuery)super.clone();
    clone.disjuncts = (ArrayList)this.disjuncts.clone();
    return clone;
  
protected org.apache.lucene.search.WeightcreateWeight(org.apache.lucene.search.Searcher searcher)

    return new DisjunctionMaxWeight(searcher);
  
public booleanequals(java.lang.Object o)
Return true iff we represent the same query as o

param
o another object
return
true iff o is a DisjunctionMaxQuery with the same boost and the same subqueries, in the same order, as us

    if (! (o instanceof DisjunctionMaxQuery) ) return false;
    DisjunctionMaxQuery other = (DisjunctionMaxQuery)o;
    return this.getBoost() == other.getBoost()
            && this.tieBreakerMultiplier == other.tieBreakerMultiplier
            && this.disjuncts.equals(other.disjuncts);
  
public voidextractTerms(java.util.Set terms)

      for (int i = 0; i < disjuncts.size(); i++) {
          ((Query)disjuncts.get(i)).extractTerms(terms);
      }
  
public inthashCode()
Compute a hash code for hashing us

return
the hash code

    return Float.floatToIntBits(getBoost())
            + Float.floatToIntBits(tieBreakerMultiplier)
            + disjuncts.hashCode();
  
public java.util.Iteratoriterator()
An Iterator over the disjuncts

    return disjuncts.iterator();
  
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.index.IndexReader reader)
Optimize our representation and our subqueries representations

param
reader the IndexReader we query
return
an optimized copy of us (which may not be a copy if there is nothing to optimize)

    if (disjuncts.size() == 1) {
      Query singleton = (Query) disjuncts.get(0);
      Query result = singleton.rewrite(reader);
      if (getBoost() != 1.0f) {
        if (result == singleton) result = (Query)result.clone();
        result.setBoost(getBoost() * result.getBoost());
      }
      return result;
    }
    DisjunctionMaxQuery clone = null;
    for (int i = 0 ; i < disjuncts.size(); i++) {
      Query clause = (Query) disjuncts.get(i);
      Query rewrite = clause.rewrite(reader);
      if (rewrite != clause) {
        if (clone == null) clone = (DisjunctionMaxQuery)this.clone();
        clone.disjuncts.set(i, rewrite);
      }
    }
    if (clone != null) return clone;
    else return this;
  
public java.lang.StringtoString(java.lang.String field)
Prettyprint us.

param
field the field to which we are applied
return
a string that shows what we do, of the form "(disjunct1 | disjunct2 | ... | disjunctn)^boost"

    StringBuffer buffer = new StringBuffer();
    buffer.append("(");
    for (int i = 0 ; i < disjuncts.size(); i++) {
      Query subquery = (Query) disjuncts.get(i);
      if (subquery instanceof BooleanQuery) {   // wrap sub-bools in parens
        buffer.append("(");
        buffer.append(subquery.toString(field));
        buffer.append(")");
      }
      else buffer.append(subquery.toString(field));
      if (i != disjuncts.size()-1) buffer.append(" | ");
    }
    buffer.append(")");
    if (tieBreakerMultiplier != 0.0f) {
      buffer.append("~");
      buffer.append(tieBreakerMultiplier);
    }
    if (getBoost() != 1.0) {
      buffer.append("^");
      buffer.append(getBoost());
    }
    return buffer.toString();