Skip to content

Commit 8fb5445

Browse files
committed
Merge branch 'main' of https://github.com/weaviate/typescript-client into hybrid/add-support-for-query-property-weights
2 parents 9397c86 + 15a1206 commit 8fb5445

File tree

3 files changed

+253
-13
lines changed

3 files changed

+253
-13
lines changed

src/collections/filters/integration.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ describe('Testing of the filter class with a simple collection', () => {
146146
});
147147
expect(res.objects.length).toEqual(2);
148148

149-
const obj1 = res.objects[0];
150-
const obj2 = res.objects[1];
149+
// Return of fetch not necessarily in order due to filter
150+
expect(res.objects.map((o) => o.properties.text)).toContain('two');
151+
expect(res.objects.map((o) => o.properties.text)).toContain('three');
151152

152-
expect(obj1.properties.text).toEqual('two');
153-
expect(obj1.properties.int).toEqual(2);
154-
expect(obj1.properties.float).toEqual(2.2);
155-
expect(obj1.uuid).toEqual(ids[1]);
153+
expect(res.objects.map((o) => o.properties.int)).toContain(2);
154+
expect(res.objects.map((o) => o.properties.int)).toContain(3);
156155

157-
expect(obj2.properties.text).toEqual('three');
158-
expect(obj2.properties.int).toEqual(3);
159-
expect(obj2.properties.float).toEqual(3.3);
160-
expect(obj2.uuid).toEqual(ids[2]);
156+
expect(res.objects.map((o) => o.properties.float)).toContain(2.2);
157+
expect(res.objects.map((o) => o.properties.float)).toContain(3.3);
158+
159+
expect(res.objects.map((o) => o.uuid)).toContain(ids[1]);
160+
expect(res.objects.map((o) => o.uuid)).toContain(ids[2]);
161161
});
162162

