gf2121 commented on code in PR #14714:
URL: https://github.com/apache/lucene/pull/14714#discussion_r2113858109


##########
lucene/core/src/java/org/apache/lucene/search/DocScoreEncoder.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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 org.apache.lucene.util.NumericUtils;
+
+/**
+ * An encoder do encode (doc, score) pair as a long whose sort order is same 
as {@code (o1, o2) ->
+ * Float.compare(o1.score, 
o2.score)).thenComparing(Comparator.comparingInt((ScoreDoc o) ->
+ * o.doc).reversed())}
+ *
+ * <p>Note that negative score is allowed but relationship between two codes 
encoded by negative
+ * scores is undefined. The only thing guaranteed is codes encoded from 
negative scores are smaller
+ * than codes encoded from non-negative scores.
+ */
+class DocScoreEncoder {
+
+  static final long LEAST_COMPETITIVE_CODE = encode(Integer.MAX_VALUE, 
Float.NEGATIVE_INFINITY);
+  private static final int POS_INF_TO_SORTABLE_INT = 
scoreToSortableInt(Float.POSITIVE_INFINITY);
+
+  static long encode(int docId, float score) {
+    return encodeIntScore(docId, scoreToSortableInt(score));
+  }
+
+  static long encodeIntScore(int docId, int score) {
+    return (((long) score) << 32) | (~docId & 0xFFFFFFFFL);
+  }
+
+  static float toScore(long value) {
+    return sortableIntToScore(toIntScore(value));
+  }
+
+  static int toIntScore(long value) {
+    return (int) (value >>> 32);
+  }
+
+  static int docId(long value) {
+    return (int) ~value;
+  }
+
+  static int nextUp(int intScore) {
+    assert intScore <= POS_INF_TO_SORTABLE_INT;
+    int nextUp = Math.min(POS_INF_TO_SORTABLE_INT, intScore + 1);
+    assert nextUp == 
scoreToSortableInt(Math.nextUp(sortableIntToScore(intScore)));
+    return nextUp;
+  }
+
+  /**
+   * Score is non-negative float so wo use floatToRawIntBits instead of {@link
+   * NumericUtils#floatToSortableInt}. We do not assert score >= 0 here to 
allow pass negative float
+   * to indicate totally non-competitive, e.g. {@link #LEAST_COMPETITIVE_CODE}.
+   */

Review Comment:
   > FWIW, I believe that LEAST_COMPETITIVE_CODE could use a score of 0 since 
Integer.MAX_VALUE is not an allowed doc ID?
   
   The issue of using `encode(Integer.MAX_VALUE , 0f)` is that topScore will be 
decoded as 0
   
https://github.com/apache/lucene/blob/e650ac4db19a03f417afab67b24406a8a684b7d4/lucene/core/src/java/org/apache/lucene/search/TopScoreDocCollector.java#L135
   Then score 0 will not be competitive as we are comparing score only.
   
https://github.com/apache/lucene/blob/e650ac4db19a03f417afab67b24406a8a684b7d4/lucene/core/src/java/org/apache/lucene/search/TopScoreDocCollector.java#L115
   
   I agree this contract is bit tricky, maybe we should just use 
`NumericUtils#floatToSortableInt`.



-- 
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-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to