Skip to content

Commit

Permalink
pref: HttpClient vector search (#343)
Browse files Browse the repository at this point in the history
* fix: HttpClient query and search types

* feat: HttpClient add hybridSearch
  • Loading branch information
zhanshuyou authored Jul 23, 2024
1 parent a2f6d71 commit 9eb302e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions milvus/const/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ export enum ErrorCode {
UpsertAutoIDTrue = 'UpsertAutoIDTrue',
CollectionNotExists = 'CollectionNotExists',
IllegalArgument = 'IllegalArgument',
RateLimit = 'RateLimit',
}
9 changes: 9 additions & 0 deletions milvus/http/Vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
HttpBaseResponse,
FetchOptions,
HttpVectorUpsertResponse,
HttpVectorHybridSearchReq,
} from '../types';

/**
Expand Down Expand Up @@ -79,6 +80,14 @@ export function Vector<T extends Constructor<HttpBaseClient>>(Base: T) {
return await this.POST<HttpVectorSearchResponse>(url, data, options);
}

async hybridSearch(
data: HttpVectorHybridSearchReq,
options?: FetchOptions
) {
const url = `${this.vectorPrefix}/hybrid_search`;
return await this.POST<HttpVectorSearchResponse>(url, data, options);
}

// POST delete collection
async delete(
data: HttpVectorDeleteReq,
Expand Down
28 changes: 25 additions & 3 deletions milvus/types/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export interface HttpVectorQueryReq extends HttpBaseReq {
filter?: string;
limit?: number;
offset?: number;
params?: Record<string, string | number>;
partitionNames?: string[];
}

type QueryResult = Record<string, any>[];
Expand All @@ -204,10 +204,32 @@ export interface HttpVectorQueryResponse
extends HttpBaseResponse<QueryResult> {}

// search request
export interface HttpVectorSearchReq
extends Omit<HttpVectorQueryReq, 'filter'> {
export interface HttpVectorSearchReq extends HttpVectorQueryReq {
data: FloatVector[];
annsField?: string;
groupingField?: string;
searchParams?: Record<string, any>;
}

// hybrid search request
interface HttpVectorHybridSearchParams {
data: FloatVector[];
limit: number;
filter?: string;
outputFields?: string[];
offset?: number;
annsField?: string;
ignoreGrowing?: boolean;
metricType?: string;
params?: Record<string, string | number>;
}

export interface HttpVectorHybridSearchReq extends HttpBaseReq {
search: HttpVectorHybridSearchParams[];
rerank: Record<string, any>;
partitionNames?: string[];
outputFields?: string[];
limit?: number;
}

export interface HttpVectorSearchResponse extends HttpVectorQueryResponse {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zilliz/milvus2-sdk-node",
"author": "[email protected]",
"version": "2.4.4",
"milvusVersion": "v2.4.4",
"milvusVersion": "v2.4.6",
"main": "dist/milvus",
"files": [
"dist"
Expand Down
6 changes: 5 additions & 1 deletion test/grpc/Data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ describe(`Data.API`, () => {
const res = await milvusClient.flush({
collection_names: [COLLECTION_NAME],
});
const segIDs = res.coll_segIDs[COLLECTION_NAME].data;
if (res.status.error_code === ErrorCode.RateLimit) {
expect(res.coll_segIDs[COLLECTION_NAME]).toBeUndefined();
return;
}
const segIDs = res.coll_segIDs[COLLECTION_NAME]?.data;
await milvusClient.getFlushState({
segmentIDs: segIDs,
});
Expand Down
24 changes: 24 additions & 0 deletions test/http/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ export function generateTests(
expect(upsert.data.upsertIds).toEqual([target.id]);
});

it('should hybrid search data successfully', async () => {
const search = await client.hybridSearch({
collectionName: createParams.collectionName,
outputFields: ['*'],
rerank: {
strategy: 'rrf',
params: {
k: 5,
},
},
search: [
{
data: [[1, 2, 3, 4]],
outputFields: ['*'],
limit: 5,
},
],
});

expect(search.code).toEqual(0);
expect(search.data.length).toEqual(5);
expect(typeof search.data[0].distance).toEqual('number');
});

it('should query data and get data and delete successfully', async () => {
const query = await client.query({
collectionName: createParams.collectionName,
Expand Down

0 comments on commit 9eb302e

Please sign in to comment.