Skip to content

Commit 5da5f11

Browse files
committed
fix upsert failed on autoid enabled collection (#402)
Signed-off-by: ryjiang <jiangruiyi@gmail.com>
1 parent 502a03c commit 5da5f11

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

milvus/grpc/Data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class Data extends Collection {
146146
// If primarykey is set `autoid = true`, you cannot insert the data.
147147
const fieldMap = new Map<string, Field>(
148148
collectionInfo.schema.fields
149-
.filter(v => !v.is_primary_key || !v.autoID)
149+
.filter(v => !v.autoID || upsert)
150150
.map(v => [
151151
v.name,
152152
{

test/grpc/Upsert.spec.ts

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
genCollectionParams,
1212
VECTOR_FIELD_NAME,
1313
GENERATE_NAME,
14+
dynamicFields,
1415
} from '../tools';
1516

1617
const milvusClient = new MilvusClient({ address: IP });
@@ -29,6 +30,14 @@ const COLLECTION_NAME_AUTO_ID_PARAMS = genCollectionParams({
2930
dim: [4],
3031
vectorType: [DataType.FloatVector],
3132
autoID: true,
33+
enableDynamic: true,
34+
fields: [
35+
{
36+
name: 'other_ids',
37+
data_type: DataType.Int32,
38+
description: '',
39+
},
40+
],
3241
});
3342

3443
const PARTITION_NAME = 'test';
@@ -56,6 +65,30 @@ describe(`Upsert API`, () => {
5665

5766
// create collection autoid = true and float_vector
5867
await milvusClient.createCollection(COLLECTION_NAME_AUTO_ID_PARAMS);
68+
// create index before load
69+
await milvusClient.createIndex({
70+
collection_name: COLLECTION_NAME_AUTO_ID,
71+
field_name: VECTOR_FIELD_NAME,
72+
extra_params: {
73+
index_type: 'IVF_FLAT',
74+
metric_type: 'L2',
75+
params: JSON.stringify({ nlist: 1024 }),
76+
},
77+
});
78+
// load collection
79+
await milvusClient.loadCollectionSync({
80+
collection_name: COLLECTION_NAME_AUTO_ID,
81+
});
82+
// pare data
83+
const vectorsData = generateInsertData(
84+
[...COLLECTION_NAME_AUTO_ID_PARAMS.fields, ...dynamicFields],
85+
10
86+
);
87+
// insert data
88+
await milvusClient.insert({
89+
collection_name: COLLECTION_NAME_AUTO_ID,
90+
fields_data: vectorsData,
91+
});
5992

6093
// create collection autoid = false and binary_vector
6194

@@ -162,20 +195,46 @@ describe(`Upsert API`, () => {
162195
}
163196
});
164197

165-
it(`Upsert Data on float field and autoId is true expect error`, async () => {
166-
const vectorsData = generateInsertData(
167-
COLLECTION_NAME_AUTO_ID_PARAMS.fields,
168-
10
169-
);
198+
it(`Upsert Data on float field and autoId is true expect successful`, async () => {
199+
const query = await milvusClient.query({
200+
collection_name: COLLECTION_NAME_AUTO_ID,
201+
expr: 'id > 0',
202+
limit: 4,
203+
});
204+
205+
const other_ids = query.data.map((item: any) => item.other_ids);
206+
207+
// modify the bool field to true
208+
const vectorsData = query.data.map((item: any) => {
209+
item.bool = true;
210+
item.dynamic_upserted_int32 = 100;
211+
return item;
212+
});
170213

214+
// upsert the data
171215
const params: InsertReq = {
172216
collection_name: COLLECTION_NAME_AUTO_ID,
173217
fields_data: vectorsData,
174218
};
175219

220+
// expect successful
176221
const res = await milvusClient.upsert(params);
222+
expect(res.status.error_code).toEqual(ErrorCode.SUCCESS);
177223

178-
expect(res.status.error_code).toEqual(ErrorCode.IllegalArgument);
224+
// query these id
225+
const query2 = await milvusClient.query({
226+
collection_name: COLLECTION_NAME_AUTO_ID,
227+
expr: `other_ids in [${other_ids.join(',')}]`,
228+
limit: 4,
229+
});
230+
// check the bool field
231+
expect(query2.data.every((item: any) => item.bool)).toBeTruthy();
232+
// check the dynamic field
233+
expect(
234+
query2.data.every(
235+
(item: any) => item.$meta.dynamic_upserted_int32 === 100
236+
)
237+
).toBeTruthy();
179238
});
180239

181240
it(`Upsert Data on different scalar fields`, async () => {

0 commit comments

Comments
 (0)