lucene 4: Upgraded MatchNoDocsQuery.

This commit is contained in:
Martijn van Groningen 2012-10-31 13:51:47 +01:00 committed by Shay Banon
parent ddc3eb3415
commit e75c732bdd
2 changed files with 14 additions and 30 deletions

View File

@ -19,9 +19,11 @@
package org.elasticsearch.common.lucene.search; package org.elasticsearch.common.lucene.search;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.*; import org.apache.lucene.search.*;
import org.apache.lucene.util.Bits;
import java.io.IOException; import java.io.IOException;
import java.util.Set; import java.util.Set;
@ -43,20 +45,6 @@ public final class MatchNoDocsQuery extends Query {
* Weight implementation that matches no documents. * Weight implementation that matches no documents.
*/ */
private class MatchNoDocsWeight extends Weight { private class MatchNoDocsWeight extends Weight {
/**
* The similarity implementation.
*/
private final Similarity similarity;
/**
* Creates a new weight that matches nothing.
*
* @param searcher the search to match for
*/
public MatchNoDocsWeight(final Searcher searcher) {
this.similarity = searcher.getSimilarity();
}
@Override @Override
public String toString() { public String toString() {
@ -69,36 +57,29 @@ public final class MatchNoDocsQuery extends Query {
} }
@Override @Override
public float getValue() { public float getValueForNormalization() throws IOException {
return 0; return 0;
} }
@Override @Override
public float sumOfSquaredWeights() { public void normalize(float norm, float topLevelBoost) {
return 0;
} }
@Override @Override
public void normalize(final float queryNorm) { public Scorer scorer(AtomicReaderContext context, boolean scoreDocsInOrder, boolean topScorer, Bits acceptDocs) throws IOException {
}
@Override
public Scorer scorer(final IndexReader reader,
final boolean scoreDocsInOrder,
final boolean topScorer) throws IOException {
return null; return null;
} }
@Override @Override
public Explanation explain(final IndexReader reader, public Explanation explain(final AtomicReaderContext context,
final int doc) { final int doc) {
return new ComplexExplanation(false, 0, "MatchNoDocs matches nothing"); return new ComplexExplanation(false, 0, "MatchNoDocs matches nothing");
} }
} }
@Override @Override
public Weight createWeight(final Searcher searcher) { public Weight createWeight(IndexSearcher searcher) throws IOException {
return new MatchNoDocsWeight(searcher); return new MatchNoDocsWeight();
} }
@Override @Override

View File

@ -19,6 +19,7 @@
package org.elasticsearch.common.lucene.search; package org.elasticsearch.common.lucene.search;
import gnu.trove.set.hash.THashSet;
import org.apache.lucene.index.*; import org.apache.lucene.index.*;
import org.apache.lucene.search.MultiPhraseQuery; import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
@ -137,7 +138,7 @@ public class MultiPhrasePrefixQuery extends Query {
} }
Term[] suffixTerms = termArrays.get(sizeMinus1); Term[] suffixTerms = termArrays.get(sizeMinus1);
int position = positions.get(sizeMinus1); int position = positions.get(sizeMinus1);
List<Term> terms = new ArrayList<Term>(); Set<Term> terms = new THashSet<Term>();
for (Term term : suffixTerms) { for (Term term : suffixTerms) {
getPrefixTerms(terms, term, reader); getPrefixTerms(terms, term, reader);
if (terms.size() > maxExpansions) { if (terms.size() > maxExpansions) {
@ -151,7 +152,9 @@ public class MultiPhrasePrefixQuery extends Query {
return query.rewrite(reader); return query.rewrite(reader);
} }
private void getPrefixTerms(List<Term> terms, final Term prefix, final IndexReader reader) throws IOException { private void getPrefixTerms(Set<Term> terms, final Term prefix, final IndexReader reader) throws IOException {
// SlowCompositeReaderWrapper could be used... but this would merge all terms from each segment into one terms
// instance, which is very expensive. Therefore I think it is better to iterate over each leaf individually.
TermsEnum termsEnum = null; TermsEnum termsEnum = null;
List<AtomicReaderContext> leaves = reader.leaves(); List<AtomicReaderContext> leaves = reader.leaves();
for (AtomicReaderContext leaf : leaves) { for (AtomicReaderContext leaf : leaves) {
@ -167,7 +170,7 @@ public class MultiPhrasePrefixQuery extends Query {
} }
for (BytesRef term = termsEnum.term(); term != null; term = termsEnum.next()) { for (BytesRef term = termsEnum.term(); term != null; term = termsEnum.next()) {
if (StringHelper.startsWith(term, prefix.bytes())) { if (!StringHelper.startsWith(term, prefix.bytes())) {
break; break;
} }