|
| 1 | +package io.qdrant.client; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.UUID; |
| 5 | +import io.qdrant.client.grpc.Points.ContextInput; |
| 6 | +import io.qdrant.client.grpc.Points.DiscoverInput; |
| 7 | +import io.qdrant.client.grpc.Points.Fusion; |
| 8 | +import io.qdrant.client.grpc.Points.OrderBy; |
| 9 | +import io.qdrant.client.grpc.Points.PointId; |
| 10 | +import io.qdrant.client.grpc.Points.Query; |
| 11 | +import io.qdrant.client.grpc.Points.RecommendInput; |
| 12 | +import io.qdrant.client.grpc.Points.VectorInput; |
| 13 | + |
| 14 | +import static io.qdrant.client.VectorInputFactory.vectorInput; |
| 15 | +import static io.qdrant.client.VectorInputFactory.multiVectorInput; |
| 16 | + |
| 17 | + |
| 18 | +/** |
| 19 | + * Convenience methods for constructing {@link Query} |
| 20 | + */ |
| 21 | +public final class QueryFactory { |
| 22 | + private QueryFactory() { |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates a {@link Query} for recommendation. |
| 27 | + * |
| 28 | + * @param input An instance of {@link RecommendInput} |
| 29 | + * @return a new instance of {@link Query} |
| 30 | + */ |
| 31 | + public static Query recommend(RecommendInput input) { |
| 32 | + return Query.newBuilder().setRecommend(input).build(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates a {@link Query} for discovery. |
| 37 | + * |
| 38 | + * @param input An instance of {@link DiscoverInput} |
| 39 | + * @return a new instance of {@link Query} |
| 40 | + */ |
| 41 | + public static Query discover(DiscoverInput input) { |
| 42 | + return Query.newBuilder().setDiscover(input).build(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates a {@link Query} for context search. |
| 47 | + * |
| 48 | + * @param input An instance of {@link ContextInput} |
| 49 | + * @return a new instance of {@link Query} |
| 50 | + */ |
| 51 | + public static Query context(ContextInput input) { |
| 52 | + return Query.newBuilder().setContext(input).build(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a {@link Query} for pre-fetch results fusion. |
| 57 | + * |
| 58 | + * @param fusion An instance of {@link Fusion} |
| 59 | + * @return a new instance of {@link Query} |
| 60 | + */ |
| 61 | + public static Query fusion(Fusion fusion) { |
| 62 | + return Query.newBuilder().setFusion(fusion).build(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a {@link Query} to order points by a payload field. |
| 67 | + * |
| 68 | + * @param key Name of the payload field to order by |
| 69 | + * @return a new instance of {@link Query} |
| 70 | + */ |
| 71 | + public static Query orderBy(String key) { |
| 72 | + OrderBy orderBy = OrderBy.newBuilder().setKey(key).build(); |
| 73 | + return Query.newBuilder().setOrderBy(orderBy).build(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Creates a {@link Query} to order points by a payload field. |
| 78 | + * |
| 79 | + * @param orderBy An instance of {@link OrderBy} |
| 80 | + * @return a new instance of {@link Query} |
| 81 | + */ |
| 82 | + public static Query orderBy(OrderBy orderBy) { |
| 83 | + return Query.newBuilder().setOrderBy(orderBy).build(); |
| 84 | + } |
| 85 | + |
| 86 | + // region Nearest search queries |
| 87 | + |
| 88 | + /** |
| 89 | + * Creates a {@link Query} for nearest search. |
| 90 | + * |
| 91 | + * @param input An instance of {@link VectorInput} |
| 92 | + * @return a new instance of {@link Query} |
| 93 | + */ |
| 94 | + public static Query nearest(VectorInput input) { |
| 95 | + return Query.newBuilder().setNearest(input).build(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates a {@link Query} from a list of floats |
| 100 | + * |
| 101 | + * @param values A map of vector names to values |
| 102 | + * @return A new instance of {@link Query} |
| 103 | + */ |
| 104 | + public static Query nearest(List < Float > values) { |
| 105 | + return Query.newBuilder().setNearest(vectorInput(values)).build(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Creates a {@link Query} from a list of floats |
| 110 | + * |
| 111 | + * @param values A list of values |
| 112 | + * @return A new instance of {@link Query} |
| 113 | + */ |
| 114 | + public static Query nearest(float...values) { |
| 115 | + return Query.newBuilder().setNearest(vectorInput(values)).build(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Creates a {@link Query} from a list of floats and integers as indices |
| 120 | + * |
| 121 | + * @param values The list of floats representing the vector. |
| 122 | + * @param indices The list of integers representing the indices. |
| 123 | + * @return A new instance of {@link Query} |
| 124 | + */ |
| 125 | + public static Query nearest(List < Float > values, List < Integer > indices) { |
| 126 | + return Query.newBuilder().setNearest(vectorInput(values, indices)).build(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Creates a {@link Query} from a nested array of floats representing a multi |
| 131 | + * vector |
| 132 | + * |
| 133 | + * @param vectors The nested array of floats. |
| 134 | + * @return A new instance of {@link Query} |
| 135 | + */ |
| 136 | + public static Query nearest(float[][] vectors) { |
| 137 | + return Query.newBuilder().setNearest(multiVectorInput(vectors)).build(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Creates a {@link Query} from a {@link long} |
| 142 | + * |
| 143 | + * @param id The point id |
| 144 | + * @return a new instance of {@link Query} |
| 145 | + */ |
| 146 | + public static Query nearest(long id) { |
| 147 | + return Query.newBuilder().setNearest(vectorInput(id)).build(); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Creates a {@link Query} from a {@link UUID} |
| 152 | + * |
| 153 | + * @param id The pint id |
| 154 | + * @return a new instance of {@link Query} |
| 155 | + */ |
| 156 | + public static Query nearest(UUID id) { |
| 157 | + return Query.newBuilder().setNearest(vectorInput(id)).build(); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Creates a {@link Query} from a {@link PointId} |
| 162 | + * |
| 163 | + * @param id The pint id |
| 164 | + * @return a new instance of {@link Query} |
| 165 | + */ |
| 166 | + public static Query nearest(PointId id) { |
| 167 | + return Query.newBuilder().setNearest(vectorInput(id)).build(); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Creates a {@link Query} from a nested list of floats representing a multi |
| 172 | + * vector |
| 173 | + * |
| 174 | + * @param vectors The nested list of floats. |
| 175 | + * @return A new instance of {@link Query} |
| 176 | + */ |
| 177 | + public static Query nearestMultiVector(List < List < Float >> vectors) { |
| 178 | + return Query.newBuilder().setNearest(multiVectorInput(vectors)).build(); |
| 179 | + } |
| 180 | + |
| 181 | + // endregion |
| 182 | +} |
0 commit comments