Skip to content

Commit

Permalink
Fix single search failed on mutliple vectors collection if the anns f…
Browse files Browse the repository at this point in the history
…ield is specified. (#300)

* add more tests

Signed-off-by: ryjiang <jiangruiyi@gmail.com>

* Fix multiple test

Signed-off-by: ryjiang <jiangruiyi@gmail.com>

---------

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
  • Loading branch information
shanghaikid committed Apr 12, 2024
1 parent c77c5cf commit 88fad2f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
13 changes: 8 additions & 5 deletions milvus/utils/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,10 @@ export const buildSearchRequest = (
req = Object.assign(cloneObj(data), singleReq);
} else {
// if it is not hybrid search, and we have built one request, skip
if (requests.length === 1) {
const skip =
requests.length === 1 ||
(typeof req.anns_field !== 'undefined' && req.anns_field !== name);
if (skip) {
continue;
}
}
Expand All @@ -697,9 +700,9 @@ export const buildSearchRequest = (

// create search request
requests.push({
collection_name: data.collection_name,
partition_names: data.partition_names || [],
output_fields: data.output_fields || default_output_fields,
collection_name: req.collection_name,
partition_names: req.partition_names || [],
output_fields: req.output_fields || default_output_fields,
nq: searchReq.nq || searchingVector.length,
dsl: searchReq.expr || searchSimpleReq.filter || '',
dsl_type: DslType.BoolExprV1,
Expand All @@ -712,7 +715,7 @@ export const buildSearchRequest = (
searchReq.search_params || buildSearchParams(req, name)
),
consistency_level:
data.consistency_level || (collectionInfo.consistency_level as any),
req.consistency_level || (collectionInfo.consistency_level as any),
});
} else {
// if field is not vector, add it to output fields
Expand Down
2 changes: 1 addition & 1 deletion test/grpc/Float16Vector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
generateInsertData,
} from '../tools';

const milvusClient = new MilvusClient({ address: IP, logLevel: 'info' });
const milvusClient = new MilvusClient({ address: IP, logLevel: 'debug' });
const COLLECTION_NAME = GENERATE_NAME();

const dbParam = {
Expand Down
75 changes: 52 additions & 23 deletions test/grpc/MultipleVectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ const p = {
vectorType: [
DataType.FloatVector,
DataType.FloatVector,
// DataType.Float16Vector,
DataType.Float16Vector,
DataType.SparseFloatVector,
],
dim: [8, 16],
dim: [8, 16, 4, 8],
};
const collectionParams = genCollectionParams(p);

Expand Down Expand Up @@ -89,12 +90,21 @@ describe(`Multiple vectors API testing`, () => {
metric_type: MetricType.COSINE,
index_type: IndexType.AUTOINDEX,
},
// {
// collection_name: COLLECTION_NAME,
// field_name: 'vector2',
// metric_type: MetricType.COSINE,
// index_type: IndexType.AUTOINDEX,
// },
{
collection_name: COLLECTION_NAME,
field_name: 'vector2',
metric_type: MetricType.COSINE,
index_type: IndexType.AUTOINDEX,
},
{
collection_name: COLLECTION_NAME,
field_name: 'vector3',
metric_type: MetricType.IP,
index_type: IndexType.SPARSE_WAND,
params: {
drop_ratio_build: 0.2,
},
},
]);

expect(indexes.error_code).toEqual(ErrorCode.SUCCESS);
Expand All @@ -112,18 +122,18 @@ describe(`Multiple vectors API testing`, () => {
const query = await milvusClient.query({
collection_name: COLLECTION_NAME,
filter: 'id > 0',
output_fields: ['vector', 'vector1'],
output_fields: ['vector', 'vector1', 'vector2', 'vector3'],
});

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

const item = query.data[0];
expect(item.vector.length).toEqual(p.dim[0]);
expect(item.vector1.length).toEqual(p.dim[1]);
// expect(item.vector2.length).toEqual(p.dim[2]);
expect(item.vector2.length).toEqual(p.dim[2]);
});

it(`search multiple vector collection with old search api should be successful`, async () => {
it(`search multiple vector collection with single vector search should be successful`, async () => {
// search default first vector field
const search0 = await milvusClient.search({
collection_name: COLLECTION_NAME,
Expand All @@ -150,16 +160,26 @@ describe(`Multiple vectors API testing`, () => {
expect(search2.status.error_code).toEqual(ErrorCode.SUCCESS);
expect(search2.results.length).toEqual(5);

// // search third vector field
// const search3 = await milvusClient.search({
// collection_name: COLLECTION_NAME,
// data: [1, 2, 3, 4, 5, 6, 7, 8],
// anns_field: 'vector2',
// });
// search third vector field
const searchF16 = await milvusClient.search({
collection_name: COLLECTION_NAME,
data: [1, 2, 3, 4],
anns_field: 'vector2',
});

expect(searchF16.status.error_code).toEqual(ErrorCode.SUCCESS);
expect(searchF16.results.length).toEqual(search.results.length);

// search the fourth vector field
const searchSparse = await milvusClient.search({
collection_name: COLLECTION_NAME,
data: { 1: 2, 3: 4 },
anns_field: 'vector3',
limit: 10,
});

// expect(search3.status.error_code).toEqual(ErrorCode.SUCCESS);
// expect(search3.results.length).toEqual(search.results.length);
// expect(search3.results).toEqual(search.results);
expect(searchSparse.status.error_code).toEqual(ErrorCode.SUCCESS);
expect(searchSparse.results.length).toBeGreaterThan(0);
});

it(`hybrid search with rrf ranker set should be successful`, async () => {
Expand All @@ -175,14 +195,23 @@ describe(`Multiple vectors API testing`, () => {
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
anns_field: 'vector1',
},
{
data: [1, 2, 3, 4],
anns_field: 'vector2',
},
{
data: { 1: 2, 3: 4 },
anns_field: 'vector3',
},
],
rerank: RRFRanker(),
limit: 5,
output_fields: ['id'],
output_fields: ['id', 'vector2', 'vector3'],
});

expect(search.status.error_code).toEqual(ErrorCode.SUCCESS);
expect(search.results.length).toEqual(5);
expect(Object.keys(search.results[0]).length).toEqual(4);
});

it(`hybrid search with weighted ranker set should be successful`, async () => {
Expand Down Expand Up @@ -217,8 +246,8 @@ describe(`Multiple vectors API testing`, () => {
params: { nprobe: 2 },
},
{
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
anns_field: 'vector1',
data: [1, 2, 3, 4],
anns_field: 'vector2',
},
],
limit: 5,
Expand Down

0 comments on commit 88fad2f

Please sign in to comment.