mirror of https://github.com/apache/lucene.git
SOLR-9353: Factor out ReRankQParserPlugin.ReRankQueryRescorer private class.
This commit is contained in:
parent
cc3f3e8a8b
commit
812dc1d0b5
|
@ -205,6 +205,8 @@ Other Changes
|
||||||
|
|
||||||
* SOLR-9392: Fixed CDCR Test failures which were due to leaked resources. (shalin)
|
* SOLR-9392: Fixed CDCR Test failures which were due to leaked resources. (shalin)
|
||||||
|
|
||||||
|
* SOLR-9353: Factor out ReRankQParserPlugin.ReRankQueryRescorer private class. (Christine Poerschke)
|
||||||
|
|
||||||
================== 6.1.0 ==================
|
================== 6.1.0 ==================
|
||||||
|
|
||||||
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.
|
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.
|
||||||
|
|
|
@ -32,6 +32,7 @@ import org.apache.lucene.search.LeafCollector;
|
||||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||||
import org.apache.lucene.search.Query;
|
import org.apache.lucene.search.Query;
|
||||||
import org.apache.lucene.search.QueryRescorer;
|
import org.apache.lucene.search.QueryRescorer;
|
||||||
|
import org.apache.lucene.search.Rescorer;
|
||||||
import org.apache.lucene.search.ScoreDoc;
|
import org.apache.lucene.search.ScoreDoc;
|
||||||
import org.apache.lucene.search.Sort;
|
import org.apache.lucene.search.Sort;
|
||||||
import org.apache.lucene.search.TopDocs;
|
import org.apache.lucene.search.TopDocs;
|
||||||
|
@ -97,12 +98,32 @@ public class ReRankQParserPlugin extends QParserPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final class ReRankQueryRescorer extends QueryRescorer {
|
||||||
|
|
||||||
|
final double reRankWeight;
|
||||||
|
|
||||||
|
public ReRankQueryRescorer(Query reRankQuery, double reRankWeight) {
|
||||||
|
super(reRankQuery);
|
||||||
|
this.reRankWeight = reRankWeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected float combine(float firstPassScore, boolean secondPassMatches, float secondPassScore) {
|
||||||
|
float score = firstPassScore;
|
||||||
|
if (secondPassMatches) {
|
||||||
|
score += reRankWeight * secondPassScore;
|
||||||
|
}
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private final class ReRankQuery extends RankQuery {
|
private final class ReRankQuery extends RankQuery {
|
||||||
private Query mainQuery = defaultQuery;
|
private Query mainQuery = defaultQuery;
|
||||||
private Query reRankQuery;
|
final private Query reRankQuery;
|
||||||
private int reRankDocs;
|
final private int reRankDocs;
|
||||||
private int length;
|
final private int length;
|
||||||
private double reRankWeight;
|
final private double reRankWeight;
|
||||||
|
final private Rescorer reRankQueryRescorer;
|
||||||
private Map<BytesRef, Integer> boostedPriority;
|
private Map<BytesRef, Integer> boostedPriority;
|
||||||
|
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
|
@ -126,6 +147,7 @@ public class ReRankQParserPlugin extends QParserPlugin {
|
||||||
this.reRankDocs = reRankDocs;
|
this.reRankDocs = reRankDocs;
|
||||||
this.reRankWeight = reRankWeight;
|
this.reRankWeight = reRankWeight;
|
||||||
this.length = length;
|
this.length = length;
|
||||||
|
this.reRankQueryRescorer = new ReRankQueryRescorer(reRankQuery, reRankWeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RankQuery wrap(Query _mainQuery) {
|
public RankQuery wrap(Query _mainQuery) {
|
||||||
|
@ -149,7 +171,7 @@ public class ReRankQParserPlugin extends QParserPlugin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ReRankCollector(reRankDocs, length, reRankQuery, reRankWeight, cmd, searcher, boostedPriority);
|
return new ReRankCollector(reRankDocs, length, reRankQueryRescorer, cmd, searcher, boostedPriority);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -172,57 +194,43 @@ public class ReRankQParserPlugin extends QParserPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException{
|
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException{
|
||||||
return new ReRankWeight(mainQuery, reRankQuery, reRankWeight, searcher, needsScores);
|
return new ReRankWeight(mainQuery, reRankQueryRescorer, searcher, needsScores);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ReRankWeight extends FilterWeight {
|
private class ReRankWeight extends FilterWeight {
|
||||||
private Query reRankQuery;
|
|
||||||
private IndexSearcher searcher;
|
private IndexSearcher searcher;
|
||||||
private double reRankWeight;
|
final private Rescorer reRankQueryRescorer;
|
||||||
|
|
||||||
public ReRankWeight(Query mainQuery, Query reRankQuery, double reRankWeight, IndexSearcher searcher, boolean needsScores) throws IOException {
|
public ReRankWeight(Query mainQuery, Rescorer reRankQueryRescorer, IndexSearcher searcher, boolean needsScores) throws IOException {
|
||||||
super(mainQuery, mainQuery.createWeight(searcher, needsScores));
|
super(mainQuery, mainQuery.createWeight(searcher, needsScores));
|
||||||
this.reRankQuery = reRankQuery;
|
|
||||||
this.searcher = searcher;
|
this.searcher = searcher;
|
||||||
this.reRankWeight = reRankWeight;
|
this.reRankQueryRescorer = reRankQueryRescorer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
|
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
|
||||||
Explanation mainExplain = in.explain(context, doc);
|
Explanation mainExplain = in.explain(context, doc);
|
||||||
return new QueryRescorer(reRankQuery) {
|
return reRankQueryRescorer.explain(searcher, mainExplain, context.docBase+doc);
|
||||||
@Override
|
|
||||||
protected float combine(float firstPassScore, boolean secondPassMatches, float secondPassScore) {
|
|
||||||
float score = firstPassScore;
|
|
||||||
if (secondPassMatches) {
|
|
||||||
score += reRankWeight * secondPassScore;
|
|
||||||
}
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
}.explain(searcher, mainExplain, context.docBase+doc);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ReRankCollector extends TopDocsCollector {
|
private class ReRankCollector extends TopDocsCollector {
|
||||||
|
|
||||||
private Query reRankQuery;
|
final private TopDocsCollector mainCollector;
|
||||||
private TopDocsCollector mainCollector;
|
final private IndexSearcher searcher;
|
||||||
private IndexSearcher searcher;
|
final private int reRankDocs;
|
||||||
private int reRankDocs;
|
final private int length;
|
||||||
private int length;
|
final private Map<BytesRef, Integer> boostedPriority;
|
||||||
private double reRankWeight;
|
final private Rescorer reRankQueryRescorer;
|
||||||
private Map<BytesRef, Integer> boostedPriority;
|
|
||||||
|
|
||||||
|
|
||||||
public ReRankCollector(int reRankDocs,
|
public ReRankCollector(int reRankDocs,
|
||||||
int length,
|
int length,
|
||||||
Query reRankQuery,
|
Rescorer reRankQueryRescorer,
|
||||||
double reRankWeight,
|
|
||||||
QueryCommand cmd,
|
QueryCommand cmd,
|
||||||
IndexSearcher searcher,
|
IndexSearcher searcher,
|
||||||
Map<BytesRef, Integer> boostedPriority) throws IOException {
|
Map<BytesRef, Integer> boostedPriority) throws IOException {
|
||||||
super(null);
|
super(null);
|
||||||
this.reRankQuery = reRankQuery;
|
|
||||||
this.reRankDocs = reRankDocs;
|
this.reRankDocs = reRankDocs;
|
||||||
this.length = length;
|
this.length = length;
|
||||||
this.boostedPriority = boostedPriority;
|
this.boostedPriority = boostedPriority;
|
||||||
|
@ -234,7 +242,7 @@ public class ReRankQParserPlugin extends QParserPlugin {
|
||||||
this.mainCollector = TopFieldCollector.create(sort, Math.max(this.reRankDocs, length), false, true, true);
|
this.mainCollector = TopFieldCollector.create(sort, Math.max(this.reRankDocs, length), false, true, true);
|
||||||
}
|
}
|
||||||
this.searcher = searcher;
|
this.searcher = searcher;
|
||||||
this.reRankWeight = reRankWeight;
|
this.reRankQueryRescorer = reRankQueryRescorer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTotalHits() {
|
public int getTotalHits() {
|
||||||
|
@ -276,16 +284,8 @@ public class ReRankQParserPlugin extends QParserPlugin {
|
||||||
|
|
||||||
mainDocs.scoreDocs = reRankScoreDocs;
|
mainDocs.scoreDocs = reRankScoreDocs;
|
||||||
|
|
||||||
TopDocs rescoredDocs = new QueryRescorer(reRankQuery) {
|
TopDocs rescoredDocs = reRankQueryRescorer
|
||||||
@Override
|
.rescore(searcher, mainDocs, mainDocs.scoreDocs.length);
|
||||||
protected float combine(float firstPassScore, boolean secondPassMatches, float secondPassScore) {
|
|
||||||
float score = firstPassScore;
|
|
||||||
if (secondPassMatches) {
|
|
||||||
score += reRankWeight * secondPassScore;
|
|
||||||
}
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
}.rescore(searcher, mainDocs, mainDocs.scoreDocs.length);
|
|
||||||
|
|
||||||
Arrays.sort(rescoredDocs.scoreDocs, new BoostedComp(boostedDocs, mainDocs.scoreDocs, rescoredDocs.getMaxScore()));
|
Arrays.sort(rescoredDocs.scoreDocs, new BoostedComp(boostedDocs, mainDocs.scoreDocs, rescoredDocs.getMaxScore()));
|
||||||
|
|
||||||
|
@ -325,16 +325,8 @@ public class ReRankQParserPlugin extends QParserPlugin {
|
||||||
|
|
||||||
mainDocs.scoreDocs = reRankScoreDocs;
|
mainDocs.scoreDocs = reRankScoreDocs;
|
||||||
|
|
||||||
TopDocs rescoredDocs = new QueryRescorer(reRankQuery) {
|
TopDocs rescoredDocs = reRankQueryRescorer
|
||||||
@Override
|
.rescore(searcher, mainDocs, mainDocs.scoreDocs.length);
|
||||||
protected float combine(float firstPassScore, boolean secondPassMatches, float secondPassScore) {
|
|
||||||
float score = firstPassScore;
|
|
||||||
if (secondPassMatches) {
|
|
||||||
score += reRankWeight * secondPassScore;
|
|
||||||
}
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
}.rescore(searcher, mainDocs, mainDocs.scoreDocs.length);
|
|
||||||
|
|
||||||
//Lower howMany to return if we've collected fewer documents.
|
//Lower howMany to return if we've collected fewer documents.
|
||||||
howMany = Math.min(howMany, mainScoreDocs.length);
|
howMany = Math.min(howMany, mainScoreDocs.length);
|
||||||
|
|
Loading…
Reference in New Issue