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

MultiPhraseQuery

public class MultiPhraseQuery extends Query
MultiPhraseQuery is a generalized version of PhraseQuery, with an added method {@link #add(Term[])}. To use this class, to search for the phrase "Microsoft app*" first use add(Term) on the term "Microsoft", then find all terms that have "app" as prefix using IndexReader.terms(Term), and use MultiPhraseQuery.add(Term[] terms) to add them to the query.
author
Anders Nielsen
version
1.0

Fields Summary
private String
field
private ArrayList
termArrays
private Vector
positions
private int
slop
Constructors Summary
Methods Summary
public voidadd(org.apache.lucene.index.Term term)
Add a single term at the next position in the phrase.

see
PhraseQuery#add(Term)

 add(new Term[]{term}); 
public voidadd(org.apache.lucene.index.Term[] terms)
Add multiple terms at the next position in the phrase. Any of the terms may match.

see
PhraseQuery#add(Term)

    int position = 0;
    if (positions.size() > 0)
      position = ((Integer) positions.lastElement()).intValue() + 1;

    add(terms, position);
  
public voidadd(org.apache.lucene.index.Term[] terms, int position)
Allows to specify the relative position of terms within the phrase.

see
PhraseQuery#add(Term, int)
param
terms
param
position

    if (termArrays.size() == 0)
      field = terms[0].field();

    for (int i = 0; i < terms.length; i++) {
      if (terms[i].field() != field) {
        throw new IllegalArgumentException(
            "All phrase terms must be in the same field (" + field + "): "
                + terms[i]);
      }
    }

    termArrays.add(terms);
    positions.addElement(new Integer(position));
  
protected org.apache.lucene.search.WeightcreateWeight(org.apache.lucene.search.Searcher searcher)

    return new MultiPhraseWeight(searcher);
  
public booleanequals(java.lang.Object o)
Returns true if o is equal to this.

    if (!(o instanceof MultiPhraseQuery)) return false;
    MultiPhraseQuery other = (MultiPhraseQuery)o;
    return this.getBoost() == other.getBoost()
      && this.slop == other.slop
      && this.termArrays.equals(other.termArrays)
      && this.positions.equals(other.positions);
  
public voidextractTerms(java.util.Set terms)

    for (Iterator iter = termArrays.iterator(); iter.hasNext();) {
      Term[] arr = (Term[])iter.next();
      for (int i=0; i<arr.length; i++) {
        terms.add(arr[i]);
      }
    }
  
public int[]getPositions()
Returns the relative positions of terms in this phrase.

    int[] result = new int[positions.size()];
    for (int i = 0; i < positions.size(); i++)
      result[i] = ((Integer) positions.elementAt(i)).intValue();
    return result;
  
public intgetSlop()
Sets the phrase slop for this query.

see
PhraseQuery#getSlop()

 return slop; 
public java.util.ListgetTermArrays()
Returns a List of the terms in the multiphrase. Do not modify the List or its contents.

	  return Collections.unmodifiableList(termArrays);
  
public inthashCode()
Returns a hash code value for this object.

    return Float.floatToIntBits(getBoost())
      ^ slop
      ^ termArrays.hashCode()
      ^ positions.hashCode()
      ^ 0x4AC65113;
  
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.index.IndexReader reader)

    if (termArrays.size() == 1) {                 // optimize one-term case
      Term[] terms = (Term[])termArrays.get(0);
      BooleanQuery boq = new BooleanQuery(true);
      for (int i=0; i<terms.length; i++) {
        boq.add(new TermQuery(terms[i]), BooleanClause.Occur.SHOULD);
      }
      boq.setBoost(getBoost());
      return boq;
    } else {
      return this;
    }
  
public voidsetSlop(int s)
Sets the phrase slop for this query.

see
PhraseQuery#setSlop(int)


              
       slop = s; 
public final java.lang.StringtoString(java.lang.String f)
Prints a user-readable version of this query.

    StringBuffer buffer = new StringBuffer();
    if (!field.equals(f)) {
      buffer.append(field);
      buffer.append(":");
    }

    buffer.append("\"");
    Iterator i = termArrays.iterator();
    while (i.hasNext()) {
      Term[] terms = (Term[])i.next();
      if (terms.length > 1) {
        buffer.append("(");
        for (int j = 0; j < terms.length; j++) {
          buffer.append(terms[j].text());
          if (j < terms.length-1)
            buffer.append(" ");
        }
        buffer.append(")");
      } else {
        buffer.append(terms[0].text());
      }
      if (i.hasNext())
        buffer.append(" ");
    }
    buffer.append("\"");

    if (slop != 0) {
      buffer.append("~");
      buffer.append(slop);
    }

    buffer.append(ToStringUtils.boost(getBoost()));

    return buffer.toString();