Skip to content

Commit 6132589

Browse files
committed
fix: forbid minimumMatch argument with And operator
1 parent 542f64f commit 6132589

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

src/collections/query/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ export {
247247
BaseHybridOptions,
248248
BaseNearOptions,
249249
BaseNearTextOptions,
250+
Bm25OperatorOptions,
250251
Bm25Options,
251252
FetchObjectByIdOptions,
252253
FetchObjectsOptions,
@@ -266,3 +267,5 @@ export {
266267
QueryReturn,
267268
SearchOptions,
268269
} from './types.js';
270+
271+
export { Bm25Operator } from './utils.js';

src/collections/query/integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe('Testing of the collection.query methods with a simple collection', ()
152152
it('should query with hybrid + bm25Operator', async () => {
153153
const ret = await collection.query.hybrid('carrot', {
154154
limit: 1,
155-
bm25Operator: Bm25Operator.and({ minimumMatch: 1 }),
155+
bm25Operator: Bm25Operator.and(),
156156
});
157157
expect(ret.objects.length).toEqual(1);
158158
expect(ret.objects[0].properties.testProp).toEqual('carrot');

src/collections/query/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ export type Bm25QueryProperty<T> = {
8484
weight: number;
8585
};
8686

87-
export type Bm25OperatorOptions = {
88-
operator: 'And' | 'Or';
89-
minimumMatch: number;
90-
};
87+
export type Bm25OperatorOr = { operator: 'Or'; minimumMatch: number };
88+
export type Bm25OperatorAnd = { operator: 'And' };
89+
90+
export type Bm25OperatorOptions = Bm25OperatorOr | Bm25OperatorAnd;
9191

9292
export type Bm25SearchOptions<T> = {
9393
/** Which properties of the collection to perform the keyword search on. */

src/collections/query/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MultiTargetVectorJoin } from '../index.js';
2-
import { Bm25OperatorOptions, NearVectorInputType, TargetVectorInputType } from './types.js';
2+
import { Bm25OperatorOptions, Bm25OperatorOr, NearVectorInputType, TargetVectorInputType } from './types.js';
33

44
export class NearVectorInputGuards {
55
public static is1DArray(input: NearVectorInputType): input is number[] {
@@ -36,11 +36,11 @@ export class TargetVectorInputGuards {
3636
}
3737

3838
export class Bm25Operator {
39-
static and(opts: Omit<Bm25OperatorOptions, 'operator'>): Bm25OperatorOptions {
40-
return { ...opts, operator: 'And' };
39+
static and(): Bm25OperatorOptions {
40+
return { operator: 'And' };
4141
}
4242

43-
static or(opts: Omit<Bm25OperatorOptions, 'operator'>): Bm25OperatorOptions {
43+
static or(opts: Omit<Bm25OperatorOr, 'operator'>): Bm25OperatorOptions {
4444
return { ...opts, operator: 'Or' };
4545
}
4646
}

src/collections/serialize/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -967,13 +967,14 @@ export class Serialize {
967967
searchOperator?: Bm25OperatorOptions
968968
): SearchOperatorOptions | undefined => {
969969
if (searchOperator) {
970-
return SearchOperatorOptions.fromPartial({
971-
minimumOrTokensMatch: searchOperator.minimumMatch,
972-
operator:
973-
searchOperator.operator === ('And' as const)
974-
? SearchOperatorOptions_Operator.OPERATOR_AND
975-
: SearchOperatorOptions_Operator.OPERATOR_OR,
976-
});
970+
return SearchOperatorOptions.fromPartial(
971+
searchOperator.operator === ('And' as const)
972+
? { operator: SearchOperatorOptions_Operator.OPERATOR_AND }
973+
: {
974+
operator: SearchOperatorOptions_Operator.OPERATOR_OR,
975+
minimumOrTokensMatch: searchOperator.minimumMatch,
976+
}
977+
);
977978
}
978979
};
979980

0 commit comments

Comments
 (0)