|
| 1 | +/** |
| 2 | + * Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 |
| 3 | + */ |
| 4 | + |
| 5 | +import { Decoder, DecoderOption, GlideString } from "../BaseClient"; |
| 6 | +import { GlideClient } from "../GlideClient"; |
| 7 | +import { GlideClusterClient } from "../GlideClusterClient"; |
| 8 | +import { Field, FtCreateOptions } from "./GlideFtOptions"; |
| 9 | + |
| 10 | +/** Module for Vector Search commands */ |
| 11 | +export class GlideFt { |
| 12 | + /** |
| 13 | + * Creates an index and initiates a backfill of that index. |
| 14 | + * |
| 15 | + * @param client The client to execute the command. |
| 16 | + * @param indexName The index name for the index to be created. |
| 17 | + * @param schema The fields of the index schema, specifying the fields and their types. |
| 18 | + * @param options Optional arguments for the `FT.CREATE` command. See {@link FtCreateOptions}. |
| 19 | + * |
| 20 | + * @returns If the index is successfully created, returns "OK". |
| 21 | + * |
| 22 | + * @example |
| 23 | + * ```typescript |
| 24 | + * // Example usage of FT.CREATE to create a 6-dimensional JSON index using the HNSW algorithm |
| 25 | + * await GlideFt.create(client, "json_idx1", [{ |
| 26 | + * type: "VECTOR", |
| 27 | + * name: "$.vec", |
| 28 | + * alias: "VEC", |
| 29 | + * attributes: { |
| 30 | + * algorithm: "HNSW", |
| 31 | + * type: "FLOAT32", |
| 32 | + * dimension: 6, |
| 33 | + * distanceMetric: "L2", |
| 34 | + * numberOfEdges: 32, |
| 35 | + * }, |
| 36 | + * }], { |
| 37 | + * dataType: "JSON", |
| 38 | + * prefixes: ["json:"] |
| 39 | + * }); |
| 40 | + * ``` |
| 41 | + */ |
| 42 | + static async create( |
| 43 | + client: GlideClient | GlideClusterClient, |
| 44 | + indexName: GlideString, |
| 45 | + schema: Field[], |
| 46 | + options?: FtCreateOptions, |
| 47 | + ): Promise<"OK"> { |
| 48 | + const args: GlideString[] = ["FT.CREATE", indexName]; |
| 49 | + |
| 50 | + if (options) { |
| 51 | + if ("dataType" in options) { |
| 52 | + args.push("ON", options.dataType); |
| 53 | + } |
| 54 | + |
| 55 | + if ("prefixes" in options && options.prefixes) { |
| 56 | + args.push( |
| 57 | + "PREFIX", |
| 58 | + options.prefixes.length.toString(), |
| 59 | + ...options.prefixes, |
| 60 | + ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + args.push("SCHEMA"); |
| 65 | + |
| 66 | + schema.forEach((f) => { |
| 67 | + args.push(f.name); |
| 68 | + |
| 69 | + if (f.alias) { |
| 70 | + args.push("AS", f.alias); |
| 71 | + } |
| 72 | + |
| 73 | + args.push(f.type); |
| 74 | + |
| 75 | + switch (f.type) { |
| 76 | + case "TAG": { |
| 77 | + if (f.separator) { |
| 78 | + args.push("SEPARATOR", f.separator); |
| 79 | + } |
| 80 | + |
| 81 | + if (f.caseSensitive) { |
| 82 | + args.push("CASESENSITIVE"); |
| 83 | + } |
| 84 | + |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + case "VECTOR": { |
| 89 | + if (f.attributes) { |
| 90 | + args.push(f.attributes.algorithm); |
| 91 | + |
| 92 | + const attributes: GlideString[] = []; |
| 93 | + |
| 94 | + // all VectorFieldAttributes attributes |
| 95 | + if (f.attributes.dimension) { |
| 96 | + attributes.push( |
| 97 | + "DIM", |
| 98 | + f.attributes.dimension.toString(), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + if (f.attributes.distanceMetric) { |
| 103 | + attributes.push( |
| 104 | + "DISTANCE_METRIC", |
| 105 | + f.attributes.distanceMetric.toString(), |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + if (f.attributes.type) { |
| 110 | + attributes.push( |
| 111 | + "TYPE", |
| 112 | + f.attributes.type.toString(), |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + if (f.attributes.initialCap) { |
| 117 | + attributes.push( |
| 118 | + "INITIAL_CAP", |
| 119 | + f.attributes.initialCap.toString(), |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + // VectorFieldAttributesHnsw attributes |
| 124 | + if ("m" in f.attributes && f.attributes.m) { |
| 125 | + attributes.push("M", f.attributes.m.toString()); |
| 126 | + } |
| 127 | + |
| 128 | + if ( |
| 129 | + "efContruction" in f.attributes && |
| 130 | + f.attributes.efContruction |
| 131 | + ) { |
| 132 | + attributes.push( |
| 133 | + "EF_CONSTRUCTION", |
| 134 | + f.attributes.efContruction.toString(), |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + if ( |
| 139 | + "efRuntime" in f.attributes && |
| 140 | + f.attributes.efRuntime |
| 141 | + ) { |
| 142 | + attributes.push( |
| 143 | + "EF_RUNTIME", |
| 144 | + f.attributes.efRuntime.toString(), |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + args.push(attributes.length.toString(), ...attributes); |
| 149 | + } |
| 150 | + |
| 151 | + break; |
| 152 | + } |
| 153 | + |
| 154 | + default: |
| 155 | + // no-op |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + return _handleCustomCommand(client, args, { |
| 160 | + decoder: Decoder.String, |
| 161 | + }) as Promise<"OK">; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * @internal |
| 167 | + */ |
| 168 | +function _handleCustomCommand( |
| 169 | + client: GlideClient | GlideClusterClient, |
| 170 | + args: GlideString[], |
| 171 | + decoderOption: DecoderOption, |
| 172 | +) { |
| 173 | + return client instanceof GlideClient |
| 174 | + ? (client as GlideClient).customCommand(args, decoderOption) |
| 175 | + : (client as GlideClusterClient).customCommand(args, decoderOption); |
| 176 | +} |
0 commit comments