From e9ef1e791788d4b9e2b34e73fc1371b97ae99083 Mon Sep 17 00:00:00 2001 From: ryjiang Date: Mon, 30 Oct 2023 15:38:35 +0800 Subject: [PATCH] update api Signed-off-by: ryjiang --- milvus/MilvusClient.ts | 9 +++++--- milvus/const/defaults.ts | 4 +++- milvus/http/Collection.ts | 11 +++++++++ milvus/types/Http.ts | 6 ++--- test/http/api.spec.ts | 48 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 69 insertions(+), 9 deletions(-) diff --git a/milvus/MilvusClient.ts b/milvus/MilvusClient.ts index 8bfa1185..aaafa1e2 100644 --- a/milvus/MilvusClient.ts +++ b/milvus/MilvusClient.ts @@ -12,6 +12,9 @@ import { CreateCollectionReq, ERROR_REASONS, checkCreateCollectionCompatibility, + DEFAULT_PRIMARY_KEY_FIELD, + DEFAULT_METRIC_TYPE, + DEFAULT_VECTOR_FIELD, } from '.'; import sdkInfo from '../sdk.json'; @@ -91,10 +94,10 @@ export class MilvusClient extends GRPCClient { const { collection_name, dimension, - primary_field_name = 'id', + primary_field_name = DEFAULT_PRIMARY_KEY_FIELD, id_type = DataType.Int64, - metric_type = 'IP', - vector_field_name = 'vector', + metric_type = DEFAULT_METRIC_TYPE, + vector_field_name = DEFAULT_VECTOR_FIELD, enableDynamicField = true, enable_dynamic_field = true, auto_id = false, diff --git a/milvus/const/defaults.ts b/milvus/const/defaults.ts index 5b29f962..fd9dbf1f 100644 --- a/milvus/const/defaults.ts +++ b/milvus/const/defaults.ts @@ -4,7 +4,9 @@ export const DEFAULT_DEBUG = false; export const DEFAULT_MILVUS_PORT = 19530; // default milvus port export const DEFAULT_CONNECT_TIMEOUT = 15 * 1000; // 15s export const DEFAULT_TOPK = 100; // default topk -export const DEFAULT_METRIC_TYPE = 'L2'; +export const DEFAULT_METRIC_TYPE = 'IP'; +export const DEFAULT_VECTOR_FIELD = 'vector'; +export const DEFAULT_PRIMARY_KEY_FIELD = 'id'; export const DEFAULT_MAX_RETRIES = 3; // max retry time export const DEFAULT_RETRY_DELAY = 30; // retry delay, 30ms export const DEFAULT_PARTITIONS_NUMBER = 64; diff --git a/milvus/http/Collection.ts b/milvus/http/Collection.ts index 72c65e99..a8a373a1 100644 --- a/milvus/http/Collection.ts +++ b/milvus/http/Collection.ts @@ -8,6 +8,11 @@ import { HttpBaseResponse, HttpBaseReq, } from '../types'; +import { + DEFAULT_PRIMARY_KEY_FIELD, + DEFAULT_METRIC_TYPE, + DEFAULT_VECTOR_FIELD, +} from '../const'; export function Collection>(Base: T) { return class extends Base { @@ -16,6 +21,12 @@ export function Collection>(Base: T) { data: HttpCollectionCreateReq ): Promise { const url = `/vector/collections/create`; + + // if some keys not provided, using default value + data.metricType = data.metricType || DEFAULT_METRIC_TYPE; + data.primaryField = data.primaryField || DEFAULT_PRIMARY_KEY_FIELD; + data.vectorField = data.vectorField || DEFAULT_VECTOR_FIELD; + return await this.POST(url, data); } diff --git a/milvus/types/Http.ts b/milvus/types/Http.ts index ef74faca..a42852a9 100644 --- a/milvus/types/Http.ts +++ b/milvus/types/Http.ts @@ -48,9 +48,9 @@ export interface HttpBaseResponse { // collection operations export interface HttpCollectionCreateReq extends HttpBaseReq { dimension: number; - metricType: string; - primaryField: string; - vectorField: string; + metricType?: string; + primaryField?: string; + vectorField?: string; description?: string; } // list collection request diff --git a/test/http/api.spec.ts b/test/http/api.spec.ts index 97e12732..fe397954 100644 --- a/test/http/api.spec.ts +++ b/test/http/api.spec.ts @@ -1,4 +1,9 @@ -import { HttpClient, MilvusClient } from '../../milvus'; +import { + HttpClient, + MilvusClient, + DEFAULT_METRIC_TYPE, + DEFAULT_VECTOR_FIELD, +} from '../../milvus'; import { IP, ENDPOINT, @@ -28,6 +33,11 @@ describe(`HTTP API tests`, () => { description: 'des', }; + const createDefaultParams = { + collectionName: 'default_collection_name', + dimension: 128, + }; + const count = 10; const data = generateInsertData( [ @@ -67,22 +77,51 @@ describe(`HTTP API tests`, () => { expect(hasCollection.value).toEqual(true); }); + it('should create collection with only dimension successfully', async () => { + const createDefault = await client.createCollection(createDefaultParams); + + expect(createDefault.code).toEqual(200); + }); + it('should describe collection successfully', async () => { const describe = await client.describeCollection({ collectionName: createParams.collectionName, }); expect(describe.code).toEqual(200); + expect(describe.data.collectionName).toEqual(createParams.collectionName); expect(describe.data.description).toEqual(createParams.description); expect(describe.data.shardsNum).toEqual(1); expect(describe.data.enableDynamic).toEqual(true); expect(describe.data.fields.length).toEqual(2); + expect(describe.data.indexes[0].fieldName).toEqual( + createParams.vectorField + ); + expect(describe.data.indexes[0].metricType).toEqual( + createParams.metricType + ); + }); + + it('should describe default collection successfully', async () => { + const describe = await client.describeCollection({ + collectionName: createDefaultParams.collectionName, + }); + + expect(describe.code).toEqual(200); + expect(describe.data.collectionName).toEqual( + createDefaultParams.collectionName + ); + expect(describe.data.shardsNum).toEqual(1); + expect(describe.data.enableDynamic).toEqual(true); + expect(describe.data.fields.length).toEqual(2); + expect(describe.data.indexes[0].fieldName).toEqual(DEFAULT_VECTOR_FIELD); + expect(describe.data.indexes[0].metricType).toEqual(DEFAULT_METRIC_TYPE); }); it('should list collections successfully', async () => { const list = await client.listCollections(); expect(list.code).toEqual(200); - expect(list.data[0]).toEqual(createParams.collectionName); + expect(list.data.indexOf(createParams.collectionName) !== -1).toEqual(true); }); it('should insert data successfully', async () => { @@ -125,5 +164,10 @@ describe(`HTTP API tests`, () => { }); expect(drop.code).toEqual(200); + + const dropDefault = await client.dropCollection({ + collectionName: createDefaultParams.collectionName, + }); + expect(dropDefault.code).toEqual(200); }); });