コード例 #1
0
 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;
 }