2626import org .apache .lucene .search .knn .KnnSearchStrategy ;
2727import org .apache .lucene .util .ArrayUtil ;
2828import org .apache .lucene .util .BitSet ;
29- import org .apache . lucene . util . NumericUtils ;
29+ import org .elasticsearch . index . codec . vectors . cluster . NeighborQueue ;
3030
3131/**
3232 * This collects the nearest children vectors. Diversifying the results over the provided parent
@@ -36,7 +36,7 @@ class DiversifyingNearestChildrenKnnCollector extends AbstractMaxScoreKnnCollect
3636
3737 private final BitSet parentBitSet ;
3838 private final NodeIdCachingHeap heap ;
39- private long minCompetitiveDocScore = Long . MIN_VALUE ;
39+ private long minCompetitiveDocScore = LEAST_COMPETITIVE ;
4040 private float minCompetitiveScore = Float .NEGATIVE_INFINITY ;
4141
4242 /**
@@ -74,10 +74,7 @@ public boolean collect(int docId, float nodeScore) {
7474
7575 @ Override
7676 public float minCompetitiveSimilarity () {
77- if (heap .size >= k () || minCompetitiveDocScore > Long .MIN_VALUE ) {
78- return Math .max (minCompetitiveScore , heap .topScore ());
79- }
80- return Float .NEGATIVE_INFINITY ;
77+ return heap .size < k () ? Float .NEGATIVE_INFINITY : Math .max (minCompetitiveScore , heap .topScore ());
8178 }
8279
8380 @ Override
@@ -108,17 +105,14 @@ public int numCollected() {
108105
109106 @ Override
110107 public long getMinCompetitiveDocScore () {
111- return ((( long ) NumericUtils . floatToSortableInt ( heap .topScore ())) << 32 ) | ( 0xFFFFFFFFL & ~ heap . topNode ()) ;
108+ return heap . size () > 0 ? Math . max ( NeighborQueue . encodeRaw ( heap . topNode (), heap .topScore ()), minCompetitiveDocScore ) : minCompetitiveDocScore ;
112109 }
113110
114111 @ Override
115112 void updateMinCompetitiveDocScore (long minCompetitiveDocScore ) {
116- if (minCompetitiveDocScore == Long .MIN_VALUE ) {
117- // no update
118- return ;
119- }
120- this .minCompetitiveDocScore = minCompetitiveDocScore ;
121- this .minCompetitiveScore = NumericUtils .sortableIntToFloat ((int ) (minCompetitiveDocScore >> 32 ));
113+ long queueMinCompetitiveDocScore = heap .size () > 0 ? NeighborQueue .encodeRaw (heap .topNode (), heap .topScore ()) : LEAST_COMPETITIVE ;
114+ this .minCompetitiveDocScore = Math .max (this .minCompetitiveDocScore , Math .max (queueMinCompetitiveDocScore , minCompetitiveDocScore ));
115+ this .minCompetitiveScore = NeighborQueue .decodeScoreRaw (this .minCompetitiveDocScore );
122116 }
123117
124118 /**
@@ -154,10 +148,16 @@ private static class NodeIdCachingHeap {
154148 }
155149
156150 public final int topNode () {
151+ if (size == 0 ) {
152+ return Integer .MAX_VALUE ;
153+ }
157154 return heapNodes [1 ].child ;
158155 }
159156
160157 public final float topScore () {
158+ if (size == 0 ) {
159+ return Float .NEGATIVE_INFINITY ;
160+ }
161161 return heapNodes [1 ].score ;
162162 }
163163
0 commit comments