Skip to content

Commit f2d5634

Browse files
caoshilicaoshili
caoshili
authored and
caoshili
committed
support array and filtering index && optimize code and bugFix
1 parent 62c64bd commit f2d5634

File tree

12 files changed

+385
-326
lines changed

12 files changed

+385
-326
lines changed

example/example.ts

Lines changed: 123 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,19 @@
1313
*/
1414

1515
import {
16+
MochowClient,
1617
Row,
1718
IndexSchema,
1819
FieldSchema,
1920
ClientConfiguration,
2021
AutoBuildTiming,
21-
PartitionParams,
2222
TableSchema,
2323
FieldType,
2424
IndexType,
2525
MetricType,
2626
PartitionType,
27-
PrimaryKey,
28-
PartitionKey,
2927
ReadConsistency,
30-
UpdateFields,
3128
IndexState,
32-
SearchParams,
33-
ANNSearchParams,
34-
BatchANNSearchParams,
3529
TableState,
3630
ServerErrCode,
3731
InvertedIndexFieldAttribute,
@@ -48,8 +42,14 @@ import {
4842
BM25SearchArgs,
4943
HybridSearchArgs,
5044
HybridSearchRequest,
51-
} from "../mochow/types/index"
52-
import { MochowClient } from "../mochow/MochowClient"
45+
CreateTableArgs,
46+
InsertArgs,
47+
QueryArgs,
48+
SelectArgs,
49+
ElementType,
50+
IndexStructureType,
51+
BatchQueryArgs,
52+
} from "../mochow";
5353

