From b24859d9f741dbffc0d9edb7aba5949dd6b2de8e Mon Sep 17 00:00:00 2001 From: Tommy Smith Date: Wed, 15 Jan 2025 12:31:24 +0100 Subject: [PATCH] Fix bug in configure.invertedIndex where undefined `b` and `k1` were sent to the server --- src/collections/configure/index.ts | 48 ++++++++++++++++---------- src/collections/configure/unit.test.ts | 9 +++-- src/collections/types/internal.ts | 6 ++++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/collections/configure/index.ts b/src/collections/configure/index.ts index 7a1ca15c..f80f7c50 100644 --- a/src/collections/configure/index.ts +++ b/src/collections/configure/index.ts @@ -91,19 +91,25 @@ const configure = { stopwordsRemovals?: string[]; }): InvertedIndexConfigCreate => { return { - bm25: { - b: options.bm25b, - k1: options.bm25k1, - }, + bm25: + options.bm25b || options.bm25k1 + ? { + b: options.bm25b, + k1: options.bm25k1, + } + : undefined, cleanupIntervalSeconds: options.cleanupIntervalSeconds, indexTimestamps: options.indexTimestamps, indexPropertyLength: options.indexPropertyLength, indexNullState: options.indexNullState, - stopwords: { - preset: options.stopwordsPreset, - additions: options.stopwordsAdditions, - removals: options.stopwordsRemovals, - }, + stopwords: + options.stopwordsAdditions || options.stopwordsRemovals || options.stopwordsPreset + ? { + preset: options.stopwordsPreset, + additions: options.stopwordsAdditions, + removals: options.stopwordsRemovals, + } + : undefined, }; }, /** @@ -195,16 +201,22 @@ const reconfigure = { stopwordsRemovals?: string[]; }): InvertedIndexConfigUpdate => { return { - bm25: { - b: options.bm25b, - k1: options.bm25k1, - }, + bm25: + options.bm25b || options.bm25k1 + ? { + b: options.bm25b, + k1: options.bm25k1, + } + : undefined, cleanupIntervalSeconds: options.cleanupIntervalSeconds, - stopwords: { - preset: options.stopwordsPreset, - additions: options.stopwordsAdditions, - removals: options.stopwordsRemovals, - }, + stopwords: + options.stopwordsAdditions || options.stopwordsRemovals || options.stopwordsPreset + ? { + preset: options.stopwordsPreset, + additions: options.stopwordsAdditions, + removals: options.stopwordsRemovals, + } + : undefined, }; }, vectorizer: { diff --git a/src/collections/configure/unit.test.ts b/src/collections/configure/unit.test.ts index 9af374a5..93c50f65 100644 --- a/src/collections/configure/unit.test.ts +++ b/src/collections/configure/unit.test.ts @@ -13,7 +13,7 @@ import { ModuleConfig, VectorConfigCreate, } from '../types/index.js'; -import { configure, reconfigure } from './index.js'; +import { configure } from './index.js'; import { InvertedIndexConfigCreate, MultiTenancyConfigCreate, @@ -25,6 +25,11 @@ import { } from './types/index.js'; describe('Unit testing of the configure & reconfigure factory classes', () => { + it('should create the correct InvertedIndexConfig type with defaults', () => { + const config = configure.invertedIndex({}); + expect(config).toEqual({}); + }); + it('should create the correct InvertedIndexConfig type with all values', () => { const config = configure.invertedIndex({ bm25b: 0.5, @@ -90,7 +95,7 @@ describe('Unit testing of the configure & reconfigure factory classes', () => { }); it('should create the correct ReplicationConfigUpdate type with all values', () => { - const config = reconfigure.replication({ + const config = configure.replication({ asyncEnabled: true, deletionStrategy: 'DeleteOnConflict', factor: 2, diff --git a/src/collections/types/internal.ts b/src/collections/types/internal.ts index a935a789..001c24ab 100644 --- a/src/collections/types/internal.ts +++ b/src/collections/types/internal.ts @@ -124,3 +124,9 @@ export type NonReferenceInputs = Obj extends undefined }; export type MapPhoneNumberType = T extends PhoneNumber ? PhoneNumberInput : T; + +type AtLeastOne = { + [K in keyof T]: Pick; +}[keyof T]; + +export type NonEmpty = keyof T extends never ? never : T;