1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.VectorSearch = exports.VectorQuery = exports.VectorQueryCombination = void 0;
|
4 | const errors_1 = require("./errors");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var VectorQueryCombination;
|
11 | (function (VectorQueryCombination) {
|
12 | |
13 |
|
14 |
|
15 | VectorQueryCombination["AND"] = "and";
|
16 | |
17 |
|
18 |
|
19 | VectorQueryCombination["OR"] = "or";
|
20 | })(VectorQueryCombination || (exports.VectorQueryCombination = VectorQueryCombination = {}));
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | class 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 |
|
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 |
|
70 |
|
71 |
|
72 |
|
73 | boost(boost) {
|
74 | this._boost = boost;
|
75 | return this;
|
76 | }
|
77 | |
78 |
|
79 |
|
80 |
|
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 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | static create(fieldName, vector) {
|
96 | return new VectorQuery(fieldName, vector);
|
97 | }
|
98 | }
|
99 | exports.VectorQuery = VectorQuery;
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | class 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 |
|
118 |
|
119 | get queries() {
|
120 | return this._queries;
|
121 | }
|
122 | |
123 |
|
124 |
|
125 | get options() {
|
126 | return this._options;
|
127 | }
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 | static fromVectorQuery(query) {
|
134 | return new VectorSearch([query]);
|
135 | }
|
136 | }
|
137 | exports.VectorSearch = VectorSearch;
|