|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.datastore; |
| 18 | + |
| 19 | +import com.google.common.base.MoreObjects; |
| 20 | +import com.google.common.base.MoreObjects.ToStringHelper; |
| 21 | +import com.google.protobuf.ByteString; |
| 22 | +import com.google.protobuf.DoubleValue; |
| 23 | +import com.google.protobuf.Int32Value; |
| 24 | + |
| 25 | +import java.io.Serializable; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +import javax.annotation.Nullable; |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * A query that finds the entities whose vector fields are closest to a certain query vector. |
| 33 | + * Create an instance of `FindNearest` with {@link Query#findNearest}. |
| 34 | + */ |
| 35 | +public final class FindNearest implements Serializable { |
| 36 | + |
| 37 | + private final String vectorProperty; |
| 38 | + private final VectorValue queryVector; |
| 39 | + private final DistanceMeasure measure; |
| 40 | + private final int limit; |
| 41 | + /* |
| 42 | + Optional. Optional name of the field to output the result of the vector |
| 43 | + * distance calculation. |
| 44 | + */ |
| 45 | + private final @Nullable String distanceResultField; |
| 46 | + private final @Nullable Double distanceThreshold; |
| 47 | + |
| 48 | + private static final long serialVersionUID = 4688656124180403551L; |
| 49 | + |
| 50 | + /** Creates a VectorQuery */ |
| 51 | + public FindNearest(String vectorProperty, VectorValue queryVector, DistanceMeasure measure, int limit, @Nullable String distanceResultField,@Nullable Double distanceThreshold) { |
| 52 | + this.vectorProperty = vectorProperty; |
| 53 | + this.queryVector = queryVector; |
| 54 | + this.measure = measure; |
| 55 | + this.limit = limit; |
| 56 | + this.distanceResultField = distanceResultField; |
| 57 | + this.distanceThreshold = distanceThreshold; |
| 58 | + } |
| 59 | + |
| 60 | + public FindNearest(String vectorProperty, VectorValue queryVector, DistanceMeasure measure, int limit) { |
| 61 | + this(vectorProperty, queryVector, measure, limit, null, null); |
| 62 | + } |
| 63 | + |
| 64 | + public FindNearest(String vectorProperty, VectorValue queryVector, DistanceMeasure measure, int limit, @Nullable String distanceResultField) { |
| 65 | + this(vectorProperty, queryVector, measure, limit, distanceResultField, null); |
| 66 | + } |
| 67 | + |
| 68 | + public FindNearest(String vectorProperty, VectorValue queryVector, DistanceMeasure measure, int limit, @Nullable Double distanceThreshold) { |
| 69 | + this(vectorProperty, queryVector, measure, limit, null, distanceThreshold); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public int hashCode() { |
| 74 | + return Objects.hash(vectorProperty, queryVector, measure, limit, distanceResultField, distanceThreshold); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns true if this VectorQuery is equal to the provided object. |
| 79 | + * |
| 80 | + * @param obj The object to compare against. |
| 81 | + * @return Whether this VectorQuery is equal to the provided object. |
| 82 | + */ |
| 83 | + @Override |
| 84 | + public boolean equals(Object obj) { |
| 85 | + if (this == obj) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + if (obj == null || !(obj instanceof FindNearest)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + FindNearest otherQuery = (FindNearest) obj; |
| 92 | + return Objects.equals(vectorProperty, otherQuery.vectorProperty) |
| 93 | + && Objects.equals(queryVector, otherQuery.queryVector) |
| 94 | + && Objects.equals(distanceResultField, otherQuery.distanceResultField) |
| 95 | + && Objects.equals(distanceThreshold, otherQuery.distanceThreshold) |
| 96 | + && limit == otherQuery.limit |
| 97 | + && measure == otherQuery.measure; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String toString() { |
| 102 | + ToStringHelper toStringHelper = MoreObjects.toStringHelper(this); |
| 103 | + toStringHelper.add("vectorProperty", vectorProperty); |
| 104 | + toStringHelper.add("queryVector", queryVector); |
| 105 | + toStringHelper.add("measure", measure); |
| 106 | + toStringHelper.add("limit", limit); |
| 107 | + toStringHelper.add("distanceResultField", distanceResultField); |
| 108 | + toStringHelper.add("distanceThreshold", distanceThreshold); |
| 109 | + return toStringHelper.toString(); |
| 110 | + } |
| 111 | + |
| 112 | + static FindNearest fromPb(com.google.datastore.v1.FindNearest findNearestPb) { |
| 113 | + String vectorProperty = findNearestPb.getVectorProperty().getName(); |
| 114 | + VectorValue queryVector = VectorValue.MARSHALLER.fromProto(findNearestPb.getQueryVector()).build(); |
| 115 | + DistanceMeasure distanceMeasure = DistanceMeasure.valueOf(findNearestPb.getDistanceMeasure().toString()); |
| 116 | + int limit = findNearestPb.getLimit().getValue(); |
| 117 | + String distanceResultField = findNearestPb.getDistanceResultProperty() == null || findNearestPb.getDistanceResultProperty().isEmpty() ? null:findNearestPb.getDistanceResultProperty(); |
| 118 | + Double distanceThreshold = findNearestPb.getDistanceThreshold() == null || findNearestPb.getDistanceThreshold() == DoubleValue.getDefaultInstance() ? null : findNearestPb.getDistanceThreshold().getValue(); |
| 119 | + return new FindNearest(vectorProperty,queryVector, distanceMeasure, limit, distanceResultField, distanceThreshold); |
| 120 | + } |
| 121 | + |
| 122 | + com.google.datastore.v1.FindNearest toPb() { |
| 123 | + com.google.datastore.v1.FindNearest.Builder findNearestPb = com.google.datastore.v1.FindNearest.newBuilder(); |
| 124 | + findNearestPb.getVectorPropertyBuilder().setName(vectorProperty); |
| 125 | + findNearestPb.setQueryVector(queryVector.toPb()); |
| 126 | + findNearestPb.setDistanceMeasure(toProto(measure)); |
| 127 | + findNearestPb.setLimit(Int32Value.of(limit)); |
| 128 | + if (distanceResultField != null) |
| 129 | + { |
| 130 | + findNearestPb.setDistanceResultProperty(distanceResultField); |
| 131 | + } |
| 132 | + if (distanceThreshold != null) { |
| 133 | + findNearestPb.setDistanceThreshold(DoubleValue.of(distanceThreshold)); |
| 134 | + } |
| 135 | + return findNearestPb.build(); |
| 136 | + } |
| 137 | + |
| 138 | + protected static com.google.datastore.v1.FindNearest.DistanceMeasure toProto( |
| 139 | + DistanceMeasure distanceMeasure) { |
| 140 | + switch (distanceMeasure) { |
| 141 | + case COSINE: |
| 142 | + return com.google.datastore.v1.FindNearest.DistanceMeasure.COSINE; |
| 143 | + case EUCLIDEAN: |
| 144 | + return com.google.datastore.v1.FindNearest.DistanceMeasure.EUCLIDEAN; |
| 145 | + case DOT_PRODUCT: |
| 146 | + return com.google.datastore.v1.FindNearest.DistanceMeasure.DOT_PRODUCT; |
| 147 | + default: |
| 148 | + return com.google.datastore.v1.FindNearest.DistanceMeasure.UNRECOGNIZED; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * The distance measure to use when comparing vectors in a {@link FindNearest query}. |
| 154 | + * |
| 155 | + * @see com.google.cloud.firestore.Query#findNearest |
| 156 | + */ |
| 157 | + public enum DistanceMeasure { |
| 158 | + /** |
| 159 | + * COSINE distance compares vectors based on the angle between them, which allows you to measure |
| 160 | + * similarity that isn't based on the vectors' magnitude. We recommend using DOT_PRODUCT with |
| 161 | + * unit normalized vectors instead of COSINE distance, which is mathematically equivalent with |
| 162 | + * better performance. |
| 163 | + */ |
| 164 | + COSINE, |
| 165 | + /** Measures the EUCLIDEAN distance between the vectors. */ |
| 166 | + EUCLIDEAN, |
| 167 | + /** Similar to cosine but is affected by the magnitude of the vectors. */ |
| 168 | + DOT_PRODUCT |
| 169 | + } |
| 170 | +} |
0 commit comments