Skip to content

Commit

Permalink
add alterCollectionFieldProperties API (#392)
Browse files Browse the repository at this point in the history
* support alterCollectionField api

Signed-off-by: ryjiang <[email protected]>

* rename

Signed-off-by: ryjiang <[email protected]>

* Add alterCollectionFieldProperties API

Signed-off-by: ryjiang <[email protected]>

* update comment

Signed-off-by: ryjiang <[email protected]>

---------

Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid authored Dec 23, 2024
1 parent edd1052 commit b57d2d9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
51 changes: 50 additions & 1 deletion milvus/grpc/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
CreateCollectionWithSchemaReq,
FieldSchema,
DropCollectionPropertiesReq,
AlterCollectionFieldPropertiesReq,
isVectorType,
} from '../';

Expand Down Expand Up @@ -364,7 +365,7 @@ export class Collection extends Database {
* const milvusClient = new milvusClient(MILUVS_ADDRESS);
* const resStatus = await milvusClient.dropCollectionProperties({
* collection_name: 'my-collection',
* delete_keys: ["collection.ttl.seconds"]
* delete_keys: ["collection.ttl.seconds"]
* });
* ```
*
Expand Down Expand Up @@ -394,6 +395,54 @@ export class Collection extends Database {
return promise;
}

/**
* Modifies a collection field's properties.
* Note that this operation only modifies the properties of the field, not the field itself.
*
* @param {AlterCollectionFieldPropertiesReq} data - The request parameters.
* @param {string} data.collection_name - The name of the collection to modify.
* @param {string} data.field_name - The name of the field to modify.
* @param {Object} data.properties - The properties to modify. For example, to change field mmap setting and max_length, use { 'mmap.enabled', true, max_length: 128}.
* @param {string} [data.db_name] - The name of the database where the collection is located.
* @param {number} [data.timeout] - An optional duration of time in milliseconds to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined.
*
* @returns {Promise<ResStatus>} The response status of the operation.
* @returns {string} status.error_code - The error code of the operation.
* @returns {string} status.reason - The reason for the error, if any.
*
* @example
* ```
* const milvusClient = new milvusClient(MILUVS_ADDRESS);
* const resStatus = await milvusClient.alterCollectionField({
* collection_name: 'my-collection',
* field_name: 'my-field',
* properties: {"mmap.enabled": true}
* });
* ```
*/
async alterCollectionFieldProperties(
data: AlterCollectionFieldPropertiesReq
): Promise<ResStatus> {
const req: any = {
collection_name: data.collection_name,
field_name: data.field_name,
properties: parseToKeyValue(data.properties),
};

if (data.db_name) {
req.db_name = data.db_name;
}

const promise = await promisify(
this.channelPool,
'AlterCollectionField',
req,
data?.timeout || this.timeout
);

return promise;
}

// alias
list_collections = this.showCollections;
// alias
Expand Down
8 changes: 7 additions & 1 deletion milvus/types/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export type TypeParamKey =
| 'max_capacity'
| 'analyzer_params'
| 'enable_analyzer'
| 'enable_match';
| 'enable_match'
| 'mmap.enabled';

// returned from milvus
export type FieldSchema = {
Expand Down Expand Up @@ -268,6 +269,11 @@ export interface AlterCollectionReq extends collectionNameReq {
delete_keys?: string[]; // optional, deleted properties, strings array
}

export interface AlterCollectionFieldPropertiesReq extends collectionNameReq {
field_name: string; // required, field name
properties: Properties; // required, properties
}

export interface DropCollectionPropertiesReq extends collectionNameReq {
properties: string[]; // required, deleted properties, strings array
}
Expand Down
1 change: 1 addition & 0 deletions milvus/utils/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export const assignTypeParams = (
'enable_match',
'enable_analyzer',
'analyzer_params',
'mmap.enabled',
]
): FieldType => {
const newField = cloneObj<FieldType>(field);
Expand Down
45 changes: 44 additions & 1 deletion test/grpc/Collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '../tools';
import { timeoutTest } from '../tools';

const milvusClient = new MilvusClient({ address: IP });
const milvusClient = new MilvusClient({ address: IP, logLevel: 'info' });
const COLLECTION_NAME = GENERATE_NAME();
const NUMBER_DIM_COLLECTION_NAME = GENERATE_NAME();
const NEW_COLLECTION_NAME = GENERATE_NAME();
Expand Down Expand Up @@ -436,6 +436,49 @@ describe(`Collection API`, () => {
).toEqual(value2);
});

it(`Alter collection field properties should success`, async () => {
const key = 'mmap.enabled';
const value = true;

const alter = await milvusClient.alterCollectionFieldProperties({
collection_name: LOAD_COLLECTION_NAME,
field_name: 'json',
properties: { [key]: value },
db_name: 'Collection', // pass test case
});

expect(alter.error_code).toEqual(ErrorCode.SUCCESS);

const describe = await milvusClient.describeCollection({
collection_name: LOAD_COLLECTION_NAME,
});

// find json field
const jsonField = describe.schema.fields.find(
f => f.name === 'json'
) as any;
expect(jsonField['mmap.enabled']).toEqual('true');

const alter2 = await milvusClient.alterCollectionFieldProperties({
collection_name: LOAD_COLLECTION_NAME,
field_name: 'varChar',
properties: { max_length: 1024 },
db_name: 'Collection', // pass test case
});
expect(alter2.error_code).toEqual(ErrorCode.SUCCESS);

const describe2 = await milvusClient.describeCollection({
collection_name: LOAD_COLLECTION_NAME,
});

// find varChar field
const varCharField = describe2.schema.fields.find(
f => f.name === 'varChar'
) as any;

expect(varCharField['max_length']).toEqual('1024');
});

it(`Load Collection Sync throw COLLECTION_NAME_IS_REQUIRED`, async () => {
try {
await milvusClient.loadCollectionSync({} as any);
Expand Down
2 changes: 2 additions & 0 deletions test/utils/Format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe('utils/format', () => {
enable_match: true,
analyzer_params: { key: 'value' },
enable_analyzer: true,
'mmap.enabled': true,
} as FieldType;
const expectedOutput = {
name: 'vector',
Expand All @@ -201,6 +202,7 @@ describe('utils/format', () => {
enable_match: 'true',
analyzer_params: JSON.stringify({ key: 'value' }),
enable_analyzer: 'true',
'mmap.enabled': 'true',
},
};
expect(assignTypeParams(field)).toEqual(expectedOutput);
Expand Down

0 comments on commit b57d2d9

Please sign in to comment.