|
| 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 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.search.vectors; |
| 10 | + |
| 11 | +import org.apache.lucene.index.ByteVectorValues; |
| 12 | +import org.apache.lucene.index.FloatVectorValues; |
| 13 | +import org.apache.lucene.index.LeafReaderContext; |
| 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.VectorScorer; |
| 22 | +import org.apache.lucene.search.Weight; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +/** |
| 29 | + * Exact knn query. Will iterate and score all documents that have the provided dense vector field in the index. |
| 30 | + */ |
| 31 | +public abstract class DenseVectorQuery extends Query { |
| 32 | + |
| 33 | + protected final String field; |
| 34 | + |
| 35 | + public DenseVectorQuery(String field) { |
| 36 | + this.field = field; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void visit(QueryVisitor queryVisitor) { |
| 41 | + queryVisitor.visitLeaf(this); |
| 42 | + } |
| 43 | + |
| 44 | + abstract static class DenseVectorWeight extends Weight { |
| 45 | + private final String field; |
| 46 | + private final float boost; |
| 47 | + |
| 48 | + protected DenseVectorWeight(DenseVectorQuery query, float boost) { |
| 49 | + super(query); |
| 50 | + this.field = query.field; |
| 51 | + this.boost = boost; |
| 52 | + } |
| 53 | + |
| 54 | + abstract VectorScorer vectorScorer(LeafReaderContext leafReaderContext) throws IOException; |
| 55 | + |
| 56 | + @Override |
| 57 | + public Explanation explain(LeafReaderContext leafReaderContext, int i) throws IOException { |
| 58 | + VectorScorer vectorScorer = vectorScorer(leafReaderContext); |
| 59 | + if (vectorScorer == null) { |
| 60 | + return Explanation.noMatch("No vector values found for field: " + field); |
| 61 | + } |
| 62 | + DocIdSetIterator iterator = vectorScorer.iterator(); |
| 63 | + iterator.advance(i); |
| 64 | + if (iterator.docID() == i) { |
| 65 | + float score = vectorScorer.score(); |
| 66 | + return Explanation.match(vectorScorer.score() * boost, "found vector with calculated similarity: " + score); |
| 67 | + } |
| 68 | + return Explanation.noMatch("Document not found in vector values for field: " + field); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException { |
| 73 | + VectorScorer vectorScorer = vectorScorer(leafReaderContext); |
| 74 | + if (vectorScorer == null) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + return new DenseVectorScorer(this, vectorScorer); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean isCacheable(LeafReaderContext leafReaderContext) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static class Floats extends DenseVectorQuery { |
| 87 | + |
| 88 | + private final float[] query; |
| 89 | + |
| 90 | + public Floats(float[] query, String field) { |
| 91 | + super(field); |
| 92 | + this.query = query; |
| 93 | + } |
| 94 | + |
| 95 | + public float[] getQuery() { |
| 96 | + return query; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String toString(String field) { |
| 101 | + return "DenseVectorQuery.Floats"; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { |
| 106 | + return new DenseVectorWeight(Floats.this, boost) { |
| 107 | + @Override |
| 108 | + VectorScorer vectorScorer(LeafReaderContext leafReaderContext) throws IOException { |
| 109 | + FloatVectorValues vectorValues = leafReaderContext.reader().getFloatVectorValues(field); |
| 110 | + if (vectorValues == null) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + return vectorValues.scorer(query); |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean equals(Object o) { |
| 120 | + if (this == o) return true; |
| 121 | + if (o == null || getClass() != o.getClass()) return false; |
| 122 | + Floats floats = (Floats) o; |
| 123 | + return Objects.equals(field, floats.field) && Objects.deepEquals(query, floats.query); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int hashCode() { |
| 128 | + return Objects.hash(field, Arrays.hashCode(query)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public static class Bytes extends DenseVectorQuery { |
| 133 | + |
| 134 | + private final byte[] query; |
| 135 | + |
| 136 | + public Bytes(byte[] query, String field) { |
| 137 | + super(field); |
| 138 | + this.query = query; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public String toString(String field) { |
| 143 | + return "DenseVectorQuery.Bytes"; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { |
| 148 | + return new DenseVectorWeight(Bytes.this, boost) { |
| 149 | + @Override |
| 150 | + VectorScorer vectorScorer(LeafReaderContext leafReaderContext) throws IOException { |
| 151 | + ByteVectorValues vectorValues = leafReaderContext.reader().getByteVectorValues(field); |
| 152 | + if (vectorValues == null) { |
| 153 | + return null; |
| 154 | + } |
| 155 | + return vectorValues.scorer(query); |
| 156 | + } |
| 157 | + }; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public boolean equals(Object o) { |
| 162 | + if (this == o) return true; |
| 163 | + if (o == null || getClass() != o.getClass()) return false; |
| 164 | + Bytes bytes = (Bytes) o; |
| 165 | + return Objects.equals(field, bytes.field) && Objects.deepEquals(query, bytes.query); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public int hashCode() { |
| 170 | + return Objects.hash(field, Arrays.hashCode(query)); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + static class DenseVectorScorer extends Scorer { |
| 175 | + |
| 176 | + private final VectorScorer vectorScorer; |
| 177 | + private final DocIdSetIterator iterator; |
| 178 | + private final float boost; |
| 179 | + |
| 180 | + DenseVectorScorer(DenseVectorWeight weight, VectorScorer vectorScorer) { |
| 181 | + super(weight); |
| 182 | + this.vectorScorer = vectorScorer; |
| 183 | + this.iterator = vectorScorer.iterator(); |
| 184 | + this.boost = weight.boost; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public DocIdSetIterator iterator() { |
| 189 | + return vectorScorer.iterator(); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public float getMaxScore(int i) throws IOException { |
| 194 | + // TODO: can we optimize this at all? |
| 195 | + return Float.POSITIVE_INFINITY; |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public float score() throws IOException { |
| 200 | + assert iterator.docID() != -1; |
| 201 | + return vectorScorer.score() * boost; |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public int docID() { |
| 206 | + return iterator.docID(); |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments