You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "benwtrent (via GitHub)" <gi...@apache.org> on 2026/03/20 20:15:24 UTC
Re: [PR] Add BayesianScoreQuery and LogOddsConjunctionQuery for probabilistic hybrid search [lucene]
benwtrent commented on code in PR #15827:
URL: https://github.com/apache/lucene/pull/15827#discussion_r2965896305
##########
lucene/core/src/java/org/apache/lucene/search/BayesianScoreQuery.java:
##########
@@ -0,0 +1,234 @@
+/*
+ * 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.Objects;
+import org.apache.lucene.index.LeafReaderContext;
+
+/**
+ * A query wrapper that transforms the inner query's score into a calibrated probability via sigmoid
+ * calibration: {@code P = sigmoid(alpha * (score - beta))}.
+ *
+ * <p>This implements the query-level Bayesian transform from "Bayesian BM25": the inner query
+ * (typically a multi-term BooleanQuery with BM25Similarity) produces a raw score, and this wrapper
+ * maps it to a probability in (0, 1) suitable for combination with other probability signals via
+ * {@link LogOddsConjunctionQuery}.
+ *
+ * <p>The alpha parameter controls the sigmoid steepness (score sensitivity), and beta controls the
+ * midpoint (decision boundary). These can be set manually or estimated from the score distribution.
+ */
Review Comment:
```suggestion
* @lucene.experimental
*/
```
##########
lucene/core/src/java/org/apache/lucene/search/LogOddsConjunctionQuery.java:
##########
@@ -0,0 +1,290 @@
+/*
+ * 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.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.lucene.index.LeafReaderContext;
+
+/**
+ * A query that combines sub-query probability scores via log-odds conjunction. Sub-queries are
+ * expected to produce scores in (0, 1) representing probabilities (e.g., from {@link
+ * BayesianScoreQuery} wrapping a BM25 query, or KNN cosine similarity).
+ *
+ * <p>The combination formula resolves the shrinkage problem of naive probabilistic AND by:
+ *
+ * <ol>
+ * <li>Converting each sub-score to log-odds: logit(p) = log(p / (1 - p))
+ * <li>Computing the mean log-odds across all clauses (non-matching contribute 0 = neutral)
+ * <li>Applying multiplicative confidence scaling: meanLogit * n^alpha
+ * <li>Converting back to probability via sigmoid
+ * </ol>
+ *
+ * <p>The alpha parameter controls the confidence scaling exponent. The default alpha=0.5 implements
+ * the sqrt(n) scaling law from "From Bayesian Inference to Neural Computation".
+ *
+ * @see LogOddsConjunctionScorer
Review Comment:
```suggestion
* @see LogOddsConjunctionScorer
* @lucene.experimental
```
##########
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).
Review Comment:
```suggestion
* <p>Non-matching sub-scorers contribute logit(0.5) = 0 (neutral evidence).
* @lucene.experimental
```
##########
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:
We can use `Math.clamp`
--
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: issues-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org