mirror of https://github.com/apache/lucene.git
Solr GraphTermsQParser simplifications (#1405)
No WeightOrDocIdSet; no override of bulkScorer (needless)
This commit is contained in:
parent
f4eb586ef6
commit
2dd92fc052
|
@ -22,6 +22,7 @@ import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.lucene.document.DoublePoint;
|
import org.apache.lucene.document.DoublePoint;
|
||||||
import org.apache.lucene.document.FloatPoint;
|
import org.apache.lucene.document.FloatPoint;
|
||||||
|
@ -40,7 +41,6 @@ import org.apache.lucene.index.TermState;
|
||||||
import org.apache.lucene.index.TermStates;
|
import org.apache.lucene.index.TermStates;
|
||||||
import org.apache.lucene.index.Terms;
|
import org.apache.lucene.index.Terms;
|
||||||
import org.apache.lucene.index.TermsEnum;
|
import org.apache.lucene.index.TermsEnum;
|
||||||
import org.apache.lucene.search.BulkScorer;
|
|
||||||
import org.apache.lucene.search.ConstantScoreQuery;
|
import org.apache.lucene.search.ConstantScoreQuery;
|
||||||
import org.apache.lucene.search.ConstantScoreScorer;
|
import org.apache.lucene.search.ConstantScoreScorer;
|
||||||
import org.apache.lucene.search.ConstantScoreWeight;
|
import org.apache.lucene.search.ConstantScoreWeight;
|
||||||
|
@ -56,7 +56,6 @@ import org.apache.lucene.search.Scorer;
|
||||||
import org.apache.lucene.search.Weight;
|
import org.apache.lucene.search.Weight;
|
||||||
import org.apache.lucene.util.Accountable;
|
import org.apache.lucene.util.Accountable;
|
||||||
import org.apache.lucene.util.ArrayUtil;
|
import org.apache.lucene.util.ArrayUtil;
|
||||||
import org.apache.lucene.util.BitDocIdSet;
|
|
||||||
import org.apache.lucene.util.BytesRef;
|
import org.apache.lucene.util.BytesRef;
|
||||||
import org.apache.lucene.util.BytesRefBuilder;
|
import org.apache.lucene.util.BytesRefBuilder;
|
||||||
import org.apache.lucene.util.BytesRefIterator;
|
import org.apache.lucene.util.BytesRefIterator;
|
||||||
|
@ -74,8 +73,7 @@ import org.apache.solr.schema.SchemaField;
|
||||||
* This allows graph traversals to skip traversing high frequency nodes which is often desirable from a performance standpoint.
|
* This allows graph traversals to skip traversing high frequency nodes which is often desirable from a performance standpoint.
|
||||||
*
|
*
|
||||||
* Syntax: {!graphTerms f=field maxDocFreq=10000}term1,term2,term3
|
* Syntax: {!graphTerms f=field maxDocFreq=10000}term1,term2,term3
|
||||||
**/
|
*/
|
||||||
|
|
||||||
public class GraphTermsQParserPlugin extends QParserPlugin {
|
public class GraphTermsQParserPlugin extends QParserPlugin {
|
||||||
public static final String NAME = "graphTerms";
|
public static final String NAME = "graphTerms";
|
||||||
|
|
||||||
|
@ -227,17 +225,7 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString(String defaultField) {
|
public String toString(String defaultField) {
|
||||||
StringBuilder builder = new StringBuilder();
|
return Arrays.stream(this.queryTerms).map(Term::toString).collect(Collectors.joining(","));
|
||||||
boolean first = true;
|
|
||||||
for (Term term : this.queryTerms) {
|
|
||||||
if (!first) {
|
|
||||||
builder.append(',');
|
|
||||||
}
|
|
||||||
first = false;
|
|
||||||
builder.append(term.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -245,21 +233,12 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
|
||||||
visitor.visitLeaf(this);
|
visitor.visitLeaf(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class WeightOrDocIdSet {
|
|
||||||
final Weight weight;
|
|
||||||
final DocIdSet set;
|
|
||||||
|
|
||||||
WeightOrDocIdSet(DocIdSet bitset) {
|
|
||||||
this.set = bitset;
|
|
||||||
this.weight = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
|
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
|
||||||
|
|
||||||
List<TermStates> finalContexts = new ArrayList();
|
List<TermStates> finalContexts = new ArrayList<>();
|
||||||
List<Term> finalTerms = new ArrayList();
|
List<Term> finalTerms = new ArrayList<>();
|
||||||
|
{
|
||||||
List<LeafReaderContext> contexts = searcher.getTopReaderContext().leaves();
|
List<LeafReaderContext> contexts = searcher.getTopReaderContext().leaves();
|
||||||
TermStates[] termStates = new TermStates[this.queryTerms.length];
|
TermStates[] termStates = new TermStates[this.queryTerms.length];
|
||||||
collectTermStates(searcher.getIndexReader(), contexts, termStates, this.queryTerms);
|
collectTermStates(searcher.getIndexReader(), contexts, termStates, this.queryTerms);
|
||||||
|
@ -270,14 +249,16 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
|
||||||
finalTerms.add(queryTerms[i]);
|
finalTerms.add(queryTerms[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return new ConstantScoreWeight(this, boost) {
|
return new ConstantScoreWeight(this, boost) {
|
||||||
|
|
||||||
private WeightOrDocIdSet rewrite(LeafReaderContext context) throws IOException {
|
@Override
|
||||||
|
public Scorer scorer(LeafReaderContext context) throws IOException {
|
||||||
final LeafReader reader = context.reader();
|
final LeafReader reader = context.reader();
|
||||||
Terms terms = reader.terms(field);
|
Terms terms = reader.terms(field);
|
||||||
if (terms == null) {
|
if (terms == null) {
|
||||||
return new WeightOrDocIdSet(new BitDocIdSet(new FixedBitSet(reader.maxDoc()), 0));
|
return null;
|
||||||
}
|
}
|
||||||
TermsEnum termsEnum = terms.iterator();
|
TermsEnum termsEnum = terms.iterator();
|
||||||
PostingsEnum docs = null;
|
PostingsEnum docs = null;
|
||||||
|
@ -292,42 +273,9 @@ public class GraphTermsQParserPlugin extends QParserPlugin {
|
||||||
builder.add(docs);
|
builder.add(docs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new WeightOrDocIdSet(builder.build());
|
DocIdSet docIdSet = builder.build();
|
||||||
}
|
DocIdSetIterator disi = docIdSet.iterator();
|
||||||
|
return disi == null ? null : new ConstantScoreScorer(this, score(), scoreMode, disi);
|
||||||
private Scorer scorer(DocIdSet set) throws IOException {
|
|
||||||
if (set == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final DocIdSetIterator disi = set.iterator();
|
|
||||||
if (disi == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new ConstantScoreScorer(this, score(), scoreMode, disi);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BulkScorer bulkScorer(LeafReaderContext context) throws IOException {
|
|
||||||
final WeightOrDocIdSet weightOrBitSet = rewrite(context);
|
|
||||||
if (weightOrBitSet.weight != null) {
|
|
||||||
return weightOrBitSet.weight.bulkScorer(context);
|
|
||||||
} else {
|
|
||||||
final Scorer scorer = scorer(weightOrBitSet.set);
|
|
||||||
if (scorer == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return new DefaultBulkScorer(scorer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Scorer scorer(LeafReaderContext context) throws IOException {
|
|
||||||
final WeightOrDocIdSet weightOrBitSet = rewrite(context);
|
|
||||||
if (weightOrBitSet.weight != null) {
|
|
||||||
return weightOrBitSet.weight.scorer(context);
|
|
||||||
} else {
|
|
||||||
return scorer(weightOrBitSet.set);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue