Skip to content

Commit

Permalink
Merge branch 'main' of github.com:milvus-io/milvus-sdk-node into defa…
Browse files Browse the repository at this point in the history
…ult_null
  • Loading branch information
shanghaikid committed Oct 24, 2024
2 parents 2e22bc9 + ab7357d commit 00691a0
Show file tree
Hide file tree
Showing 25 changed files with 1,194 additions and 342 deletions.
6 changes: 3 additions & 3 deletions examples/milvus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"benchp": "autocannon http://localhost:4000 -c 1 -p 10"
},
"dependencies": {
"@zilliz/milvus2-sdk-node": "^2.4.3",
"express": "^4.19.2"
"@zilliz/milvus2-sdk-node": "^2.4.9",
"express": "^4.21.0"
},
"devDependencies": {
"@types/express": "^4.17.17",
"autocannon": "^7.11.0"
"autocannon": "^7.15.0"
}
}
227 changes: 142 additions & 85 deletions examples/milvus/yarn.lock

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions examples/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MilvusClient } from '@zilliz/milvus2-sdk-node';

async function getData() {
async function getData(address: string) {
const milvusClient = new MilvusClient({
address: 'localhost:19530',
address,
});

let res: any = await milvusClient.getMetric({
Expand All @@ -17,7 +17,24 @@ async function getData() {
}

export default async function Home() {
const data = await getData();
const address = `10.102.5.223:19530`;

return <>{JSON.stringify(data)}</>;
const data = await getData(address);

return (
<div className="min-h-screen flex items-center justify-center bg-gray-100">
<div className="max-w-4xl w-full p-6 bg-white rounded-lg shadow-md">
<h1 className="text-3xl font-bold text-center mb-4">
Welcome to Milvus
</h1>
<h2 className="text-xl font-bold text-center mb-4">{address}</h2>

<div className="bg-gray-50 p-4 rounded-lg">
<pre className="whitespace-pre-wrap text-sm text-gray-800">
{JSON.stringify(data, null, 2)}
</pre>
</div>
</div>
</div>
);
}
10 changes: 6 additions & 4 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"lint": "next lint"
},
"dependencies": {
"@zilliz/milvus2-sdk-node": "^2.4.3",
"next": "^14.2.4",
"react": "^18",
"react-dom": "^18"
"@uiw/react-codemirror": "^4.23.3",
"@zilliz/milvus2-sdk-node": "^2.4.9",
"codemirror": "^6.0.1",
"next": "^14.2.13",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/node": "^20",
Expand Down
294 changes: 226 additions & 68 deletions examples/nextjs/yarn.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion milvus/const/limits.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// limits
export const MAX_PARTITION_KEY_FIELD_COUNT = 1;
export const MAX_PARTITIONS_NUMBER = 4096;
export const MAX_PARTITIONS_NUMBER = 4096;

export const NO_LIMIT = -1;
7 changes: 7 additions & 0 deletions milvus/const/milvus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export enum MetricType {
SUBSTRUCTURE = 'SUBSTRUCTURE',
// SUPERSTRUCTURE superstructure
SUPERSTRUCTURE = 'SUPERSTRUCTURE',
// BM 25
BM25 = 'BM25',
}

// Index types
Expand Down Expand Up @@ -279,6 +281,11 @@ export enum DataType {
SparseFloatVector = 104,
}

export enum FunctionType {
Unknown = 0,
BM25 = 1,
}

export const VectorDataTypes = [
DataType.BinaryVector,
DataType.FloatVector,
Expand Down
14 changes: 2 additions & 12 deletions milvus/grpc/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ export class BaseClient {
protected schemaProto: Root;
// The Milvus protobuf.
protected milvusProto: Root;
// The milvus collection schema Type
protected collectionSchemaType: Type;
// The milvus field schema Type
protected fieldSchemaType: Type;

// milvus proto
protected readonly protoInternalPath = {
serviceName: 'milvus.proto.milvus.MilvusService',
collectionSchema: 'milvus.proto.schema.CollectionSchema',
fieldSchema: 'milvus.proto.schema.FieldSchema',
functionSchema: 'milvus.proto.schema.FunctionSchema',
};

/**
Expand Down Expand Up @@ -136,14 +134,6 @@ export class BaseClient {
this.schemaProto = protobuf.loadSync(this.protoFilePath.schema);
this.milvusProto = protobuf.loadSync(this.protoFilePath.milvus);

// Get the CollectionSchemaType and FieldSchemaType from the schemaProto object.
this.collectionSchemaType = this.schemaProto.lookupType(
this.protoInternalPath.collectionSchema
);
this.fieldSchemaType = this.schemaProto.lookupType(
this.protoInternalPath.fieldSchema
);

// options
this.channelOptions = {
// Milvus default max_receive_message_length is 100MB, but Milvus support change max_receive_message_length .
Expand Down
19 changes: 16 additions & 3 deletions milvus/grpc/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,28 @@ export class Collection extends Database {
validatePartitionNumbers(num_partitions);
}

// Get the CollectionSchemaType and FieldSchemaType from the schemaProto object.
const schemaTypes = {
collectionSchemaType: this.schemaProto.lookupType(
this.protoInternalPath.collectionSchema
),
fieldSchemaType: this.schemaProto.lookupType(
this.protoInternalPath.fieldSchema
),
functionSchemaType: this.schemaProto.lookupType(
this.protoInternalPath.functionSchema
),
};

// Create the payload object with the collection_name, description, and fields.
// it should follow CollectionSchema in schema.proto
const payload = formatCollectionSchema(data, this.fieldSchemaType);
const payload = formatCollectionSchema(data, schemaTypes);

// Create the collectionParams object from the payload.
const collectionSchema = this.collectionSchemaType.create(payload);
const collectionSchema = schemaTypes.collectionSchemaType.create(payload);

// Encode the collectionParams object to bytes.
const schemaBytes = this.collectionSchemaType
const schemaBytes = schemaTypes.collectionSchemaType
.encode(collectionSchema)
.finish();

Expand Down
7 changes: 7 additions & 0 deletions milvus/grpc/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
getSparseDim,
f32ArrayToBinaryBytes,
getValidDataArray,
NO_LIMIT,
} from '../';
import { Collection } from './Collection';

Expand Down Expand Up @@ -142,9 +143,11 @@ export class Data extends Collection {

// Tip: The field data sequence needs to be set same as `collectionInfo.schema.fields`.
// If primarykey is set `autoid = true`, you cannot insert the data.
// and if function field is set, you need to ignore the field value in the data.
const fieldMap = new Map<string, _Field>(
collectionInfo.schema.fields
.filter(v => !v.is_primary_key || !v.autoID)
.filter(v => !v.is_function_output)
.map(v => [
v.name,
{
Expand Down Expand Up @@ -721,6 +724,10 @@ export class Data extends Collection {
collection_name: data.collection_name,
expr: userExpr,
});
// if limit not set, set it to count
if (!data.limit || data.limit === NO_LIMIT) {
data.limit = count.data;
}
// total should be the minimum of total and count
const total = data.limit > count.data ? count.data : data.limit;
const batchSize =
Expand Down
28 changes: 24 additions & 4 deletions milvus/types/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
LoadState,
DataTypeMap,
ShowCollectionsType,
FunctionType,
} from '../';

// returned from milvus
Expand All @@ -21,7 +22,7 @@ export interface FieldSchema {
index_params: KeyValuePair[];
fieldID: string | number;
name: string;
is_primary_key?: boolean;
is_primary_key: boolean;
description: string;
data_type: keyof typeof DataType;
autoID: boolean;
Expand All @@ -33,6 +34,7 @@ export interface FieldSchema {
is_dynamic?: boolean;
is_clustering_key?: boolean;
nullable?: boolean;
is_function_output: boolean;
}

export interface CollectionData {
Expand All @@ -57,7 +59,7 @@ export interface ReplicaInfo {
node_ids: string[];
}

export type TypeParam = string | number;
export type TypeParam = string | number | Record<string, any>;
export type TypeParamKey = 'dim' | 'max_length' | 'max_capacity';

// create collection
Expand All @@ -68,6 +70,8 @@ export interface FieldType {
element_type?: DataType | keyof typeof DataTypeMap;
is_primary_key?: boolean;
is_partition_key?: boolean;
is_function_output?: boolean;
is_clustering_key?: boolean;
type_params?: {
[key: string]: TypeParam;
};
Expand All @@ -77,6 +81,9 @@ export interface FieldType {
max_length?: TypeParam;
default_value?: number | string;
nullable?: boolean;
enable_match?: boolean;
tokenizer_params?: Record<string, any>;
enable_tokenizer?: boolean;
}

export interface ShowCollectionsReq extends GrpcTimeOut {
Expand All @@ -87,6 +94,15 @@ export interface ShowCollectionsReq extends GrpcTimeOut {

export type Properties = Record<string, string | number | boolean>;

export type Function = {
name: string;
description?: string;
type: FunctionType;
input_field_names: string[];
output_field_names?: string[];
params: Record<string, any>;
};

export interface BaseCreateCollectionReq extends GrpcTimeOut {
// collection name
collection_name: string; // required, collection name
Expand All @@ -100,10 +116,12 @@ export interface BaseCreateCollectionReq extends GrpcTimeOut {
| 'Customized'; // optional,consistency level, default is 'Bounded'
num_partitions?: number; // optional, partitions number, default is 1
partition_key_field?: string; // optional, partition key field
clustring_key_field?: string; // optional, clustring key field
enable_dynamic_field?: boolean; // optional, enable dynamic field, default is false
enableDynamicField?: boolean; // optional, alias of enable_dynamic_field
properties?: Properties;
db_name?: string;
properties?: Properties; // optional, collection properties
db_name?: string; // optional, db name
functions?: Function[]; // optionals, doc-in/doc-out functions
}

export interface CreateCollectionWithFieldsReq extends BaseCreateCollectionReq {
Expand Down Expand Up @@ -187,6 +205,7 @@ export interface CollectionSchema {
enable_dynamic_field: boolean;
autoID: boolean;
fields: FieldSchema[];
functions: Function[];
}

export interface DescribeCollectionResponse extends TimeStamp {
Expand All @@ -205,6 +224,7 @@ export interface DescribeCollectionResponse extends TimeStamp {
shards_num: number;
num_partitions?: string; // int64
db_name: string;
functions: Function[];
}

export interface GetCompactionPlansResponse extends resStatusResponse {
Expand Down
19 changes: 12 additions & 7 deletions milvus/types/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export interface SearchParam {
group_by_field?: string; // group by field
}

// old search api parameter type
// old search api parameter type, deprecated
export interface SearchReq extends collectionNameReq {
anns_field?: string; // your vector field name
partition_names?: string[]; // partition names
Expand All @@ -310,13 +310,18 @@ export interface SearchIteratorReq
limit: number;
}

export type SearchTextType = string | string[];
export type SearchVectorType = VectorTypes | VectorTypes[];
export type SearchDataType = SearchVectorType | SearchTextType;
export type SearchMultipleDataType = VectorTypes[] | SearchTextType[];

// simplified search api parameter type
export interface SearchSimpleReq extends collectionNameReq {
partition_names?: string[]; // partition names
anns_field?: string; // your vector field name
data?: VectorTypes[] | VectorTypes; // vector to search
vector?: VectorTypes; // alias for data
vectors?: VectorTypes[]; // alias for data
anns_field?: string; // your vector field name,rquired if you are searching on multiple vector fields collection
data?: SearchDataType; // vector or text to search
vector?: VectorTypes; // alias for data, deprecated
vectors?: VectorTypes[]; // alias for data, deprecated
output_fields?: string[];
limit?: number; // how many results you want
topk?: number; // limit alias
Expand All @@ -336,7 +341,7 @@ export type HybridSearchSingleReq = Pick<
SearchParam,
'anns_field' | 'ignore_growing' | 'group_by_field'
> & {
data: VectorTypes[] | VectorTypes; // vector to search
data: SearchDataType; // vector to search
expr?: string; // filter expression
params?: keyValueObj; // extra search parameters
transformers?: OutputTransformers; // provide custom data transformer for specific data type like bf16 or f16 vectors
Expand Down Expand Up @@ -421,7 +426,7 @@ export interface QueryReq extends collectionNameReq {

export interface QueryIteratorReq
extends Omit<QueryReq, 'ids' | 'offset' | 'limit'> {
limit: number;
limit?: number;
batchSize: number;
}

Expand Down
Loading

0 comments on commit 00691a0

Please sign in to comment.