Skip to content

Commit d45f963

Browse files
committed
feat: add helper factory for building and/or operator expressions
1 parent 1247c3d commit d45f963

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/collections/query/integration.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
22
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
3+
import { requireAtLeast } from '../../../test/version.js';
34
import { WeaviateUnsupportedFeatureError } from '../../errors.js';
45
import weaviate, { WeaviateClient } from '../../index.js';
56
import { Collection } from '../collection/index.js';
67
import { CrossReference, Reference } from '../references/index.js';
78
import { GroupByOptions } from '../types/index.js';
9+
import { Bm25Operator } from './utils.js';
810

911
describe('Testing of the collection.query methods with a simple collection', () => {
1012
let client: WeaviateClient;
@@ -132,6 +134,28 @@ describe('Testing of the collection.query methods with a simple collection', ()
132134
expect(ret.objects[0].uuid).toEqual(id);
133135
});
134136

137+
requireAtLeast(1, 31, 0)('bm25 search operator (minimum_should_match)', () => {
138+
it('should query with bm25 + operator', async () => {
139+
const ret = await collection.query.bm25('carrot', {
140+
limit: 1,
141+
operator: Bm25Operator.or({ minimumMatch: 1 }),
142+
});
143+
expect(ret.objects.length).toEqual(1);
144+
expect(ret.objects[0].properties.testProp).toEqual('carrot');
145+
expect(ret.objects[0].uuid).toEqual(id);
146+
});
147+
148+
it('should query with hybrid + bm25Operator', async () => {
149+
const ret = await collection.query.hybrid('carrot', {
150+
limit: 1,
151+
bm25Operator: Bm25Operator.and({ minimumMatch: 1 }),
152+
});
153+
expect(ret.objects.length).toEqual(1);
154+
expect(ret.objects[0].properties.testProp).toEqual('carrot');
155+
expect(ret.objects[0].uuid).toEqual(id);
156+
});
157+
});
158+
135159
it('should query with hybrid and vector', async () => {
136160
const ret = await collection.query.hybrid('carrot', {
137161
limit: 1,

src/collections/query/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export type Bm25QueryProperty<T> = {
8585
};
8686

8787
export type Bm25OperatorOptions = {
88-
operator: 'and' | 'or';
88+
operator: 'And' | 'Or';
8989
minimumMatch: number;
9090
}
9191

src/collections/query/utils.ts

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

44
export class NearVectorInputGuards {
55
public static is1DArray(input: NearVectorInputType): input is number[] {
@@ -34,3 +34,14 @@ export class TargetVectorInputGuards {
3434
return i.combination !== undefined && i.targetVectors !== undefined;
3535
}
3636
}
37+
38+
export class Bm25Operator {
39+
static and(opts: Omit<Bm25OperatorOptions, 'operator'>): Bm25OperatorOptions {
40+
return { ...opts, operator: 'And' };
41+
}
42+
43+
static or(opts: Omit<Bm25OperatorOptions, 'operator'>): Bm25OperatorOptions {
44+
return { ...opts, operator: 'Or' };
45+
}
46+
47+
}

src/collections/serialize/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ export class Serialize {
967967
if (searchOperator) {
968968
return SearchOperatorOptions.fromPartial({
969969
minimumOrTokensMatch: searchOperator.minimumMatch,
970-
operator: searchOperator.operator === 'and' as const ? SearchOperatorOptions_Operator.OPERATOR_AND : SearchOperatorOptions_Operator.OPERATOR_OR,
970+
operator: searchOperator.operator === 'And' as const ? SearchOperatorOptions_Operator.OPERATOR_AND : SearchOperatorOptions_Operator.OPERATOR_OR,
971971
});
972972
}
973973
}
@@ -1080,6 +1080,8 @@ export class Serialize {
10801080
}
10811081
};
10821082
const { targets, targetVectors, vectorBytes, nearText, nearVector } = Serialize.hybridVector(args);
1083+
1084+
console.info(`search operator: ${JSON.stringify(SearchOperatorOptions.toJSON(this.bm25SearchOperator(args.bm25Operator)!))}`);
10831085
return Hybrid.fromPartial({
10841086
query: args.query,
10851087
alpha: args.alpha ? args.alpha : 0.5,

0 commit comments

Comments
 (0)