UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.VectorSearch = exports.VectorQuery = exports.VectorQueryCombination = void 0;
4const errors_1 = require("./errors");
5/**
6 * Specifies how multiple vector searches are combined.
7 *
8 * @category Full Text Search
9 */
10var VectorQueryCombination;
11(function (VectorQueryCombination) {
12 /**
13 * Indicates that multiple vector queries should be combined with logical AND.
14 */
15 VectorQueryCombination["AND"] = "and";
16 /**
17 * Indicates that multiple vector queries should be combined with logical OR.
18 */
19 VectorQueryCombination["OR"] = "or";
20})(VectorQueryCombination || (exports.VectorQueryCombination = VectorQueryCombination = {}));
21/**
22 * Represents a vector query.
23 *
24 * @category Full Text Search
25 */
26class VectorQuery {
27 constructor(fieldName, vector) {
28 if (!fieldName) {
29 throw new errors_1.InvalidArgumentError(new Error('Must provide a field name.'));
30 }
31 this._fieldName = fieldName;
32 if (!vector) {
33 throw new errors_1.InvalidArgumentError(new Error('Provided vector cannot be empty.'));
34 }
35 if (Array.isArray(vector)) {
36 if (vector.length == 0) {
37 throw new errors_1.InvalidArgumentError(new Error('Provided vector cannot be empty.'));
38 }
39 this._vector = vector;
40 }
41 else if (typeof vector === 'string') {
42 this._vectorBase64 = vector;
43 }
44 else {
45 throw new errors_1.InvalidArgumentError(new Error('Provided vector must be either a number[] or base64 encoded string.'));
46 }
47 }
48 /**
49 * @internal
50 */
51 toJSON() {
52 var _a;
53 const output = {
54 field: this._fieldName,
55 k: (_a = this._numCandidates) !== null && _a !== void 0 ? _a : 3,
56 };
57 if (this._vector) {
58 output['vector'] = this._vector;
59 }
60 else {
61 output['vector_base64'] = this._vectorBase64;
62 }
63 if (this._boost) {
64 output['boost'] = this._boost;
65 }
66 return output;
67 }
68 /**
69 * Adds boost option to vector query.
70 *
71 * @param boost A floating point value.
72 */
73 boost(boost) {
74 this._boost = boost;
75 return this;
76 }
77 /**
78 * Adds numCandidates option to vector query. Value must be >= 1.
79 *
80 * @param numCandidates An integer value.
81 */
82 numCandidates(numCandidates) {
83 if (numCandidates < 1) {
84 throw new errors_1.InvalidArgumentError(new Error('Provided value for numCandidates must be >= 1.'));
85 }
86 this._numCandidates = numCandidates;
87 return this;
88 }
89 /**
90 * Creates a vector query.
91 *
92 * @param fieldName The name of the field in the JSON document that holds the vector.
93 * @param vector List of floating point values that represent the vector.
94 */
95 static create(fieldName, vector) {
96 return new VectorQuery(fieldName, vector);
97 }
98}
99exports.VectorQuery = VectorQuery;
100/**
101 * Represents a vector search.
102 *
103 * @category Full Text Search
104 */
105class VectorSearch {
106 constructor(queries, options) {
107 if (!Array.isArray(queries) || queries.length == 0) {
108 throw new errors_1.InvalidArgumentError(new Error('Provided queries must be an array and cannot be empty.'));
109 }
110 if (!queries.every((q) => q instanceof VectorQuery)) {
111 throw new errors_1.InvalidArgumentError(new Error('All provided queries must be a VectorQuery.'));
112 }
113 this._queries = queries;
114 this._options = options;
115 }
116 /**
117 * @internal
118 */
119 get queries() {
120 return this._queries;
121 }
122 /**
123 * @internal
124 */
125 get options() {
126 return this._options;
127 }
128 /**
129 * Creates a vector search from a single VectorQuery.
130 *
131 * @param query A vectory query that should be a part of the vector search.
132 */
133 static fromVectorQuery(query) {
134 return new VectorSearch([query]);
135 }
136}
137exports.VectorSearch = VectorSearch;