lucene 4: Upgraded MultiPhrasePrefixQuery.

This commit is contained in:
Martijn van Groningen 2012-10-31 13:35:06 +01:00 committed by Shay Banon
parent da551e8847
commit ddc3eb3415
1 changed files with 23 additions and 16 deletions

View File

@ -19,11 +19,11 @@
package org.elasticsearch.common.lucene.search;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.index.*;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.ToStringUtils;
import java.io.IOException;
@ -152,23 +152,30 @@ public class MultiPhrasePrefixQuery extends Query {
}
private void getPrefixTerms(List<Term> terms, final Term prefix, final IndexReader reader) throws IOException {
TermEnum enumerator = reader.terms(prefix);
try {
do {
Term term = enumerator.term();
if (term != null
&& term.text().startsWith(prefix.text())
&& term.field().equals(field)) {
terms.add(term);
} else {
TermsEnum termsEnum = null;
List<AtomicReaderContext> leaves = reader.leaves();
for (AtomicReaderContext leaf : leaves) {
Terms _terms = leaf.reader().terms(field);
if (_terms == null) {
continue;
}
termsEnum = _terms.iterator(termsEnum);
TermsEnum.SeekStatus seekStatus = termsEnum.seekCeil(prefix.bytes());
if (TermsEnum.SeekStatus.END == seekStatus) {
continue;
}
for (BytesRef term = termsEnum.term(); term != null; term = termsEnum.next()) {
if (StringHelper.startsWith(term, prefix.bytes())) {
break;
}
terms.add(new Term(field, BytesRef.deepCopyOf(term)));
if (terms.size() >= maxExpansions) {
break;
return;
}
}
} while (enumerator.next());
} finally {
enumerator.close();
}
}