|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.search.vectors; |
| 11 | + |
| 12 | +import org.apache.lucene.index.LeafReaderContext; |
| 13 | +import org.apache.lucene.search.BooleanClause; |
| 14 | +import org.apache.lucene.search.DocIdSetIterator; |
| 15 | +import org.apache.lucene.search.Explanation; |
| 16 | +import org.apache.lucene.search.IndexSearcher; |
| 17 | +import org.apache.lucene.search.Query; |
| 18 | +import org.apache.lucene.search.QueryVisitor; |
| 19 | +import org.apache.lucene.search.ScoreMode; |
| 20 | +import org.apache.lucene.search.Scorer; |
| 21 | +import org.apache.lucene.search.ScorerSupplier; |
| 22 | +import org.apache.lucene.search.Weight; |
| 23 | +import org.apache.lucene.search.join.BitSetProducer; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +/** |
| 29 | + * A Lucene query that selects the highest-scoring child document for each parent block. |
| 30 | + * <p> |
| 31 | + * Children are scored using the {@code innerQuery}, and for each parent (as defined by the |
| 32 | + * {@code parentFilter}), the single best-scoring child is returned. |
| 33 | + */ |
| 34 | +public class DiversifyingParentBlockQuery extends Query { |
| 35 | + private final BitSetProducer parentFilter; |
| 36 | + private final Query innerQuery; |
| 37 | + |
| 38 | + public DiversifyingParentBlockQuery(BitSetProducer parentFilter, Query innerQuery) { |
| 39 | + this.parentFilter = Objects.requireNonNull(parentFilter); |
| 40 | + this.innerQuery = Objects.requireNonNull(innerQuery); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Query rewrite(IndexSearcher indexSearcher) throws IOException { |
| 45 | + Query rewritten = innerQuery.rewrite(indexSearcher); |
| 46 | + if (rewritten != innerQuery) { |
| 47 | + return new DiversifyingParentBlockQuery(parentFilter, rewritten); |
| 48 | + } |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { |
| 54 | + Weight innerWeight = innerQuery.createWeight(searcher, scoreMode, boost); |
| 55 | + return new DiversifyingParentBlockWeight(this, innerWeight, parentFilter); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String toString(String field) { |
| 60 | + return "DiversifyingBlockQuery(inner=" + innerQuery.toString(field) + ")"; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void visit(QueryVisitor visitor) { |
| 65 | + innerQuery.visit(visitor.getSubVisitor(BooleanClause.Occur.MUST, this)); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean equals(Object o) { |
| 70 | + if (this == o) return true; |
| 71 | + if (o == null || getClass() != o.getClass()) return false; |
| 72 | + DiversifyingParentBlockQuery that = (DiversifyingParentBlockQuery) o; |
| 73 | + return Objects.equals(innerQuery, that.innerQuery) && parentFilter == that.parentFilter; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public int hashCode() { |
| 78 | + return Objects.hash(innerQuery, parentFilter); |
| 79 | + } |
| 80 | + |
| 81 | + private static class DiversifyingParentBlockWeight extends Weight { |
| 82 | + private final Weight innerWeight; |
| 83 | + private final BitSetProducer parentFilter; |
| 84 | + |
| 85 | + DiversifyingParentBlockWeight(Query query, Weight innerWeight, BitSetProducer parentFilter) { |
| 86 | + super(query); |
| 87 | + this.innerWeight = innerWeight; |
| 88 | + this.parentFilter = parentFilter; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Explanation explain(LeafReaderContext context, int doc) throws IOException { |
| 93 | + return innerWeight.explain(context, doc); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException { |
| 98 | + var innerSupplier = innerWeight.scorerSupplier(context); |
| 99 | + var parentBits = parentFilter.getBitSet(context); |
| 100 | + if (parentBits == null || innerSupplier == null) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + return new ScorerSupplier() { |
| 105 | + @Override |
| 106 | + public Scorer get(long leadCost) throws IOException { |
| 107 | + var innerScorer = innerSupplier.get(leadCost); |
| 108 | + var innerIterator = innerScorer.iterator(); |
| 109 | + return new Scorer() { |
| 110 | + int currentDoc = -1; |
| 111 | + float currentScore = Float.NaN; |
| 112 | + |
| 113 | + @Override |
| 114 | + public int docID() { |
| 115 | + return currentDoc; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public DocIdSetIterator iterator() { |
| 120 | + return new DocIdSetIterator() { |
| 121 | + boolean exhausted = false; |
| 122 | + |
| 123 | + @Override |
| 124 | + public int docID() { |
| 125 | + return currentDoc; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public int nextDoc() throws IOException { |
| 130 | + return advance(currentDoc + 1); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public int advance(int target) throws IOException { |
| 135 | + if (exhausted) { |
| 136 | + return NO_MORE_DOCS; |
| 137 | + } |
| 138 | + if (currentDoc == -1 || innerIterator.docID() < target) { |
| 139 | + if (innerIterator.advance(target) == NO_MORE_DOCS) { |
| 140 | + exhausted = true; |
| 141 | + return currentDoc = NO_MORE_DOCS; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + int bestChild = innerIterator.docID(); |
| 146 | + float bestScore = innerScorer.score(); |
| 147 | + int parent = parentBits.nextSetBit(bestChild); |
| 148 | + |
| 149 | + int innerDoc; |
| 150 | + while ((innerDoc = innerIterator.nextDoc()) < parent) { |
| 151 | + float score = innerScorer.score(); |
| 152 | + if (score > bestScore) { |
| 153 | + bestChild = innerIterator.docID(); |
| 154 | + bestScore = score; |
| 155 | + } |
| 156 | + } |
| 157 | + if (innerDoc == NO_MORE_DOCS) { |
| 158 | + exhausted = true; |
| 159 | + } |
| 160 | + currentScore = bestScore; |
| 161 | + return currentDoc = bestChild; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public long cost() { |
| 166 | + return innerIterator.cost(); |
| 167 | + } |
| 168 | + }; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public float score() throws IOException { |
| 173 | + return currentScore; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public float getMaxScore(int upTo) throws IOException { |
| 178 | + return innerScorer.getMaxScore(upTo); |
| 179 | + } |
| 180 | + }; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public long cost() { |
| 185 | + return innerSupplier.cost(); |
| 186 | + } |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public boolean isCacheable(LeafReaderContext ctx) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments