|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package org.opensearch.neuralsearch.query; |
| 6 | + |
| 7 | +import lombok.Getter; |
| 8 | +import org.apache.lucene.search.BulkScorer; |
| 9 | +import org.apache.lucene.search.DocIdSetIterator; |
| 10 | +import org.apache.lucene.search.LeafCollector; |
| 11 | +import org.apache.lucene.search.Scorer; |
| 12 | +import org.apache.lucene.util.Bits; |
| 13 | +import org.apache.lucene.util.FixedBitSet; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +/** |
| 21 | + * Bulk scorer for hybrid query |
| 22 | + */ |
| 23 | +public class HybridBulkScorer extends BulkScorer { |
| 24 | + private static final int SHIFT = 10; |
| 25 | + private static final int WINDOW_SIZE = 1 << SHIFT; |
| 26 | + private static final int MASK = WINDOW_SIZE - 1; |
| 27 | + |
| 28 | + private final long cost; |
| 29 | + private final Scorer[] scorers; |
| 30 | + @Getter |
| 31 | + private final HybridSubQueryScorer hybridSubQueryScorer; |
| 32 | + private final boolean needsScores; |
| 33 | + @Getter |
| 34 | + private final FixedBitSet matching; |
| 35 | + @Getter |
| 36 | + private final float[][] windowScores; |
| 37 | + private final HybridQueryDocIdStream hybridQueryDocIdStream; |
| 38 | + private final int maxDoc; |
| 39 | + |
| 40 | + public HybridBulkScorer(List<Scorer> scorers, boolean needsScores, int maxDoc) { |
| 41 | + long cost = 0; |
| 42 | + this.scorers = new Scorer[scorers.size()]; |
| 43 | + for (int subQueryIndex = 0; subQueryIndex < scorers.size(); subQueryIndex++) { |
| 44 | + Scorer scorer = scorers.get(subQueryIndex); |
| 45 | + if (Objects.isNull(scorer)) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + cost += scorer.iterator().cost(); |
| 49 | + this.scorers[subQueryIndex] = scorer; |
| 50 | + } |
| 51 | + this.cost = cost; |
| 52 | + this.hybridSubQueryScorer = new HybridSubQueryScorer(scorers.size()); |
| 53 | + this.needsScores = needsScores; |
| 54 | + this.matching = new FixedBitSet(WINDOW_SIZE); |
| 55 | + this.windowScores = new float[this.scorers.length][WINDOW_SIZE]; |
| 56 | + this.maxDoc = maxDoc; |
| 57 | + this.hybridQueryDocIdStream = new HybridQueryDocIdStream(this); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public int score(LeafCollector collector, Bits acceptDocs, int min, int max) throws IOException { |
| 62 | + collector.setScorer(hybridSubQueryScorer); |
| 63 | + // making sure we are not going over the global limit defined by maxDoc |
| 64 | + max = Math.min(max, maxDoc); |
| 65 | + int[] docsIds = advance(min, scorers); |
| 66 | + while (allDocIdsUsed(docsIds, max) == false) { |
| 67 | + scoreWindow(collector, acceptDocs, min, max, docsIds); |
| 68 | + } |
| 69 | + return getNextDocIdCandidate(docsIds); |
| 70 | + } |
| 71 | + |
| 72 | + private void scoreWindow(LeafCollector collector, Bits acceptDocs, int min, int max, int[] docIds) throws IOException { |
| 73 | + // pick the lowest out of all not yet used doc ids |
| 74 | + int topDoc = -1; |
| 75 | + for (int docId : docIds) { |
| 76 | + if (docId < max) { |
| 77 | + topDoc = docId; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + final int windowBase = topDoc & ~MASK; // take the next match (at random) and find the window where it belongs |
| 83 | + final int windowMin = Math.max(min, windowBase); |
| 84 | + final int windowMax = Math.min(max, windowBase + WINDOW_SIZE); |
| 85 | + |
| 86 | + scoreWindowIntoBitSetWithSubqueryScorers(collector, acceptDocs, max, docIds, windowMin, windowMax, windowBase); |
| 87 | + } |
| 88 | + |
| 89 | + private void scoreWindowIntoBitSetWithSubqueryScorers( |
| 90 | + LeafCollector collector, |
| 91 | + Bits acceptDocs, |
| 92 | + int max, |
| 93 | + int[] docIds, |
| 94 | + int windowMin, |
| 95 | + int windowMax, |
| 96 | + int windowBase |
| 97 | + ) throws IOException { |
| 98 | + for (int subQueryIndex = 0; subQueryIndex < scorers.length; subQueryIndex++) { |
| 99 | + if (Objects.isNull(scorers[subQueryIndex]) || docIds[subQueryIndex] >= max) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + DocIdSetIterator it = scorers[subQueryIndex].iterator(); |
| 103 | + int doc = docIds[subQueryIndex]; |
| 104 | + if (doc < windowMin) { |
| 105 | + doc = it.advance(windowMin); |
| 106 | + } |
| 107 | + while (doc < windowMax) { |
| 108 | + if (Objects.isNull(acceptDocs) || acceptDocs.get(doc)) { |
| 109 | + int d = doc & MASK; |
| 110 | + if (needsScores) { |
| 111 | + float score = scorers[subQueryIndex].score(); |
| 112 | + // collect score only in case it's gt competitive score |
| 113 | + if (score > hybridSubQueryScorer.getMinScores()[subQueryIndex]) { |
| 114 | + matching.set(d); |
| 115 | + windowScores[subQueryIndex][d] = score; |
| 116 | + } |
| 117 | + } else { |
| 118 | + matching.set(d); |
| 119 | + } |
| 120 | + } |
| 121 | + doc = it.nextDoc(); |
| 122 | + } |
| 123 | + docIds[subQueryIndex] = doc; |
| 124 | + } |
| 125 | + |
| 126 | + hybridQueryDocIdStream.setBase(windowBase); |
| 127 | + collector.collect(hybridQueryDocIdStream); |
| 128 | + |
| 129 | + matching.clear(); |
| 130 | + |
| 131 | + for (float[] windowScore : windowScores) { |
| 132 | + Arrays.fill(windowScore, 0.0f); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private int[] advance(int min, Scorer[] scorers) throws IOException { |
| 137 | + int[] docIds = new int[scorers.length]; |
| 138 | + for (int subQueryIndex = 0; subQueryIndex < scorers.length; subQueryIndex++) { |
| 139 | + if (Objects.isNull(scorers[subQueryIndex])) { |
| 140 | + docIds[subQueryIndex] = DocIdSetIterator.NO_MORE_DOCS; |
| 141 | + continue; |
| 142 | + } |
| 143 | + DocIdSetIterator it = scorers[subQueryIndex].iterator(); |
| 144 | + int doc = it.docID(); |
| 145 | + if (doc < min) { |
| 146 | + doc = it.advance(min); |
| 147 | + } |
| 148 | + docIds[subQueryIndex] = doc; |
| 149 | + } |
| 150 | + return docIds; |
| 151 | + } |
| 152 | + |
| 153 | + private boolean allDocIdsUsed(int[] docsIds, int max) { |
| 154 | + for (int docId : docsIds) { |
| 155 | + if (docId < max) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + } |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + private int getNextDocIdCandidate(final int[] docsIds) { |
| 163 | + int nextDoc = -1; |
| 164 | + for (int doc : docsIds) { |
| 165 | + if (doc != DocIdSetIterator.NO_MORE_DOCS) { |
| 166 | + nextDoc = Math.max(nextDoc, doc); |
| 167 | + } |
| 168 | + } |
| 169 | + return nextDoc == -1 ? DocIdSetIterator.NO_MORE_DOCS : nextDoc; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public long cost() { |
| 174 | + return cost; |
| 175 | + } |
| 176 | +} |
0 commit comments