163163
it('should filter a fetch objects query with a reference filter', async () => {

src/collections/serialize/index.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class FilterGuards {
141141
};
142142

143143
static isFloat = (argument?: FilterValueType): argument is number => {
144-
return typeof argument === 'number';
144+
return typeof argument === 'number' && !Number.isInteger(argument);
145145
};
146146

147147
static isFloatArray = (argument?: FilterValueType): argument is number[] => {
@@ -806,8 +806,8 @@ export class Serialize {
806806
return FiltersGRPC.fromPartial({
807807
operator: Serialize.operator(filters.operator),
808808
target: filters.target,
809-
valueText: FilterGuards.isText(value) ? value : undefined,
810-
valueTextArray: FilterGuards.isTextArray(value) ? { values: value } : undefined,
809+
valueText: this.filtersGRPCValueText(value),
810+
valueTextArray: this.filtersGRPCValueTextArray(value),
811811
valueInt: FilterGuards.isInt(value) ? value : undefined,
812812
valueIntArray: FilterGuards.isIntArray(value) ? { values: value } : undefined,
813813
valueNumber: FilterGuards.isFloat(value) ? value : undefined,
@@ -819,6 +819,26 @@ export class Serialize {
819819
}
820820
};
821821

822+
private static filtersGRPCValueText = (value: any) => {
823+
if (FilterGuards.isText(value)) {
824+
return value;
825+
} else if (FilterGuards.isDate(value)) {
826+
return value.toISOString();
827+
} else {
828+
return undefined;
829+
}
830+
};
831+
832+
private static filtersGRPCValueTextArray = (value: any) => {
833+
if (FilterGuards.isTextArray(value)) {
834+
return { values: value };
835+
} else if (FilterGuards.isDateArray(value)) {
836+
return { values: value.map((v) => v.toISOString()) };
837+
} else {
838+
return undefined;
839+
}
840+
};
841+
822842
private static filterTargetToREST = (target: FilterTarget): string[] => {
823843
if (target.property) {
824844
return [target.property];

src/collections/serialize/unit.test.ts

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
NearVideoSearch,
3333
PropertiesRequest,
3434
} from '../../proto/v1/search_get.js';
35+
import { Filters as FiltersFactory } from '../filters/classes.js';
3536
import filter from '../filters/index.js';
3637
import { Reference } from '../references/index.js';
3738
import sort from '../sort/index.js';
@@ -489,6 +490,225 @@ describe('Unit testing of Serialize', () => {
489490
],
490491
});
491492
});
493+
494+
describe('.filtersGRPC', () => {
495+
it('should parse a text property', () => {
496+
const f = filter<any>().byProperty('name').equal('test');
497+
const args = Serialize.filtersGRPC(f);
498+
expect(args).toEqual<Filters>({
499+
operator: Filters_Operator.OPERATOR_EQUAL,
500+
on: [],
501+
filters: [],
502+
target: {
503+
property: 'name',
504+
},
505+
valueText: 'test',
506+
});
507+
});
508+
it('should parse a text array property', () => {
509+
const f = filter<any>().byProperty('name').equal(['test1', 'test2']);
510+
const args = Serialize.filtersGRPC(f);
511+
expect(args).toEqual<Filters>({
512+
operator: Filters_Operator.OPERATOR_EQUAL,
513+
on: [],
514+
filters: [],
515+
target: {
516+
property: 'name',
517+
},
518+
valueTextArray: { values: ['test1', 'test2'] },
519+
});
520+
});
521+
it('should parse an int property', () => {
522+
const f = filter<any>().byProperty('age').equal(10);
523+
const args = Serialize.filtersGRPC(f);
524+
expect(args).toEqual<Filters>({
525+
operator: Filters_Operator.OPERATOR_EQUAL,
526+
on: [],
527+
filters: [],
528+
target: {
529+
property: 'age',
530+
},
531+
valueInt: 10,
532+
});
533+
});
534+
it('should parse an int array property', () => {
535+
const f = filter<any>().byProperty('age').equal([10, 20]);
536+
const args = Serialize.filtersGRPC(f);
537+
expect(args).toEqual<Filters>({
538+
operator: Filters_Operator.OPERATOR_EQUAL,
539+
on: [],
540+
filters: [],
541+
target: {
542+
property: 'age',
543+
},
544+
valueIntArray: { values: [10, 20] },
545+
});
546+
});
547+
it('should parse a float property', () => {
548+
const f = filter<any>().byProperty('height').equal(1.8);
549+
const args = Serialize.filtersGRPC(f);
550+
expect(args).toEqual<Filters>({
551+
operator: Filters_Operator.OPERATOR_EQUAL,
552+
on: [],
553+
filters: [],
554+
target: {
555+
property: 'height',
556+
},
557+
valueNumber: 1.8,
558+
});
559+
});
560+
it('should parse a float array property', () => {
561+
const f = filter<any>().byProperty('height').equal([1.8, 2.8]);
562+
const args = Serialize.filtersGRPC(f);
563+
expect(args).toEqual<Filters>({
564+
operator: Filters_Operator.OPERATOR_EQUAL,
565+
on: [],
566+
filters: [],
567+
target: {
568+
property: 'height',
569+
},
570+
valueNumberArray: { values: [1.8, 2.8] },
571+
});
572+
});
573+
it('should parse a boolean property', () => {
574+
const f = filter<any>().byProperty('isHappy').equal(true);
575+
const args = Serialize.filtersGRPC(f);
576+
expect(args).toEqual<Filters>({
577+
operator: Filters_Operator.OPERATOR_EQUAL,
578+
on: [],
579+
filters: [],
580+
target: {
581+
property: 'isHappy',
582+
},
583+
valueBoolean: true,
584+
});
585+
});
586+
it('should parse a boolean array property', () => {
587+
const f = filter<any>().byProperty('isHappy').equal([true, false]);
588+
const args = Serialize.filtersGRPC(f);
589+
expect(args).toEqual<Filters>({
590+
operator: Filters_Operator.OPERATOR_EQUAL,
591+
on: [],
592+
filters: [],
593+
target: {
594+
property: 'isHappy',
595+
},
596+
valueBooleanArray: { values: [true, false] },
597+
});
598+
});
599+
it('should parse a date property', () => {
600+
const date = new Date();
601+
const f = filter<any>().byProperty('birthday').equal(date);
602+
const args = Serialize.filtersGRPC(f);
603+
expect(args).toEqual<Filters>({
604+
operator: Filters_Operator.OPERATOR_EQUAL,
605+
on: [],
606+
filters: [],
607+
target: {
608+
property: 'birthday',
609+
},
610+
valueText: date.toISOString(),
611+
});
612+
});
613+
it('should parse a date array property', () => {
614+
const date1 = new Date();
615+
const date2 = new Date();
616+
const f = filter<any>().byProperty('birthday').equal([date1, date2]);
617+
const args = Serialize.filtersGRPC(f);
618+
expect(args).toEqual<Filters>({
619+
operator: Filters_Operator.OPERATOR_EQUAL,
620+
on: [],
621+
filters: [],
622+
target: {
623+
property: 'birthday',
624+
},
625+
valueTextArray: { values: [date1.toISOString(), date2.toISOString()] },
626+
});
627+
});
628+
it('should parse a geo property', () => {
629+
const f = filter<any>()
630+
.byProperty('location')
631+
.withinGeoRange({ latitude: 1, longitude: 1, distance: 1 });
632+
const args = Serialize.filtersGRPC(f);
633+
expect(args).toEqual<Filters>({
634+
operator: Filters_Operator.OPERATOR_WITHIN_GEO_RANGE,
635+
on: [],
636+
filters: [],
637+
target: {
638+
property: 'location',
639+
},
640+
valueGeo: {
641+
distance: 1,
642+
latitude: 1,
643+
longitude: 1,
644+
},
645+
});
646+
});
647+
it('should parse several filters in a Filters.and', () => {
648+
const f = FiltersFactory.and(
649+
filter<any>().byProperty('name').equal('test'),
650+
filter<any>().byProperty('age').equal(10)
651+
);
652+
const args = Serialize.filtersGRPC(f);
653+
expect(args).toEqual<Filters>({
654+
operator: Filters_Operator.OPERATOR_AND,
655+
on: [],
656+
target: undefined,
657+
filters: [
658+
{
659+
operator: Filters_Operator.OPERATOR_EQUAL,
660+
on: [],
661+
filters: [],
662+
target: {
663+
property: 'name',
664+
},
665+
valueText: 'test',
666+
},
667+
{
668+
operator: Filters_Operator.OPERATOR_EQUAL,
669+
on: [],
670+
filters: [],
671+
target: {
672+
property: 'age',
673+
},
674+
valueInt: 10,
675+
},
676+
],
677+
});
678+
});
679+
it('should parse several filters in a Filters.or', () => {
680+
const f = FiltersFactory.or(
681+
filter<any>().byProperty('name').equal('test'),
682+
filter<any>().byProperty('age').equal(10)
683+
);
684+
const args = Serialize.filtersGRPC(f);
685+
expect(args).toEqual<Filters>({
686+
operator: Filters_Operator.OPERATOR_OR,
687+
on: [],
688+
target: undefined,
689+
filters: [
690+
{
691+
operator: Filters_Operator.OPERATOR_EQUAL,
692+
on: [],
693+
filters: [],
694+
target: {
695+
property: 'name',
696+
},
697+
valueText: 'test',
698+
},
699+
{
700+
operator: Filters_Operator.OPERATOR_EQUAL,
701+
on: [],
702+
filters: [],
703+
target: {
704+
property: 'age',
705+
},
706+
valueInt: 10,
707+
},
708+
],
709+
});
710+
});
711+
});
492712
});
493713

494714
describe('Unit testing of DataGuards', () => {

0 commit comments

Comments
 (0)