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; package org.elasticsearch.common.lucene.search;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.*;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.search.MultiPhraseQuery; import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.Query; 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 org.apache.lucene.util.ToStringUtils;
import java.io.IOException; 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 { private void getPrefixTerms(List<Term> terms, final Term prefix, final IndexReader reader) throws IOException {
TermEnum enumerator = reader.terms(prefix); TermsEnum termsEnum = null;
try { List<AtomicReaderContext> leaves = reader.leaves();
do { for (AtomicReaderContext leaf : leaves) {
Term term = enumerator.term(); Terms _terms = leaf.reader().terms(field);
if (term != null if (_terms == null) {
&& term.text().startsWith(prefix.text()) continue;
&& term.field().equals(field)) { }
terms.add(term);
} else { 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; break;
} }
terms.add(new Term(field, BytesRef.deepCopyOf(term)));
if (terms.size() >= maxExpansions) { if (terms.size() >= maxExpansions) {
break; return;
}
} }
} while (enumerator.next());
} finally {
enumerator.close();
} }
} }