jaepil commented on code in PR #15827: URL: https://github.com/apache/lucene/pull/15827#discussion_r2968309141
########## lucene/core/src/java/org/apache/lucene/search/LogOddsConjunctionScorer.java: ########## @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.search; + +import java.io.IOException; +import java.util.List; + +/** + * Scorer for {@link LogOddsConjunctionQuery}. Combines sub-scorer outputs (assumed to be + * probabilities in (0, 1)) via log-odds conjunction with multiplicative confidence scaling. + * + * <p>The scoring formula is: + * + * <pre> + * gatedLogit = softplus(logit(clamp(subScore))) for each matching sub-scorer + * logitSum = sum of gatedLogit values + * meanLogit = logitSum / n (n = total clause count, not just matching) + * scaledLogit = meanLogit * pow(n, alpha) + * score = sigmoid(scaledLogit) + * </pre> + * + * <p>Softplus gating ({@code log(1 + exp(x))}) is applied to logit values before aggregation. This + * distinguishes "absence of evidence" (non-matching sub-scorer, contributes 0) from "evidence of + * absence" (matching sub-scorer with weak probability, contributes a small positive value). A + * matching sub-scorer always contributes more than a non-matching one, preserving the ordering + * among weak matches while ensuring that no match is ever penalized. + * + * <p>Non-matching sub-scorers contribute logit(0.5) = 0 (neutral evidence). + */ +final class LogOddsConjunctionScorer extends DisjunctionScorer { + private static final float CLAMP_MIN = 1e-7f; + private static final float CLAMP_MAX = 1f - 1e-7f; + + private final List<Scorer> subScorers; + private final int totalClauses; + private final float scalingFactor; + + private final DisjunctionScoreBlockBoundaryPropagator disjunctionBlockPropagator; + + /** + * Creates a new LogOddsConjunctionScorer. + * + * @param subScorers the sub scorers to combine + * @param totalClauses the total number of clauses (including non-matching) + * @param alpha confidence scaling exponent (0.5 = sqrt(n) law) + * @param scoreMode the score mode + * @param leadCost the lead cost for iteration + */ + LogOddsConjunctionScorer( + List<Scorer> subScorers, int totalClauses, float alpha, ScoreMode scoreMode, long leadCost) + throws IOException { + super(subScorers, scoreMode, leadCost); + this.subScorers = subScorers; + this.totalClauses = totalClauses; + this.scalingFactor = (float) Math.pow(totalClauses, alpha); + if (scoreMode == ScoreMode.TOP_SCORES) { + this.disjunctionBlockPropagator = new DisjunctionScoreBlockBoundaryPropagator(subScorers); + } else { + this.disjunctionBlockPropagator = null; + } + } + + static float clampProbability(float p) { + return Math.max(CLAMP_MIN, Math.min(CLAMP_MAX, p)); Review Comment: Done. Thank you! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
