public Query createPhrasePrefixQuery(
     String field, String queryText, int phraseSlop, int maxExpansions) {
   final Query query =
       createFieldQuery(getAnalyzer(), Occur.MUST, field, queryText, true, phraseSlop);
   final MultiPhrasePrefixQuery prefixQuery = new MultiPhrasePrefixQuery();
   prefixQuery.setMaxExpansions(maxExpansions);
   prefixQuery.setSlop(phraseSlop);
   if (query instanceof PhraseQuery) {
     PhraseQuery pq = (PhraseQuery) query;
     Term[] terms = pq.getTerms();
     int[] positions = pq.getPositions();
     for (int i = 0; i < terms.length; i++) {
       prefixQuery.add(new Term[] {terms[i]}, positions[i]);
     }
     return prefixQuery;
   } else if (query instanceof MultiPhraseQuery) {
     MultiPhraseQuery pq = (MultiPhraseQuery) query;
     Term[][] terms = pq.getTermArrays();
     int[] positions = pq.getPositions();
     for (int i = 0; i < terms.length; i++) {
       prefixQuery.add(terms[i], positions[i]);
     }
     return prefixQuery;
   } else if (query instanceof TermQuery) {
     prefixQuery.add(((TermQuery) query).getTerm());
     return prefixQuery;
   }
   return query;
 }
  /**
   * Add multiple terms at the next position in the phrase. Any of the terms may match.
   *
   * @see org.apache.lucene.search.PhraseQuery.Builder#add(Term)
   */
  public void add(Term[] terms) {
    int position = 0;
    if (positions.size() > 0) position = positions.get(positions.size() - 1) + 1;

    add(terms, position);
  }
 /**
  * Add a single term at the next position in the phrase.
  *
  * @see org.apache.lucene.search.PhraseQuery.Builder#add(Term)
  */
 public void add(Term term) {
   add(new Term[] {term});
 }