|
28 | 28 |
|
29 | 29 | namespace Elastic.Clients.Elasticsearch.Mapping;
|
30 | 30 |
|
| 31 | +[JsonConverter(typeof(DenseVectorElementTypeConverter))] |
| 32 | +public enum DenseVectorElementType |
| 33 | +{ |
| 34 | + /// <summary> |
| 35 | + /// <para> |
| 36 | + /// Indexes a 4-byte floating-point value per dimension. |
| 37 | + /// </para> |
| 38 | + /// </summary> |
| 39 | + [EnumMember(Value = "float")] |
| 40 | + Float, |
| 41 | + /// <summary> |
| 42 | + /// <para> |
| 43 | + /// Indexes a 1-byte integer value per dimension. |
| 44 | + /// </para> |
| 45 | + /// </summary> |
| 46 | + [EnumMember(Value = "byte")] |
| 47 | + Byte, |
| 48 | + /// <summary> |
| 49 | + /// <para> |
| 50 | + /// Indexes a single bit per dimension. Useful for very high-dimensional vectors or models that specifically support |
| 51 | + /// bit vectors. |
| 52 | + /// </para> |
| 53 | + /// <para> |
| 54 | + /// NOTE: when using <c>bit</c>, the number of dimensions must be a multiple of <c>8</c> and must represent the number of bits. |
| 55 | + /// </para> |
| 56 | + /// </summary> |
| 57 | + [EnumMember(Value = "bit")] |
| 58 | + Bit |
| 59 | +} |
| 60 | + |
| 61 | +internal sealed class DenseVectorElementTypeConverter : JsonConverter<DenseVectorElementType> |
| 62 | +{ |
| 63 | + public override DenseVectorElementType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 64 | + { |
| 65 | + var enumString = reader.GetString(); |
| 66 | + switch (enumString) |
| 67 | + { |
| 68 | + case "float": |
| 69 | + return DenseVectorElementType.Float; |
| 70 | + case "byte": |
| 71 | + return DenseVectorElementType.Byte; |
| 72 | + case "bit": |
| 73 | + return DenseVectorElementType.Bit; |
| 74 | + } |
| 75 | + |
| 76 | + ThrowHelper.ThrowJsonException(); |
| 77 | + return default; |
| 78 | + } |
| 79 | + |
| 80 | + public override void Write(Utf8JsonWriter writer, DenseVectorElementType value, JsonSerializerOptions options) |
| 81 | + { |
| 82 | + switch (value) |
| 83 | + { |
| 84 | + case DenseVectorElementType.Float: |
| 85 | + writer.WriteStringValue("float"); |
| 86 | + return; |
| 87 | + case DenseVectorElementType.Byte: |
| 88 | + writer.WriteStringValue("byte"); |
| 89 | + return; |
| 90 | + case DenseVectorElementType.Bit: |
| 91 | + writer.WriteStringValue("bit"); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + writer.WriteNullValue(); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +[JsonConverter(typeof(DenseVectorIndexOptionsTypeConverter))] |
| 100 | +public enum DenseVectorIndexOptionsType |
| 101 | +{ |
| 102 | + /// <summary> |
| 103 | + /// <para> |
| 104 | + /// The default index type for <c>float</c> vectors. This utilizes the HNSW algorithm in addition to automatically scalar |
| 105 | + /// quantization for scalable approximate kNN search with <c>element_type</c> of <c>float</c>. |
| 106 | + /// </para> |
| 107 | + /// <para> |
| 108 | + /// This can reduce the memory footprint by 4x at the cost of some accuracy. |
| 109 | + /// </para> |
| 110 | + /// </summary> |
| 111 | + [EnumMember(Value = "int8_hnsw")] |
| 112 | + Int8Hnsw, |
| 113 | + /// <summary> |
| 114 | + /// <para> |
| 115 | + /// This utilizes a brute-force search algorithm in addition to automatically scalar quantization. Only supports |
| 116 | + /// <c>element_type</c> of <c>float</c>. |
| 117 | + /// </para> |
| 118 | + /// </summary> |
| 119 | + [EnumMember(Value = "int8_flat")] |
| 120 | + Int8Flat, |
| 121 | + /// <summary> |
| 122 | + /// <para> |
| 123 | + /// This utilizes the HNSW algorithm in addition to automatically scalar quantization for scalable approximate kNN |
| 124 | + /// search with <c>element_type</c> of <c>float</c>. |
| 125 | + /// </para> |
| 126 | + /// <para> |
| 127 | + /// This can reduce the memory footprint by 8x at the cost of some accuracy. |
| 128 | + /// </para> |
| 129 | + /// </summary> |
| 130 | + [EnumMember(Value = "int4_hnsw")] |
| 131 | + Int4Hnsw, |
| 132 | + /// <summary> |
| 133 | + /// <para> |
| 134 | + /// This utilizes a brute-force search algorithm in addition to automatically half-byte scalar quantization. |
| 135 | + /// Only supports <c>element_type</c> of <c>float</c>. |
| 136 | + /// </para> |
| 137 | + /// </summary> |
| 138 | + [EnumMember(Value = "int4_flat")] |
| 139 | + Int4Flat, |
| 140 | + /// <summary> |
| 141 | + /// <para> |
| 142 | + /// This utilizes the HNSW algorithm for scalable approximate kNN search. This supports all <c>element_type</c> values. |
| 143 | + /// </para> |
| 144 | + /// </summary> |
| 145 | + [EnumMember(Value = "hnsw")] |
| 146 | + Hnsw, |
| 147 | + /// <summary> |
| 148 | + /// <para> |
| 149 | + /// This utilizes a brute-force search algorithm for exact kNN search. This supports all <c>element_type</c> values. |
| 150 | + /// </para> |
| 151 | + /// </summary> |
| 152 | + [EnumMember(Value = "flat")] |
| 153 | + Flat |
| 154 | +} |
| 155 | + |
| 156 | +internal sealed class DenseVectorIndexOptionsTypeConverter : JsonConverter<DenseVectorIndexOptionsType> |
| 157 | +{ |
| 158 | + public override DenseVectorIndexOptionsType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 159 | + { |
| 160 | + var enumString = reader.GetString(); |
| 161 | + switch (enumString) |
| 162 | + { |
| 163 | + case "int8_hnsw": |
| 164 | + return DenseVectorIndexOptionsType.Int8Hnsw; |
| 165 | + case "int8_flat": |
| 166 | + return DenseVectorIndexOptionsType.Int8Flat; |
| 167 | + case "int4_hnsw": |
| 168 | + return DenseVectorIndexOptionsType.Int4Hnsw; |
| 169 | + case "int4_flat": |
| 170 | + return DenseVectorIndexOptionsType.Int4Flat; |
| 171 | + case "hnsw": |
| 172 | + return DenseVectorIndexOptionsType.Hnsw; |
| 173 | + case "flat": |
| 174 | + return DenseVectorIndexOptionsType.Flat; |
| 175 | + } |
| 176 | + |
| 177 | + ThrowHelper.ThrowJsonException(); |
| 178 | + return default; |
| 179 | + } |
| 180 | + |
| 181 | + public override void Write(Utf8JsonWriter writer, DenseVectorIndexOptionsType value, JsonSerializerOptions options) |
| 182 | + { |
| 183 | + switch (value) |
| 184 | + { |
| 185 | + case DenseVectorIndexOptionsType.Int8Hnsw: |
| 186 | + writer.WriteStringValue("int8_hnsw"); |
| 187 | + return; |
| 188 | + case DenseVectorIndexOptionsType.Int8Flat: |
| 189 | + writer.WriteStringValue("int8_flat"); |
| 190 | + return; |
| 191 | + case DenseVectorIndexOptionsType.Int4Hnsw: |
| 192 | + writer.WriteStringValue("int4_hnsw"); |
| 193 | + return; |
| 194 | + case DenseVectorIndexOptionsType.Int4Flat: |
| 195 | + writer.WriteStringValue("int4_flat"); |
| 196 | + return; |
| 197 | + case DenseVectorIndexOptionsType.Hnsw: |
| 198 | + writer.WriteStringValue("hnsw"); |
| 199 | + return; |
| 200 | + case DenseVectorIndexOptionsType.Flat: |
| 201 | + writer.WriteStringValue("flat"); |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | + writer.WriteNullValue(); |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +[JsonConverter(typeof(DenseVectorSimilarityConverter))] |
| 210 | +public enum DenseVectorSimilarity |
| 211 | +{ |
| 212 | + /// <summary> |
| 213 | + /// <para> |
| 214 | + /// Computes the maximum inner product of two vectors. This is similar to <c>dot_product</c>, but doesn't require vectors |
| 215 | + /// to be normalized. This means that each vector’s magnitude can significantly effect the score. |
| 216 | + /// </para> |
| 217 | + /// <para> |
| 218 | + /// The document <c>_score</c> is adjusted to prevent negative values. For <c>max_inner_product</c> values <c>< 0</c>, the <c>_score</c> |
| 219 | + /// is <c>1 / (1 + -1 * max_inner_product(query, vector))</c>. For non-negative <c>max_inner_product</c> results the <c>_score</c> |
| 220 | + /// is calculated <c>max_inner_product(query, vector) + 1</c>. |
| 221 | + /// </para> |
| 222 | + /// </summary> |
| 223 | + [EnumMember(Value = "max_inner_product")] |
| 224 | + MaxInnerProduct, |
| 225 | + /// <summary> |
| 226 | + /// <para> |
| 227 | + /// Computes similarity based on the <c>L2</c> distance (also known as Euclidean distance) between the vectors. |
| 228 | + /// </para> |
| 229 | + /// <para> |
| 230 | + /// The document <c>_score</c> is computed as <c>1 / (1 + l2_norm(query, vector)^2)</c>. |
| 231 | + /// </para> |
| 232 | + /// <para> |
| 233 | + /// For <c>bit</c> vectors, instead of using <c>l2_norm</c>, the <c>hamming</c> distance between the vectors is used. |
| 234 | + /// </para> |
| 235 | + /// <para> |
| 236 | + /// The <c>_score</c> transformation is <c>(numBits - hamming(a, b)) / numBits</c>. |
| 237 | + /// </para> |
| 238 | + /// </summary> |
| 239 | + [EnumMember(Value = "l2_norm")] |
| 240 | + L2Norm, |
| 241 | + /// <summary> |
| 242 | + /// <para> |
| 243 | + /// Computes the dot product of two unit vectors. This option provides an optimized way to perform cosine similarity. |
| 244 | + /// The constraints and computed score are defined by <c>element_type</c>. |
| 245 | + /// </para> |
| 246 | + /// <para> |
| 247 | + /// When <c>element_type</c> is <c>float</c>, all vectors must be unit length, including both document and query vectors. |
| 248 | + /// </para> |
| 249 | + /// <para> |
| 250 | + /// The document <c>_score</c> is computed as <c>(1 + dot_product(query, vector)) / 2</c>. |
| 251 | + /// </para> |
| 252 | + /// <para> |
| 253 | + /// When <c>element_type</c> is <c>byte</c>, all vectors must have the same length including both document and query vectors or |
| 254 | + /// results will be inaccurate. |
| 255 | + /// </para> |
| 256 | + /// <para> |
| 257 | + /// The document <c>_score</c> is computed as <c>0.5 + (dot_product(query, vector) / (32768 * dims))</c> where <c>dims</c> is the |
| 258 | + /// number of dimensions per vector. |
| 259 | + /// </para> |
| 260 | + /// </summary> |
| 261 | + [EnumMember(Value = "dot_product")] |
| 262 | + DotProduct, |
| 263 | + /// <summary> |
| 264 | + /// <para> |
| 265 | + /// Computes the cosine similarity. During indexing Elasticsearch automatically normalizes vectors with <c>cosine</c> |
| 266 | + /// similarity to unit length. This allows to internally use <c>dot_product</c> for computing similarity, which is more |
| 267 | + /// efficient. Original un-normalized vectors can be still accessed through scripts. |
| 268 | + /// </para> |
| 269 | + /// <para> |
| 270 | + /// The document <c>_score</c> is computed as <c>(1 + cosine(query, vector)) / 2</c>. |
| 271 | + /// </para> |
| 272 | + /// <para> |
| 273 | + /// The <c>cosine</c> similarity does not allow vectors with zero magnitude, since cosine is not defined in this case. |
| 274 | + /// </para> |
| 275 | + /// </summary> |
| 276 | + [EnumMember(Value = "cosine")] |
| 277 | + Cosine |
| 278 | +} |
| 279 | + |
| 280 | +internal sealed class DenseVectorSimilarityConverter : JsonConverter<DenseVectorSimilarity> |
| 281 | +{ |
| 282 | + public override DenseVectorSimilarity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 283 | + { |
| 284 | + var enumString = reader.GetString(); |
| 285 | + switch (enumString) |
| 286 | + { |
| 287 | + case "max_inner_product": |
| 288 | + return DenseVectorSimilarity.MaxInnerProduct; |
| 289 | + case "l2_norm": |
| 290 | + return DenseVectorSimilarity.L2Norm; |
| 291 | + case "dot_product": |
| 292 | + return DenseVectorSimilarity.DotProduct; |
| 293 | + case "cosine": |
| 294 | + return DenseVectorSimilarity.Cosine; |
| 295 | + } |
| 296 | + |
| 297 | + ThrowHelper.ThrowJsonException(); |
| 298 | + return default; |
| 299 | + } |
| 300 | + |
| 301 | + public override void Write(Utf8JsonWriter writer, DenseVectorSimilarity value, JsonSerializerOptions options) |
| 302 | + { |
| 303 | + switch (value) |
| 304 | + { |
| 305 | + case DenseVectorSimilarity.MaxInnerProduct: |
| 306 | + writer.WriteStringValue("max_inner_product"); |
| 307 | + return; |
| 308 | + case DenseVectorSimilarity.L2Norm: |
| 309 | + writer.WriteStringValue("l2_norm"); |
| 310 | + return; |
| 311 | + case DenseVectorSimilarity.DotProduct: |
| 312 | + writer.WriteStringValue("dot_product"); |
| 313 | + return; |
| 314 | + case DenseVectorSimilarity.Cosine: |
| 315 | + writer.WriteStringValue("cosine"); |
| 316 | + return; |
| 317 | + } |
| 318 | + |
| 319 | + writer.WriteNullValue(); |
| 320 | + } |
| 321 | +} |
| 322 | + |
31 | 323 | [JsonConverter(typeof(DynamicMappingConverter))]
|
32 | 324 | public enum DynamicMapping
|
33 | 325 | {
|
|
0 commit comments