Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid committed Oct 30, 2023
1 parent 50ae2fc commit e9ef1e7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
9 changes: 6 additions & 3 deletions milvus/MilvusClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion milvus/const/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions milvus/http/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Constructor<HttpBaseClient>>(Base: T) {
return class extends Base {
Expand All @@ -16,6 +21,12 @@ export function Collection<T extends Constructor<HttpBaseClient>>(Base: T) {
data: HttpCollectionCreateReq
): Promise<HttpBaseResponse> {
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<HttpBaseResponse>(url, data);
}

Expand Down
6 changes: 3 additions & 3 deletions milvus/types/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export interface HttpBaseResponse<T = {}> {
// 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
Expand Down
48 changes: 46 additions & 2 deletions test/http/api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { HttpClient, MilvusClient } from '../../milvus';
import {
HttpClient,
MilvusClient,
DEFAULT_METRIC_TYPE,
DEFAULT_VECTOR_FIELD,
} from '../../milvus';
import {
IP,
ENDPOINT,
Expand Down Expand Up @@ -28,6 +33,11 @@ describe(`HTTP API tests`, () => {
description: 'des',
};

const createDefaultParams = {
collectionName: 'default_collection_name',
dimension: 128,
};

const count = 10;
const data = generateInsertData(
[
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit e9ef1e7

Please sign in to comment.