Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cannot read properties of undefined (reading 'nq') when searching with wrong anns field #408

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions milvus/const/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions milvus/utils/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/grpc/MultipleVectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
WeightedRanker,
f32ArrayToF16Bytes,
f16BytesToF32Array,
ERROR_REASONS,
} from '../../milvus';
import {
IP,
Expand Down Expand Up @@ -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);
}
});
});
93 changes: 91 additions & 2 deletions test/utils/Format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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',
Expand Down
Loading