Skip to content

Commit

Permalink
add delete support
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid committed Dec 20, 2024
1 parent 8159fce commit f203b05
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
12 changes: 10 additions & 2 deletions milvus/grpc/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,17 @@ export class Data extends Collection {
// filter > expr
data.expr = data.filter || data.expr;

const req = data as any;

// if exprValues exist, format it
if (data.exprValues) {
req.expr_template_values = formatExprValues(data.exprValues);
}

const promise = await promisify(
this.channelPool,
'Delete',
data,
req,
data.timeout || this.timeout
);
return promise;
Expand Down Expand Up @@ -439,7 +446,8 @@ export class Data extends Collection {
if ((data as DeleteByFilterReq).filter) {
expr = (data as DeleteByFilterReq).filter;
}
const req = { ...data, expr };
const req = { ...data, expr } as any;

return this.deleteEntities(req);
}

Expand Down
7 changes: 3 additions & 4 deletions milvus/types/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,11 @@ interface BaseDeleteReq extends collectionNameReq {
| 'Bounded'
| 'Eventually'
| 'Customized'; // consistency level
exprValues?: keyValueObj; // template values for filter expression, eg: {key: 'value'}
}

export interface DeleteEntitiesReq extends BaseDeleteReq {
filter?: string; // filter expression
expr?: string; // alias for filter
}
export type DeleteEntitiesReq = BaseDeleteReq &
({ expr?: string; filter?: never } | { filter?: string; expr?: never });

export interface DeleteByIdsReq extends BaseDeleteReq {
ids: string[] | number[]; // primary key values
Expand Down
4 changes: 2 additions & 2 deletions milvus/utils/Grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export const getRetryInterceptor = ({
logger.debug(
`\x1b[32m[Response(${
Date.now() - startTime.getTime()
}ms)]\x1b[0m\x1b[2m${clientId}\x1b[0m>${dbname}>\x1b[1m${methodName}\x1b[0m: ${string}`
}ms)]\x1b[0m\x1b[2m${clientId}\x1b[0m>${dbname}>\x1b[1m${methodName}\x1b[0m: ${msg}`
);

savedMessageNext(savedReceiveMessage);
Expand All @@ -217,7 +217,7 @@ export const getRetryInterceptor = ({
const msg =
string.length > 4096 ? string.slice(0, 4096) + '...' : string;
logger.debug(
`\x1b[34m[Request]\x1b[0m${clientId}>${dbname}>\x1b[1m${methodName}(${timeoutInSeconds})\x1b[0m: ${string}`
`\x1b[34m[Request]\x1b[0m${clientId}>${dbname}>\x1b[1m${methodName}(${timeoutInSeconds})\x1b[0m: ${msg}`
);
savedSendMessage = message;
next(message);
Expand Down
30 changes: 30 additions & 0 deletions test/grpc/Data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ describe(`Data.API`, () => {
expect(res.data.length).toEqual(0);
});

let default_values: string[] = [];
it(`query by ids success`, async () => {
const query = await milvusClient.query({
collection_name: COLLECTION_NAME,
Expand All @@ -682,10 +683,12 @@ describe(`Data.API`, () => {
const query0 = await milvusClient.query({
collection_name: VARCHAR_ID_COLLECTION_NAME,
expr: 'id != ""',
output_fields: ['id', 'default_value'],
});

// get first 3 ids
const ids = query0.data.slice(0, 3).map(d => d.id);
default_values = query0.data.slice(0, 3).map(d => d.default_value);
// query by ids
const queryVarcharIds = await milvusClient.query({
collection_name: VARCHAR_ID_COLLECTION_NAME,
Expand All @@ -695,6 +698,25 @@ describe(`Data.API`, () => {
expect(queryVarcharIds.data.length).toEqual(3);
});

it(`delete entities with exprValues should success`, async () => {
const res = await milvusClient.deleteEntities({
collection_name: COLLECTION_NAME,
expr: 'default_value in {value}',
exprValues: { value: default_values },
});

expect(res.status.error_code).toEqual(ErrorCode.SUCCESS);

// query again
const query = await milvusClient.query({
collection_name: COLLECTION_NAME,
expr: 'default_value in {value}',
exprValues: { value: default_values },
});

expect(query.data.length).toEqual(0);
});

it('delete withouth colleciton name should throw error', async () => {
try {
await milvusClient.deleteEntities({
Expand Down Expand Up @@ -728,6 +750,14 @@ describe(`Data.API`, () => {
});

expect(res2.status.error_code).toEqual(ErrorCode.SUCCESS);

// query again
const query2 = await milvusClient.query({
collection_name: VARCHAR_ID_COLLECTION_NAME,
expr: 'id != ""',
});

expect(query2.data.length).toEqual(0);
});

it(`delete by filter should success`, async () => {
Expand Down

0 comments on commit f203b05

Please sign in to comment.