diff --git a/milvus/const/error.ts b/milvus/const/error.ts index a33eedbf..fa5f7352 100644 --- a/milvus/const/error.ts +++ b/milvus/const/error.ts @@ -60,6 +60,7 @@ export const ERROR_REASONS = { 'Only non-primary key Int64 or VarChar field support partition key.', PARTITION_KEY_FIELD_MAXED_OUT: `Only ${MAX_PARTITION_KEY_FIELD_COUNT} field supports partition key. `, IDS_REQUIRED: 'The `ids` is missing or empty.', + NO_ANNS_FEILD_FOUND_IN_SEARCH: 'Target anns field not found, please check your search parameters.', }; export enum ErrorCode { diff --git a/milvus/utils/Format.ts b/milvus/utils/Format.ts index 3d529431..4677137f 100644 --- a/milvus/utils/Format.ts +++ b/milvus/utils/Format.ts @@ -884,6 +884,11 @@ export const buildSearchRequest = ( ? formatExprValues(searchSimpleReq.exprValues) : undefined; + // if no anns_field found in search request, throw error + if (requests.length === 0) { + throw new Error(ERROR_REASONS.NO_ANNS_FEILD_FOUND_IN_SEARCH); + } + return { isHybridSearch: isHybridSearch, request: isHybridSearch diff --git a/test/grpc/MultipleVectors.spec.ts b/test/grpc/MultipleVectors.spec.ts index f8f24378..dcc0b6d3 100644 --- a/test/grpc/MultipleVectors.spec.ts +++ b/test/grpc/MultipleVectors.spec.ts @@ -8,6 +8,7 @@ import { WeightedRanker, f32ArrayToF16Bytes, f16BytesToF32Array, + ERROR_REASONS, } from '../../milvus'; import { IP, @@ -360,4 +361,16 @@ describe(`Multiple vectors API testing`, () => { originSearch.results.map(r => r.id) ); }); + + it(`should return error, if no anns_field found in search parameters`, async () => { + try { + await milvusClient.search({ + collection_name: COLLECTION_NAME, + anns_field: 'vectorxxx', + data: [1, 2, 3, 4, 5, 6, 7, 8], + }); + } catch (e) { + expect(e.message).toEqual(ERROR_REASONS.NO_ANNS_FEILD_FOUND_IN_SEARCH); + } + }); }); diff --git a/test/utils/Format.spec.ts b/test/utils/Format.spec.ts index 23395b8f..12917813 100644 --- a/test/utils/Format.spec.ts +++ b/test/utils/Format.spec.ts @@ -34,8 +34,6 @@ import { SearchSimpleReq, formatExprValues, } from '../../milvus'; -import { json } from 'stream/consumers'; -import exp from 'constants'; describe('utils/format', () => { it(`all kinds of url should be supported`, async () => { @@ -1039,6 +1037,97 @@ describe('utils/format', () => { ); }); + it(`it should get NO_ANNS_FEILD_FOUND_IN_SEARCH if buildSearchRequest with wrong searchParams`, () => { + // path + const milvusProtoPath = path.resolve( + __dirname, + '../../proto/proto/milvus.proto' + ); + const milvusProto = protobuf.loadSync(milvusProtoPath); + + const searchParams = { + collection_name: 'test', + data: [ + { + data: [1, 2, 3, 4, 5, 6, 7, 8], + anns_field: 'vector3xxx', + params: { nprobe: 2 }, + expr: 'id > 0', + }, + { + data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], + anns_field: 'vector12', + expr: 'id > {value}', + exprValues: { value: 1 }, + }, + ], + limit: 2, + output_fields: ['vector', 'vector1'], + }; + + const describeCollectionResponse = { + status: { error_code: 'Success', reason: '' }, + collection_name: 'test', + collectionID: 0, + consistency_level: 'Session', + num_partitions: '0', + aliases: [], + virtual_channel_names: {}, + physical_channel_names: {}, + start_positions: [], + shards_num: 1, + created_timestamp: '0', + created_utc_timestamp: '0', + properties: [], + db_name: '', + schema: { + name: 'test', + description: '', + enable_dynamic_field: false, + autoID: false, + fields: [ + { + name: 'id', + fieldID: '1', + dataType: 5, + is_primary_key: true, + description: 'id field', + data_type: 'Int64', + type_params: [], + index_params: [], + }, + { + name: 'vector', + fieldID: '2', + dataType: 101, + is_primary_key: false, + description: 'vector field', + data_type: 'FloatVector', + type_params: [{ key: 'dim', value: '3' }], + index_params: [], + }, + { + name: 'vector1', + fieldID: '2', + dataType: 101, + is_primary_key: false, + description: 'vector field2', + data_type: 'FloatVector', + type_params: [{ key: 'dim', value: '3' }], + index_params: [], + }, + ], + }, + } as any; + + try { + buildSearchRequest(searchParams, describeCollectionResponse, milvusProto); + } catch (err) { + console.log(err) + expect(err.message).toEqual(ERROR_REASONS.NO_ANNS_FEILD_FOUND_IN_SEARCH); + } + }); + it('should build search params correctly', () => { const data: SearchSimpleReq = { collection_name: 'test',