Skip to content

Commit

Permalink
Add round decimal validation before search request (#109)
Browse files Browse the repository at this point in the history
Signed-off-by: tumao <[email protected]>

Signed-off-by: tumao <[email protected]>
  • Loading branch information
Tumao727 authored Oct 14, 2022
1 parent 49649d0 commit 97811fd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion milvus/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,16 @@ export class Data extends Client {
throw new Error(ERROR_REASONS.SEARCH_DIM_NOT_MATCH);
}

const round_decimal = data.search_params.round_decimal;
if (
round_decimal !== undefined &&
(!Number.isInteger(round_decimal) ||
round_decimal < -1 ||
round_decimal > 6)
) {
throw new Error(ERROR_REASONS.SEARCH_ROUND_DECIMAL_NOT_VALID);
}

// when data type is bytes , we need use protobufjs to transform data to buffer bytes.
const PlaceholderGroup = root.lookupType(
'milvus.proto.common.PlaceholderGroup'
Expand Down Expand Up @@ -377,7 +387,6 @@ export class Data extends Client {
* And if Milvus return like 3.142, Node will add more number after this like 3.142000047683716.
* So the score need to slice by round_decimal
*/
const round_decimal = data.search_params.round_decimal;
if (promise.results) {
/**
* fields_data: what you pass in output_fields, only support non vector fields.
Expand Down
1 change: 1 addition & 0 deletions milvus/const/ErrorReason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum ERROR_REASONS {
SEARCH_NOT_FIND_VECTOR_FIELD = 'Your anns_field cannot find in this collection.',
SEARCH_DIM_NOT_MATCH = 'Your vector dimension is not match your anns_field dimension',
SEARCH_PARAMS_IS_REQUIRED = 'search_params must contains anns_field, metric_type, topk and params.',
SEARCH_ROUND_DECIMAL_NOT_VALID = 'round_decimal should be an integer greater than -2 and less than 7',

DELETE_PARAMS_CHECK = 'Collection name and expr is required.',

Expand Down
21 changes: 21 additions & 0 deletions test/Data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,27 @@ describe('Data.ts Test', () => {
}
});

it('Expr Search should throw SEARCH_ROUND_DECIMAL_NOT_VALID', async () => {
try {
await milvusClient.dataManager.search({
collection_name: COLLECTION_NAME,
expr: '',
vectors: [[1, 2, 3, 4]],
search_params: {
anns_field: VECTOR_FIELD_NAME,
topk: '4',
metric_type: 'L2',
params: JSON.stringify({ nprobe: 1024 }),
round_decimal: 7,
},
output_fields: ['age'],
vector_type: DataType.FloatVector,
});
} catch (err) {
expect(err.message).toEqual(ERROR_REASONS.SEARCH_ROUND_DECIMAL_NOT_VALID);
}
});

it('Query ', async () => {
await milvusClient.dataManager.deleteEntities({
collection_name: COLLECTION_NAME,
Expand Down

0 comments on commit 97811fd

Please sign in to comment.