|
| 1 | +package io.qdrant.client; |
| 2 | + |
| 3 | +import io.qdrant.client.grpc.Points.Condition; |
| 4 | +import io.qdrant.client.grpc.Points.DecayParamsExpression; |
| 5 | +import io.qdrant.client.grpc.Points.DivExpression; |
| 6 | +import io.qdrant.client.grpc.Points.Expression; |
| 7 | +import io.qdrant.client.grpc.Points.GeoDistance; |
| 8 | +import io.qdrant.client.grpc.Points.MultExpression; |
| 9 | +import io.qdrant.client.grpc.Points.PowExpression; |
| 10 | +import io.qdrant.client.grpc.Points.SumExpression; |
| 11 | + |
| 12 | +/** Convenience methods for constructing {@link Expression} */ |
| 13 | +public final class ExpressionFactory { |
| 14 | + private ExpressionFactory() {} |
| 15 | + |
| 16 | + /** |
| 17 | + * Creates an {@link Expression} from a constant. |
| 18 | + * |
| 19 | + * @param constant The constant float value |
| 20 | + * @return a new instance of {@link Expression} |
| 21 | + */ |
| 22 | + public static Expression constant(float constant) { |
| 23 | + return Expression.newBuilder().setConstant(constant).build(); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates an {@link Expression} from a variable name. |
| 28 | + * |
| 29 | + * @param variable The variable name (e.g., payload key or score reference) |
| 30 | + * @return a new instance of {@link Expression} |
| 31 | + */ |
| 32 | + public static Expression variable(String variable) { |
| 33 | + return Expression.newBuilder().setVariable(variable).build(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates an {@link Expression} from a {@link Condition}. |
| 38 | + * |
| 39 | + * @param condition The condition to evaluate |
| 40 | + * @return a new instance of {@link Expression} |
| 41 | + */ |
| 42 | + public static Expression condition(Condition condition) { |
| 43 | + return Expression.newBuilder().setCondition(condition).build(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates an {@link Expression} from a {@link GeoDistance}. |
| 48 | + * |
| 49 | + * @param geoDistance The geo distance object |
| 50 | + * @return a new instance of {@link Expression} |
| 51 | + */ |
| 52 | + public static Expression geoDistance(GeoDistance geoDistance) { |
| 53 | + return Expression.newBuilder().setGeoDistance(geoDistance).build(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates an {@link Expression} from a date-time constant string. |
| 58 | + * |
| 59 | + * @param datetime The date-time string |
| 60 | + * @return a new instance of {@link Expression} |
| 61 | + */ |
| 62 | + public static Expression datetime(String datetime) { |
| 63 | + return Expression.newBuilder().setDatetime(datetime).build(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates an {@link Expression} from a payload key referencing date-time values. |
| 68 | + * |
| 69 | + * @param datetimeKey The payload key containing date-time values |
| 70 | + * @return a new instance of {@link Expression} |
| 71 | + */ |
| 72 | + public static Expression datetimeKey(String datetimeKey) { |
| 73 | + return Expression.newBuilder().setDatetimeKey(datetimeKey).build(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Creates an {@link Expression} that multiplies values. |
| 78 | + * |
| 79 | + * @param mult The multiplication expression |
| 80 | + * @return a new instance of {@link Expression} |
| 81 | + */ |
| 82 | + public static Expression mult(MultExpression mult) { |
| 83 | + return Expression.newBuilder().setMult(mult).build(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Creates an {@link Expression} that sums values. |
| 88 | + * |
| 89 | + * @param sum The summation expression |
| 90 | + * @return a new instance of {@link Expression} |
| 91 | + */ |
| 92 | + public static Expression sum(SumExpression sum) { |
| 93 | + return Expression.newBuilder().setSum(sum).build(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Creates an {@link Expression} that divides values. |
| 98 | + * |
| 99 | + * @param div The division expression |
| 100 | + * @return a new instance of {@link Expression} |
| 101 | + */ |
| 102 | + public static Expression div(DivExpression div) { |
| 103 | + return Expression.newBuilder().setDiv(div).build(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Creates a negated {@link Expression}. |
| 108 | + * |
| 109 | + * @param expr The expression to negate |
| 110 | + * @return a new instance of {@link Expression} |
| 111 | + */ |
| 112 | + public static Expression negate(Expression expr) { |
| 113 | + return Expression.newBuilder().setNeg(expr).build(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Creates an {@link Expression} representing absolute value. |
| 118 | + * |
| 119 | + * @param expr The expression to wrap with abs() |
| 120 | + * @return a new instance of {@link Expression} |
| 121 | + */ |
| 122 | + public static Expression abs(Expression expr) { |
| 123 | + return Expression.newBuilder().setAbs(expr).build(); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Creates an {@link Expression} representing square root. |
| 128 | + * |
| 129 | + * @param expr The expression to apply sqrt() to |
| 130 | + * @return a new instance of {@link Expression} |
| 131 | + */ |
| 132 | + public static Expression sqrt(Expression expr) { |
| 133 | + return Expression.newBuilder().setSqrt(expr).build(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Creates an {@link Expression} from a {@link PowExpression}. |
| 138 | + * |
| 139 | + * @param pow The power expression (base and exponent) |
| 140 | + * @return a new instance of {@link Expression} |
| 141 | + */ |
| 142 | + public static Expression pow(PowExpression pow) { |
| 143 | + return Expression.newBuilder().setPow(pow).build(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Creates an {@link Expression} representing exponential. |
| 148 | + * |
| 149 | + * @param expr The expression to apply exponential to |
| 150 | + * @return a new instance of {@link Expression} |
| 151 | + */ |
| 152 | + public static Expression exp(Expression expr) { |
| 153 | + return Expression.newBuilder().setExp(expr).build(); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Creates an {@link Expression} representing base-10 logarithm. |
| 158 | + * |
| 159 | + * @param expr The expression to apply log10() to |
| 160 | + * @return a new instance of {@link Expression} |
| 161 | + */ |
| 162 | + public static Expression log10(Expression expr) { |
| 163 | + return Expression.newBuilder().setLog10(expr).build(); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Creates an {@link Expression} representing natural logarithm. |
| 168 | + * |
| 169 | + * @param expr The expression to apply natural log to |
| 170 | + * @return a new instance of {@link Expression} |
| 171 | + */ |
| 172 | + public static Expression ln(Expression expr) { |
| 173 | + return Expression.newBuilder().setLn(expr).build(); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Creates an {@link Expression} representing exponential decay. |
| 178 | + * |
| 179 | + * @param decay The decay parameters |
| 180 | + * @return a new instance of {@link Expression} |
| 181 | + */ |
| 182 | + public static Expression expDecay(DecayParamsExpression decay) { |
| 183 | + return Expression.newBuilder().setExpDecay(decay).build(); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Creates an {@link Expression} representing Gaussian decay. |
| 188 | + * |
| 189 | + * @param decay The decay parameters |
| 190 | + * @return a new instance of {@link Expression} |
| 191 | + */ |
| 192 | + public static Expression gaussDecay(DecayParamsExpression decay) { |
| 193 | + return Expression.newBuilder().setGaussDecay(decay).build(); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Creates an {@link Expression} representing linear decay. |
| 198 | + * |
| 199 | + * @param decay The decay parameters |
| 200 | + * @return a new instance of {@link Expression} |
| 201 | + */ |
| 202 | + public static Expression linDecay(DecayParamsExpression decay) { |
| 203 | + return Expression.newBuilder().setLinDecay(decay).build(); |
| 204 | + } |
| 205 | +} |
0 commit comments