Skip to content

Commit 8d2338b

Browse files
committed
Fix subsearches type and mapping
1 parent 7c1ab25 commit 8d2338b

File tree

3 files changed

+59
-88
lines changed

3 files changed

+59
-88
lines changed

src/graphql/getter.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ describe('hybrid valid searchers', () => {
14151415
});
14161416

14171417
test('query and subsearches', () => {
1418-
const subQuery = `searches:[{vector:[1,2,3],certainty:0.8,targetVectors:["employer"]},{concepts:["accountant"],distance:0.3,moveTo:{concepts:["foo"],objects:[{id:"uuid"}],force:0.8}}]`;
1418+
const subQuery = `searches:[{nearVector:{vector:[1,2,3],certainty:0.8,targetVectors:["employer"]}},{nearText:{concepts:["accountant"],distance:0.3,moveTo:{concepts:["foo"],objects:[{id:"uuid"}],force:0.8}}}]`;
14191419
const expectedQuery = `{Get{Person(hybrid:{query:"accountant",${subQuery}}){name}}}`;
14201420

14211421
new Getter(mockClient)
@@ -1424,21 +1424,22 @@ describe('hybrid valid searchers', () => {
14241424
.withHybrid({
14251425
query: 'accountant',
14261426
searches: [
1427-
{ certainty: 0.8, targetVectors: ['employer'], vector: [1, 2, 3] },
1427+
{ nearVector: { certainty: 0.8, targetVectors: ['employer'], vector: [1, 2, 3] } },
14281428
{
1429-
concepts: ['accountant'],
1430-
distance: 0.3,
1431-
moveTo: {
1432-
concepts: ['foo'],
1433-
objects: [{ id: 'uuid' }],
1434-
force: 0.8,
1429+
nearText: {
1430+
concepts: ['accountant'],
1431+
distance: 0.3,
1432+
moveTo: {
1433+
concepts: ['foo'],
1434+
objects: [{ id: 'uuid' }],
1435+
force: 0.8,
1436+
},
14351437
},
14361438
},
14371439
],
14381440
})
14391441
.do();
14401442

1441-
console.log(mockClient.query.mock.calls);
14421443
expect(mockClient.query).toHaveBeenCalledWith(expectedQuery);
14431444
});
14441445
});

src/graphql/hybrid.ts

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface HybridArgs {
77
properties?: string[];
88
targetVectors?: string[];
99
fusionType?: FusionType;
10-
searches?: (NearTextSubSearch | NearVectorSubSearch)[];
10+
searches?: HybridSubSearch[];
1111
}
1212

1313
export interface NearTextSubSearch {
@@ -25,91 +25,57 @@ export interface NearVectorSubSearch {
2525
targetVectors?: string[];
2626
}
2727

28+
export interface HybridSubSearch {
29+
nearText?: NearTextSubSearch;
30+
nearVector?: NearVectorSubSearch;
31+
}
32+
2833
export enum FusionType {
2934
rankedFusion = 'rankedFusion',
3035
relativeScoreFusion = 'relativeScoreFusion',
3136
}
3237

3338
class GraphQLHybridSubSearch {
34-
constructor() {
35-
if (new.target === GraphQLHybridSubSearch) {
36-
throw new Error('Cannot instantiate abstract class');
37-
}
38-
}
39-
40-
static fromArgs(args: NearTextSubSearch | NearVectorSubSearch): GraphQLHybridSubSearch {
41-
if ('concepts' in args) {
42-
return new GraphQLHybridSubSearchNearText(args);
43-
} else {
44-
return new GraphQLHybridSubSearchNearVector(args);
45-
}
46-
}
47-
48-
toString(): string {
49-
throw new Error('Method not implemented.');
50-
}
51-
}
52-
53-
class GraphQLHybridSubSearchNearText extends GraphQLHybridSubSearch {
54-
private concepts: string[];
55-
private certainty?: number;
56-
private distance?: number;
57-
private moveAwayFrom?: Move;
58-
private moveTo?: Move;
59-
60-
constructor(args: NearTextSubSearch) {
61-
super();
62-
this.concepts = args.concepts;
63-
this.certainty = args.certainty;
64-
this.distance = args.distance;
65-
this.moveAwayFrom = args.moveAwayFrom;
66-
this.moveTo = args.moveTo;
67-
}
68-
69-
toString(): string {
70-
let args = [`concepts:${JSON.stringify(this.concepts)}`];
71-
if (this.certainty) {
72-
args = [...args, `certainty:${this.certainty}`];
73-
}
74-
if (this.distance) {
75-
args = [...args, `distance:${this.distance}`];
76-
}
77-
if (this.moveTo) {
78-
args = [...args, parseMove('moveTo', this.moveTo)];
79-
}
80-
if (this.moveAwayFrom) {
81-
args = [...args, parseMove('moveAwayFrom', this.moveAwayFrom)];
82-
}
83-
return `{${args.join(',')}}`;
84-
}
85-
}
39+
private nearText?: NearTextSubSearch;
40+
private nearVector?: NearVectorSubSearch;
8641

87-
class GraphQLHybridSubSearchNearVector extends GraphQLHybridSubSearch {
88-
private vector: number[];
89-
private certainty?: number;
90-
private distance?: number;
91-
private targetVectors?: string[];
92-
93-
constructor(args: NearVectorSubSearch) {
94-
super();
95-
this.vector = args.vector;
96-
this.certainty = args.certainty;
97-
this.distance = args.distance;
98-
this.targetVectors = args.targetVectors;
42+
constructor(args: HybridSubSearch) {
43+
this.nearText = args.nearText;
44+
this.nearVector = args.nearVector;
9945
}
10046

10147
toString(): string {
102-
let args = [`vector:${JSON.stringify(this.vector)}`];
103-
if (this.certainty) {
104-
args = [...args, `certainty:${this.certainty}`];
48+
let outer: string[] = [];
49+
if (this.nearText !== undefined) {
50+
let inner = [`concepts:${JSON.stringify(this.nearText.concepts)}`];
51+
if (this.nearText.certainty) {
52+
inner = [...inner, `certainty:${this.nearText.certainty}`];
53+
}
54+
if (this.nearText.distance) {
55+
inner = [...inner, `distance:${this.nearText.distance}`];
56+
}
57+
if (this.nearText.moveTo) {
58+
inner = [...inner, parseMove('moveTo', this.nearText.moveTo)];
59+
}
60+
if (this.nearText.moveAwayFrom) {
61+
inner = [...inner, parseMove('moveAwayFrom', this.nearText.moveAwayFrom)];
62+
}
63+
outer = [...outer, `nearText:{${inner.join(',')}}`];
10564
}
106-
if (this.distance) {
107-
args = [...args, `distance:${this.distance}`];
65+
if (this.nearVector !== undefined) {
66+
let inner = [`vector:${JSON.stringify(this.nearVector.vector)}`];
67+
if (this.nearVector.certainty) {
68+
inner = [...inner, `certainty:${this.nearVector.certainty}`];
69+
}
70+
if (this.nearVector.distance) {
71+
inner = [...inner, `distance:${this.nearVector.distance}`];
72+
}
73+
if (this.nearVector.targetVectors && this.nearVector.targetVectors.length > 0) {
74+
inner = [...inner, `targetVectors:${JSON.stringify(this.nearVector.targetVectors)}`];
75+
}
76+
outer = [...outer, `nearVector:{${inner.join(',')}}`];
10877
}
109-
if (this.targetVectors && this.targetVectors.length > 0) {
110-
args = [...args, `targetVectors:${JSON.stringify(this.targetVectors)}`];
111-
}
112-
return `{${args.join(',')}}`;
78+
return `{${outer.join(',')}}`;
11379
}
11480
}
11581

@@ -129,7 +95,7 @@ export default class GraphQLHybrid {
12995
this.properties = args.properties;
13096
this.targetVectors = args.targetVectors;
13197
this.fusionType = args.fusionType;
132-
this.searches = args.searches?.map((search) => GraphQLHybridSubSearch.fromArgs(search));
98+
this.searches = args.searches?.map((search) => new GraphQLHybridSubSearch(search));
13399
}
134100

135101
toString() {

src/graphql/journey.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,10 @@ describe('the graphql journey', () => {
472472
query: '',
473473
searches: [
474474
{
475-
concepts: ['Article'],
476-
certainty: 0.7,
475+
nearText: {
476+
concepts: ['Article'],
477+
certainty: 0.7,
478+
},
477479
},
478480
],
479481
})
@@ -536,8 +538,10 @@ describe('the graphql journey', () => {
536538
query: '',
537539
searches: [
538540
{
539-
vector: searchVec,
540-
distance: 0.3,
541+
nearVector: {
542+
vector: searchVec,
543+
certainty: 0.7,
544+
},
541545
},
542546
],
543547
})

0 commit comments

Comments
 (0)