|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.apache.lucene.search; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.Objects; |
| 10 | + |
| 11 | +import org.apache.lucene.index.IndexReader; |
| 12 | +import org.apache.lucene.index.LeafReaderContext; |
| 13 | +import org.apache.lucene.util.Bits; |
| 14 | + |
| 15 | +/** |
| 16 | + * A query that wraps another query and ensures scores do not exceed a maximum value |
| 17 | + */ |
| 18 | +public final class CappedScoreQuery extends Query { |
| 19 | + private final Query query; |
| 20 | + private final float maxScore; |
| 21 | + |
| 22 | + /** Caps scores from the passed in Query to the supplied maxScore parameter */ |
| 23 | + public CappedScoreQuery(Query query, float maxScore) { |
| 24 | + this.query = Objects.requireNonNull(query, "Query must not be null"); |
| 25 | + if (maxScore > 0 == false) { |
| 26 | + throw new IllegalArgumentException(this.getClass().getName() + " maxScore must be >0, " + maxScore + " supplied."); |
| 27 | + } |
| 28 | + this.maxScore = maxScore; |
| 29 | + } |
| 30 | + |
| 31 | + /** Returns the encapsulated query. */ |
| 32 | + public Query getQuery() { |
| 33 | + return query; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public Query rewrite(IndexReader reader) throws IOException { |
| 38 | + Query rewritten = query.rewrite(reader); |
| 39 | + |
| 40 | + if (rewritten != query) { |
| 41 | + return new CappedScoreQuery(rewritten, maxScore); |
| 42 | + } |
| 43 | + |
| 44 | + if (rewritten.getClass() == CappedScoreQuery.class) { |
| 45 | + return rewritten; |
| 46 | + } |
| 47 | + |
| 48 | + if (rewritten.getClass() == BoostQuery.class) { |
| 49 | + return new CappedScoreQuery(((BoostQuery) rewritten).getQuery(), maxScore); |
| 50 | + } |
| 51 | + |
| 52 | + return super.rewrite(reader); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * We return this as our {@link BulkScorer} so that if the CSQ wraps a query with its own optimized top-level scorer (e.g. |
| 57 | + * BooleanScorer) we can use that top-level scorer. |
| 58 | + */ |
| 59 | + protected static class CappedBulkScorer extends BulkScorer { |
| 60 | + final BulkScorer bulkScorer; |
| 61 | + final Weight weight; |
| 62 | + final float maxScore; |
| 63 | + |
| 64 | + public CappedBulkScorer(BulkScorer bulkScorer, Weight weight, float maxScore) { |
| 65 | + this.bulkScorer = bulkScorer; |
| 66 | + this.weight = weight; |
| 67 | + this.maxScore = maxScore; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public int score(LeafCollector collector, Bits acceptDocs, int min, int max) throws IOException { |
| 72 | + return bulkScorer.score(wrapCollector(collector), acceptDocs, min, max); |
| 73 | + } |
| 74 | + |
| 75 | + private LeafCollector wrapCollector(LeafCollector collector) { |
| 76 | + return new FilterLeafCollector(collector) { |
| 77 | + @Override |
| 78 | + public void setScorer(Scorable scorer) throws IOException { |
| 79 | + // we must wrap again here, but using the scorer passed in as parameter: |
| 80 | + in.setScorer(new FilterScorable(scorer) { |
| 81 | + @Override |
| 82 | + public float score() throws IOException { |
| 83 | + return Math.min(maxScore, in.score()); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void setMinCompetitiveScore(float minScore) throws IOException { |
| 88 | + scorer.setMinCompetitiveScore(minScore); |
| 89 | + } |
| 90 | + |
| 91 | + }); |
| 92 | + } |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public long cost() { |
| 98 | + return bulkScorer.cost(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { |
| 104 | + final Weight innerWeight = searcher.createWeight(query, scoreMode, boost); |
| 105 | + if (scoreMode.needsScores()) { |
| 106 | + return new CappedScoreWeight(this, innerWeight, maxScore) { |
| 107 | + @Override |
| 108 | + public BulkScorer bulkScorer(LeafReaderContext context) throws IOException { |
| 109 | + final BulkScorer innerScorer = innerWeight.bulkScorer(context); |
| 110 | + if (innerScorer == null) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + return new CappedBulkScorer(innerScorer, this, maxScore); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException { |
| 118 | + ScorerSupplier innerScorerSupplier = innerWeight.scorerSupplier(context); |
| 119 | + if (innerScorerSupplier == null) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + return new ScorerSupplier() { |
| 123 | + @Override |
| 124 | + public Scorer get(long leadCost) throws IOException { |
| 125 | + final Scorer innerScorer = innerScorerSupplier.get(leadCost); |
| 126 | + // short-circuit if scores will not need capping |
| 127 | + innerScorer.advanceShallow(0); |
| 128 | + if (innerScorer.getMaxScore(DocIdSetIterator.NO_MORE_DOCS) <= maxScore) { |
| 129 | + return innerScorer; |
| 130 | + } |
| 131 | + return new CappedScorer(innerWeight, innerScorer, maxScore); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public long cost() { |
| 136 | + return innerScorerSupplier.cost(); |
| 137 | + } |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public Matches matches(LeafReaderContext context, int doc) throws IOException { |
| 143 | + return innerWeight.matches(context, doc); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public Scorer scorer(LeafReaderContext context) throws IOException { |
| 148 | + ScorerSupplier scorerSupplier = scorerSupplier(context); |
| 149 | + if (scorerSupplier == null) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + return scorerSupplier.get(Long.MAX_VALUE); |
| 153 | + } |
| 154 | + }; |
| 155 | + } else { |
| 156 | + return innerWeight; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public String toString(String field) { |
| 162 | + return new StringBuilder("CappedScore(").append(query.toString(field)).append(')').toString(); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public boolean equals(Object other) { |
| 167 | + return sameClassAs(other) && maxScore == ((CappedScoreQuery) other).maxScore && |
| 168 | + query.equals(((CappedScoreQuery) other).query); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public int hashCode() { |
| 173 | + return 31 * classHash() + query.hashCode() + Float.hashCode(maxScore); |
| 174 | + } |
| 175 | +} |
0 commit comments