5454
export class MochowTest {
5555
private client: MochowClient
@@ -71,8 +71,7 @@ export class MochowTest {
7171
}
7272

7373
public async createTable() {
74-
let fields: FieldSchema[]
75-
fields = [
74+
let fields: FieldSchema[] = [
7675
{
7776
fieldName: "id",
7877
fieldType: FieldType.String,
@@ -106,6 +105,12 @@ export class MochowTest {
106105
fieldName: "segment",
107106
fieldType: FieldType.Text,
108107
notNull: true
108+
},
109+
{
110+
fieldName: "arrayField",
111+
fieldType: FieldType.Array,
112+
elementType: ElementType.String,
113+
notNull: true
109114
}
110115
]
111116

@@ -117,6 +122,16 @@ export class MochowTest {
117122
field: "bookName",
118123
indexType: IndexType.SecondaryIndex,
119124
},
125+
{
126+
indexName: "filtering_idx",
127+
indexType: IndexType.FilteringIndex,
128+
fields: [
129+
{
130+
field: "author",
131+
indexStructureType: IndexStructureType.Bitmap
132+
}
133+
]
134+
},
120135
{
121136
indexName: "vector_idx",
122137
field: "vector",
@@ -142,10 +157,20 @@ export class MochowTest {
142157
]
143158

144159
// create table
145-
let partitionParams: PartitionParams = { partitionType: PartitionType.HASH, partitionNum: 1 }
146160
let schema: TableSchema = { fields: fields, indexes: indexes }
147-
148-
let resp = await this.client.createTable(this.database_name, this.table_name, "test", 1, partitionParams, true, schema)
161+
let createTableReq: CreateTableArgs = {
162+
database: this.database_name,
163+
table: this.table_name,
164+
description: "test",
165+
replication: 1,
166+
partitionParams: {
167+
partitionType: PartitionType.HASH,
168+
partitionNum: 1,
169+
},
170+
enableDynamicField: false,
171+
schema: schema
172+
}
173+
let resp = await this.client.createTable(createTableReq)
149174
if (resp.code != 0) {
150175
console.log("fail to create table due to: " + resp.msg)
151176
return resp
@@ -186,6 +211,7 @@ export class MochowTest {
186211
"page": 21,
187212
"vector": [0.2123, 0.21, 0.213],
188213
"segment": "富贵功名,前缘分定,为人切莫欺心。",
214+
"arrayField": [],
189215
},
190216
{
191217
"id": "0002",
@@ -194,6 +220,7 @@ export class MochowTest {
194220
"page": 22,
195221
"vector": [0.2123, 0.22, 0.213],
196222
"segment": "正大光明,忠良善果弥深。些些狂妄天加谴,眼前不遇待时临。",
223+
"arrayField": ["细作"],
197224
},
198225
{
199226
"id": "0003",
@@ -202,6 +229,7 @@ export class MochowTest {
202229
"page": 23,
203230
"vector": [0.2123, 0.23, 0.213],
204231
"segment": "细作探知这个消息,飞报吕布。",
232+
"arrayField": ["细作", "吕布"],
205233
},
206234
{
207235
"id": "0004",
@@ -210,6 +238,7 @@ export class MochowTest {
210238
"page": 24,
211239
"vector": [0.2123, 0.24, 0.213],
212240
"segment": "布大惊,与陈宫商议。宫曰:“闻刘玄德新领徐州,可往投之。” 布从其言,竟投徐州来。有人报知玄德。",
241+
"arrayField": ["吕布", "陈宫", "刘玄德"],
213242
},
214243
{
215244
"id": "0005",
@@ -218,9 +247,15 @@ export class MochowTest {
218247
"page": 25,
219248
"vector": [0.2123, 0.24, 0.213],
220249
"segment": "玄德曰:“布乃当今英勇之士,可出迎之。”糜竺曰:“吕布乃虎狼之徒,不可收留;收则伤人矣。",
250+
"arrayField": ["玄德", "吕布", "糜竺"],
221251
}
222252
]
223-
let resp = await this.client.insertRows(this.database_name, this.table_name, data)
253+
let insertArgs: InsertArgs = {
254+
database: this.database_name,
255+
table: this.table_name,
256+
rows: data,
257+
}
258+
let resp = await this.client.upsert(insertArgs)
224259
if (resp.code != 0) {
225260
console.log("fail to upsert row due to: " + resp.msg)
226261
return resp
@@ -229,16 +264,17 @@ export class MochowTest {
229264
}
230265

231266
public async queryData() {
232-
let pk: PrimaryKey = { "id": "0001" }
233-
let projections: string[] = ["id", "bookName"]
234-
let resp = await this.client.queryRow(
235-
this.database_name,
236-
this.table_name,
237-
pk,
238-
undefined,
239-
projections,
240-
false,
241-
ReadConsistency.EVENTUAL)
267+
let queryArgs: QueryArgs = {
268+
database: this.database_name,
269+
table: this.table_name,
270+
primaryKey: {
271+
"id": "0001"
272+
},
273+
projections: ["id", "bookName"],
274+
retrieveVector: false,
275+
readConsistency: ReadConsistency.EVENTUAL
276+
}
277+
let resp = await this.client.query(queryArgs);
242278
if (resp.code != 0) {
243279
console.log("fail to query data due to: " + resp.msg)
244280
return resp
@@ -247,15 +283,43 @@ export class MochowTest {
247283
return resp
248284
}
249285

286+
public async batchQuery() {
287+
let queryArgs: BatchQueryArgs = {
288+
database: this.database_name,
289+
table: this.table_name,
290+
keys: [
291+
{
292+
primaryKey: {
293+
"id": "0001"
294+
}
295+
},
296+
{
297+
primaryKey: {
298+
"id": "0002"
299+
}
300+
}
301+
],
302+
projections: ["id", "bookName"],
303+
retrieveVector: false,
304+
readConsistency: ReadConsistency.EVENTUAL
305+
}
306+
let resp = await this.client.batchQuery(queryArgs);
307+
if (resp.code != 0) {
308+
console.log("fail to query data due to: " + resp.msg)
309+
return resp
310+
}
311+
console.log("batch query result: ", JSON.stringify(resp, null, " "))
312+
return resp
313+
}
314+
250315
public async selectData() {
251-
let projections: string[] = ["id", "bookName"]
252-
let resp = await this.client.selectRows(
253-
this.database_name,
254-
this.table_name,
255-
undefined,
256-
undefined,
257-
projections,
258-
1)
316+
let selectArgs: SelectArgs = {
317+
database: this.database_name,
318+
table: this.table_name,
319+
projections: ["id", "bookName"],
320+
limit: 1
321+
}
322+
let resp = await this.client.select(selectArgs);
259323
if (resp.code != 0) {
260324
console.log("fail to select data due to: " + resp.msg)
261325
return resp
@@ -265,19 +329,19 @@ export class MochowTest {
265329
}
266330

267331
public async updateData() {
268-
let pk: PrimaryKey = { "id": "0001" }
269-
let update: UpdateFields = {
270-
"bookName": "红楼梦",
271-
"author": "曹雪芹",
272-
"page": 100,
273-
"segment": "满纸荒唐言,一把辛酸泪",
274-
}
275-
let resp = await this.client.updateRow(
276-
this.database_name,
277-
this.table_name,
278-
pk,
279-
undefined,
280-
update)
332+
let resp = await this.client.update({
333+
database: this.database_name,
334+
table: this.table_name,
335+
primaryKey: {
336+
id: "0001",
337+
},
338+
update: {
339+
bookName: "红楼梦",
340+
author: "曹雪芹",
341+
page: 100,
342+
segment: "满纸荒唐言,一把辛酸泪",
343+
},
344+
});
281345
if (resp.code != 0) {
282346
console.log("fail to update row due to: " + resp.msg)
283347
return resp
@@ -433,7 +497,13 @@ export class MochowTest {
433497
}
434498

435499
public async deleteDataWithPK() {
436-
let resp = await this.client.deleteRow(this.database_name, this.table_name, { "id": "0001" })
500+
let resp = await this.client.delete({
501+
database: this.database_name,
502+
table: this.table_name,
503+
primaryKey: {
504+
"id": "0001"
505+
}
506+
})
437507
if (resp.code != 0) {
438508
console.log("delete data with pk failed")
439509
return resp
@@ -442,7 +512,11 @@ export class MochowTest {
442512
}
443513

444514
public async deleteDataWithFilter() {
445-
let resp = await this.client.deleteRow(this.database_name, this.table_name, undefined, undefined, "id = '0002'")
515+
let resp = await this.client.delete({
516+
database: this.database_name,
517+
table: this.table_name,
518+
filter: "id = '0002'",
519+
})
446520
if (resp.code != 0) {
447521
console.log("delete data with pk failed")
448522
return resp
@@ -560,6 +634,7 @@ export class MochowTest {
560634
await new Promise(f => setTimeout(f, 10000));
561635
await mochowTest.insertRow()
562636
await mochowTest.queryData()
637+
await mochowTest.batchQuery()
563638
await mochowTest.selectData()
564639
await mochowTest.updateData()
565640
await mochowTest.descIndex()

0 commit comments

Comments
 (0)