-
Notifications
You must be signed in to change notification settings - Fork 1
/
tantivy_test.go
581 lines (463 loc) · 13.7 KB
/
tantivy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package tantivy_go_test
import (
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anyproto/tantivy-go"
)
const NameBody = "body"
const NameId = "id"
const NameTitle = "title"
const NameBodyZh = "bodyZh"
const NameTitleZh = "titleZh"
const limit = 40
const minGram = 2
type DocSample struct {
Title string
Id string
Body string
Highlights []Highlight
}
type Fragment struct {
R [][2]int `json:"r"`
T string `json:"t"`
}
type Highlight struct {
FieldName string `json:"field_name"`
Fragment Fragment `json:"fragment"`
}
func Test(t *testing.T) {
t.Run("docs search and remove - when by raw Id", func(t *testing.T) {
schema, tc := fx(t, limit, minGram, false)
defer tc.Free()
doc, err := addDoc(t, "Example Title", "Example body doing.", "1", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc)
require.NoError(t, err)
sCtx := tantivy_go.NewSearchContextBuilder().
SetQuery("body").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameBody).
Build()
result, err := tc.Search(sCtx)
require.NoError(t, err)
size, err := result.GetSize()
require.Equal(t, 1, int(size))
results, err := tantivy_go.GetSearchResults(result, schema, func(jsonStr string) (interface{}, error) {
var doc DocSample
return doc, json.Unmarshal([]byte(jsonStr), &doc)
}, NameId, NameTitle, NameBody)
require.NoError(t, err)
require.Equal(t, len(results), int(size))
require.NoError(t, err)
for next := range results {
model := results[next].(DocSample)
require.Equal(t, DocSample{
"Example Title",
"1",
"Example body doing.",
[]Highlight{
{
NameBody,
Fragment{
[][2]int{{8, 12}},
"Example body doing",
},
}},
},
model)
}
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(1), docs)
err = tc.DeleteDocuments(NameId, "1")
require.NoError(t, err)
docs, err = tc.NumDocs()
require.Equal(t, uint64(0), docs)
})
t.Run("docs remove - when by body token", func(t *testing.T) {
_, tc := fx(t, limit, minGram, false)
defer tc.Free()
// Tokens are strang, text, bodi
doc, err := addDoc(t, "Example title", "Strange/text/body", "1", tc)
doc2, err := addDoc(t, "Example title", "Strange another something", "2", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc, doc2)
require.NoError(t, err)
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(2), docs)
err = tc.DeleteDocuments(NameBody, "strang")
require.NoError(t, err)
docs, err = tc.NumDocs()
require.Equal(t, uint64(0), docs)
})
t.Run("docs remove - when by wrong body token", func(t *testing.T) {
_, tc := fx(t, limit, minGram, false)
defer tc.Free()
// Tokens are strang, text, bodi
doc, err := addDoc(t, "Example title", "Strange/text/body", "1", tc)
doc2, err := addDoc(t, "Example title", "Strange another something", "2", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc, doc2)
require.NoError(t, err)
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(2), docs)
err = tc.DeleteDocuments(NameBody, "strange")
require.NoError(t, err)
docs, err = tc.NumDocs()
require.Equal(t, uint64(2), docs)
})
t.Run("docs remove - when by proper token and wrong field length", func(t *testing.T) {
_, tc := fx(t, 1, minGram, false)
defer tc.Free()
doc, err := addDoc(t, "Example title", "Body", "12", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc)
require.NoError(t, err)
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(1), docs)
// token limit is 1
err = tc.DeleteDocuments(NameBody, "12")
require.NoError(t, err)
docs, err = tc.NumDocs()
require.Equal(t, uint64(1), docs)
})
t.Run("docs search and remove - when thai", func(t *testing.T) {
_, tc := fx(t, limit, 1, false)
defer tc.Free()
doc, err := addDoc(t, "ตัวอย่ง", "body", "1", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc)
require.NoError(t, err)
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(1), docs)
sCtx := tantivy_go.NewSearchContextBuilder().
SetQuery("ย").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameTitle).
Build()
result, err := tc.Search(sCtx)
require.NoError(t, err)
size, err := result.GetSize()
defer result.Free()
require.Equal(t, 0, int(size))
sCtx2 := tantivy_go.NewSearchContextBuilder().
SetQuery("ย่").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameTitle).
Build()
result2, err := tc.Search(sCtx2)
require.NoError(t, err)
size2, err := result2.GetSize()
defer result2.Free()
require.Equal(t, 1, int(size2))
err = tc.DeleteDocuments(NameTitle, "ต")
require.NoError(t, err)
docs, err = tc.NumDocs()
require.Equal(t, uint64(1), docs)
err = tc.DeleteDocuments(NameTitle, "ตั")
require.NoError(t, err)
docs, err = tc.NumDocs()
require.Equal(t, uint64(0), docs)
})
t.Run("docs search - when ascii folding", func(t *testing.T) {
_, tc := fx(t, limit, 1, false)
defer tc.Free()
doc, err := addDoc(t, "Idées fête", "mères straße", "1", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc)
require.NoError(t, err)
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(1), docs)
sCtx := tantivy_go.NewSearchContextBuilder().
SetQuery("Idées fête").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameTitle).
Build()
result, err := tc.Search(sCtx)
require.NoError(t, err)
size, err := result.GetSize()
defer result.Free()
require.Equal(t, 1, int(size))
sCtx2 := tantivy_go.NewSearchContextBuilder().
SetQuery("idees fete").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameTitle).
Build()
result2, err := tc.Search(sCtx2)
require.NoError(t, err)
size2, err := result2.GetSize()
defer result2.Free()
require.Equal(t, 1, int(size2))
sCtx3 := tantivy_go.NewSearchContextBuilder().
SetQuery("straße").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameBody).
Build()
result3, err := tc.Search(sCtx3)
require.NoError(t, err)
size3, err := result3.GetSize()
defer result3.Free()
require.Equal(t, 1, int(size3))
sCtx4 := tantivy_go.NewSearchContextBuilder().
SetQuery("strasse").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameBody).
Build()
result4, err := tc.Search(sCtx4)
require.NoError(t, err)
size4, err := result4.GetSize()
defer result4.Free()
require.Equal(t, 1, int(size4))
})
t.Run("docs search and remove - when fast", func(t *testing.T) {
_, tc := fx(t, limit, minGram, false)
defer tc.Free()
doc, err := addDoc(t, "some", "body", "1", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc)
require.NoError(t, err)
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(1), docs)
sCtx := tantivy_go.NewSearchContextBuilder().
SetQuery("1").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameId).
Build()
result, err := tc.Search(sCtx)
require.NoError(t, err)
size, err := result.GetSize()
defer result.Free()
require.Equal(t, 1, int(size))
err = tc.DeleteDocuments(NameId, "1")
docs, err = tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(0), docs)
})
t.Run("docs search and remove - when title", func(t *testing.T) {
schema, tc := fx(t, limit, minGram, false)
defer tc.Free()
doc, err := addDoc(t, "Create Body", "Example title content.", "1", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc)
require.NoError(t, err)
sCtx := tantivy_go.NewSearchContextBuilder().
SetQuery("create").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameTitle).
Build()
result, err := tc.Search(sCtx)
require.NoError(t, err)
size, err := result.GetSize()
require.Equal(t, 1, int(size))
results, err := tantivy_go.GetSearchResults(result, schema, func(jsonStr string) (interface{}, error) {
var doc DocSample
return doc, json.Unmarshal([]byte(jsonStr), &doc)
}, NameId, NameTitle, NameBody)
require.NoError(t, err)
require.Equal(t, len(results), int(size))
require.NoError(t, err)
for next := range results {
model := results[next].(DocSample)
require.Equal(t, DocSample{
"Create Body",
"1",
"Example title content.",
[]Highlight{
{
NameTitle,
Fragment{
[][2]int{{0, 2}, {0, 3}, {0, 4}},
"Crea",
},
}},
},
model)
}
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(1), docs)
err = tc.DeleteDocuments(NameId, "1")
require.NoError(t, err)
docs, err = tc.NumDocs()
require.Equal(t, uint64(0), docs)
})
t.Run("docs search - when jieba", func(t *testing.T) {
_, tc := fx(t, limit, 1, false)
defer tc.Free()
doc, err := addDoc(t, "", "张华考上了北京大学;李萍进了中等技术学校;我在百货公司当售货员:我们都有光明的前途", "1", tc)
require.NoError(t, err)
doc2, err := addDoc(t, "张华考上了北京大学;李萍进了中等技术学校;我在百货公司当售货员:我们都有光明的前途", "", "2", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc, doc2)
require.NoError(t, err)
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(2), docs)
sCtx := tantivy_go.NewSearchContextBuilder().
SetQuery("售货员").
SetDocsLimit(100).
SetWithHighlights(true).
AddFieldDefaultWeight(NameBodyZh).
AddFieldDefaultWeight(NameTitleZh).
Build()
result, err := tc.Search(sCtx)
require.NoError(t, err)
size, err := result.GetSize()
defer result.Free()
require.Equal(t, 2, int(size))
})
t.Run("docs search - when weights apply", func(t *testing.T) {
schema, tc := fx(t, limit, 1, false)
defer tc.Free()
doc, err := addDoc(t, "an apple", "", "id1", tc)
require.NoError(t, err)
doc2, err := addDoc(t, "", "an apple", "id2", tc)
require.NoError(t, err)
err = tc.AddAndConsumeDocuments(doc, doc2)
require.NoError(t, err)
docs, err := tc.NumDocs()
require.NoError(t, err)
require.Equal(t, uint64(2), docs)
sCtx := tantivy_go.NewSearchContextBuilder().
SetQuery("apple").
SetDocsLimit(100).
SetWithHighlights(false).
AddField(NameTitle, 1.0).
AddField(NameBody, 1.0).
Build()
result, err := tc.Search(sCtx)
require.NoError(t, err)
size, err := result.GetSize()
defer result.Free()
require.Equal(t, 2, int(size))
resDoc, err := result.Get(0)
require.NoError(t, err)
jsonStr, err := resDoc.ToJson(schema, NameId)
require.NoError(t, err)
require.JSONEq(t, `{"highlights":[],"id":"id1","score":1.9676434993743896}`, jsonStr)
sCtx2 := tantivy_go.NewSearchContextBuilder().
SetQuery("apple").
SetDocsLimit(100).
SetWithHighlights(false).
AddField(NameTitle, 1.0).
AddField(NameBody, 10.0).
Build()
result2, err := tc.Search(sCtx2)
require.NoError(t, err)
size2, err := result2.GetSize()
defer result2.Free()
require.Equal(t, 2, int(size2))
resDoc2, err := result2.Get(0)
require.NoError(t, err)
jsonStr2, err := resDoc2.ToJson(schema, NameId)
require.NoError(t, err)
require.JSONEq(t, `{"highlights":[],"id":"id2","score":4.919108867645264}`, jsonStr2)
})
}
func addDoc(
t *testing.T,
title string,
body string,
id string,
tc *tantivy_go.TantivyContext,
) (*tantivy_go.Document, error) {
doc := tantivy_go.NewDocument()
err := doc.AddField(NameTitle, title, tc)
require.NoError(t, err)
err = doc.AddField(NameTitleZh, title, tc)
require.NoError(t, err)
err = doc.AddField(NameId, id, tc)
require.NoError(t, err)
err = doc.AddField(NameBody, body, tc)
require.NoError(t, err)
err = doc.AddField(NameBodyZh, body, tc)
return doc, err
}
func fx(
t *testing.T,
limit uintptr,
minGram uintptr,
isFastId bool,
) (*tantivy_go.Schema, *tantivy_go.TantivyContext) {
err := tantivy_go.LibInit(true, "debug")
assert.NoError(t, err)
builder, err := tantivy_go.NewSchemaBuilder()
require.NoError(t, err)
err = builder.AddTextField(
NameTitle,
true,
true,
false,
tantivy_go.IndexRecordOptionWithFreqsAndPositions,
tantivy_go.TokenizerEdgeNgram,
)
require.NoError(t, err)
err = builder.AddTextField(
NameTitleZh,
true,
true,
false,
tantivy_go.IndexRecordOptionWithFreqsAndPositions,
tantivy_go.TokenizerJieba,
)
require.NoError(t, err)
err = builder.AddTextField(
NameId,
true,
false,
isFastId,
tantivy_go.IndexRecordOptionBasic,
tantivy_go.TokenizerRaw,
)
require.NoError(t, err)
err = builder.AddTextField(
NameBody,
true,
true,
false,
tantivy_go.IndexRecordOptionWithFreqsAndPositions,
tantivy_go.TokenizerSimple,
)
require.NoError(t, err)
err = builder.AddTextField(
NameBodyZh,
true,
true,
false,
tantivy_go.IndexRecordOptionWithFreqsAndPositions,
tantivy_go.TokenizerJieba,
)
require.NoError(t, err)
schema, err := builder.BuildSchema()
require.NoError(t, err)
_ = os.RemoveAll("index_dir")
tc, err := tantivy_go.NewTantivyContextWithSchema("index_dir", schema)
require.NoError(t, err)
err = tc.RegisterTextAnalyzerSimple(tantivy_go.TokenizerSimple, limit, tantivy_go.English)
require.NoError(t, err)
err = tc.RegisterTextAnalyzerJieba(tantivy_go.TokenizerJieba, limit)
require.NoError(t, err)
err = tc.RegisterTextAnalyzerEdgeNgram(tantivy_go.TokenizerEdgeNgram, minGram, 4, 100)
require.NoError(t, err)
err = tc.RegisterTextAnalyzerRaw(tantivy_go.TokenizerRaw)
require.NoError(t, err)
return schema, tc
}