Skip to content

Commit cfe3230

Browse files
committed
updated unit tests
1 parent 230159d commit cfe3230

File tree

1 file changed

+235
-53
lines changed

1 file changed

+235
-53
lines changed

index_test.go

Lines changed: 235 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,17 @@ func TestCrud(t *testing.T) {
230230
}
231231
}
232232

233+
func approxSame(actual, expected uint64) bool {
234+
modulus := func(a, b uint64) uint64 {
235+
if a > b {
236+
return a - b
237+
}
238+
return b - b
239+
}
240+
241+
return float64(modulus(actual, expected))/float64(expected) < float64(0.25)
242+
}
243+
233244
func TestBytesRead(t *testing.T) {
234245
tmpIndexPath := createTmpIndexPath(t)
235246
defer cleanupTmpIndexPath(t, tmpIndexPath)
@@ -240,12 +251,17 @@ func TestBytesRead(t *testing.T) {
240251
documentMapping := NewDocumentMapping()
241252
indexMapping.AddDocumentMapping("hotel", documentMapping)
242253
indexMapping.StoreDynamic = false
243-
FieldMapping := NewTextFieldMapping()
244-
FieldMapping.Store = false
245-
documentMapping.AddFieldMappingsAt("reviews.content", FieldMapping)
246-
FieldMapping = NewTextFieldMapping()
247-
FieldMapping.Store = true
248-
documentMapping.AddFieldMappingsAt("type", FieldMapping)
254+
indexMapping.DocValuesDynamic = false
255+
contentFieldMapping := NewTextFieldMapping()
256+
contentFieldMapping.Store = false
257+
258+
reviewsMapping := NewDocumentMapping()
259+
reviewsMapping.AddFieldMappingsAt("content", contentFieldMapping)
260+
documentMapping.AddSubDocumentMapping("reviews", reviewsMapping)
261+
262+
typeFieldMapping := NewTextFieldMapping()
263+
typeFieldMapping.Store = false
264+
documentMapping.AddFieldMappingsAt("type", typeFieldMapping)
249265
idx, err := NewUsing(tmpIndexPath, indexMapping, Config.DefaultIndexType, Config.DefaultMemKVStore, nil)
250266
if err != nil {
251267
t.Fatal(err)
@@ -258,144 +274,310 @@ func TestBytesRead(t *testing.T) {
258274
}
259275
}()
260276

261-
file, err := os.Open("sample.txt")
262-
scanner := bufio.NewScanner(file)
263-
batch := idx.NewBatch()
264-
265-
type docStructure map[string]interface{}
266-
267-
for scanner.Scan() {
268-
var doc docStructure
269-
docContent := (scanner.Text())
270-
json.Unmarshal([]byte(docContent), &doc)
271-
err = batch.Index(fmt.Sprintf("%d", doc["id"]), doc)
272-
if err != nil {
273-
t.Fatalf("failed to create batch %v\n", err)
274-
}
277+
batch, err := getBatchFromData(idx, "sample.txt")
278+
if err != nil {
279+
t.Fatalf("failed to form a batch")
275280
}
276-
277281
err = idx.Batch(batch)
278282
if err != nil {
279283
t.Fatalf("failed to index batch %v\n", err)
280284
}
281285
query := NewQueryStringQuery("united")
282286
searchRequest := NewSearchRequestOptions(query, int(10), 0, true)
283287

284-
res, err := idx.Search(searchRequest)
288+
_, err = idx.Search(searchRequest)
285289
if err != nil {
286290
t.Error(err)
287291
}
288292
stats, _ := idx.StatsMap()["index"].(map[string]interface{})
289293
prevBytesRead, _ := stats["num_bytes_read_query_time"].(uint64)
290-
fmt.Printf("bytes read %v\n", prevBytesRead)
294+
if prevBytesRead != 28618 {
295+
t.Fatalf("expected bytes read for query string 28618, got %v",
296+
prevBytesRead)
297+
}
291298

292-
// checking for reusability
293-
res, err = idx.Search(searchRequest)
299+
// subsequent queries on the same field results in lesser amount
300+
// of bytes read because the segment static and dictionary is reused and not
301+
// loaded from mmap'd filed
302+
_, err = idx.Search(searchRequest)
294303
if err != nil {
295304
t.Error(err)
296305
}
297306
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
298307
bytesRead, _ := stats["num_bytes_read_query_time"].(uint64)
299-
fmt.Printf("bytes read %v\n", bytesRead-prevBytesRead)
308+
if bytesRead-prevBytesRead != 23 {
309+
t.Fatalf("expected bytes read for query string 23, got %v",
310+
bytesRead-prevBytesRead)
311+
}
300312
prevBytesRead = bytesRead
301-
fmt.Printf("res hits %v\n", len(res.Hits))
302313

303-
fuzz := NewFuzzyQuery("unitd")
314+
fuzz := NewFuzzyQuery("hotel")
315+
fuzz.FieldVal = "reviews.content"
304316
fuzz.Fuzziness = 2
305317
searchRequest = NewSearchRequest(fuzz)
306-
res, err = idx.Search(searchRequest)
318+
_, err = idx.Search(searchRequest)
307319
if err != nil {
308320
t.Error(err)
309321
}
310-
fmt.Printf("res hits %v\n", len(res.Hits))
311322
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
312323
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
313-
fmt.Printf("bytes read %v %v\n", stats["num_bytes_used_disk_by_root"], bytesRead-prevBytesRead)
324+
if bytesRead-prevBytesRead != 16556 {
325+
t.Fatalf("expected bytes read for fuzzy query is 16176, got %v\n",
326+
bytesRead-prevBytesRead)
327+
}
314328
prevBytesRead = bytesRead
315329

316330
typeFacet := NewFacetRequest("type", 2)
317331
query = NewQueryStringQuery("united")
318332
searchRequest = NewSearchRequestOptions(query, int(0), 0, true)
319333
searchRequest.AddFacet("types", typeFacet)
320-
res, err = idx.Search(searchRequest)
334+
_, err = idx.Search(searchRequest)
321335
if err != nil {
322336
t.Error(err)
323337
}
324338

325339
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
326340
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
327-
fmt.Printf("bytes read %v\n", bytesRead-prevBytesRead)
341+
if !approxSame(bytesRead-prevBytesRead, 259) {
342+
t.Fatalf("expected bytes read for faceted query is 259, got %v",
343+
bytesRead-prevBytesRead)
344+
}
345+
prevBytesRead = bytesRead
346+
347+
_, err = idx.Search(searchRequest)
348+
if err != nil {
349+
t.Error(err)
350+
}
351+
352+
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
353+
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
354+
if !approxSame(bytesRead-prevBytesRead, 259) {
355+
t.Fatalf("expected bytes read for faceted query is 259, got %v",
356+
bytesRead-prevBytesRead)
357+
}
328358
prevBytesRead = bytesRead
329359

330360
min := float64(8000)
331361
max := float64(8010)
332362
numRangeQuery := NewNumericRangeQuery(&min, &max)
333363
numRangeQuery.FieldVal = "id"
334364
searchRequest = NewSearchRequestOptions(numRangeQuery, int(10), 0, true)
335-
res, err = idx.Search(searchRequest)
365+
_, err = idx.Search(searchRequest)
336366
if err != nil {
337367
t.Error(err)
338368
}
339369

340370
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
341371
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
342-
fmt.Printf("bytes read %v\n", bytesRead-prevBytesRead)
372+
if bytesRead-prevBytesRead != 1678 {
373+
t.Fatalf("expected bytes read for numeric range query is 1678, got %v",
374+
bytesRead-prevBytesRead)
375+
}
343376
prevBytesRead = bytesRead
344377

345378
searchRequest = NewSearchRequestOptions(query, int(10), 0, true)
346-
searchRequest.IncludeLocations = true
347-
res, err = idx.Search(searchRequest)
379+
searchRequest.Highlight = &HighlightRequest{}
380+
_, err = idx.Search(searchRequest)
348381
if err != nil {
349382
t.Error(err)
350383
}
351384

352385
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
353386
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
354-
fmt.Printf("bytes read %v\n", bytesRead-prevBytesRead)
387+
if bytesRead-prevBytesRead != 676 {
388+
t.Fatalf("expected bytes read for query with highlighter is 676, got %v",
389+
bytesRead-prevBytesRead)
390+
}
355391
prevBytesRead = bytesRead
356392

357-
searchRequest = NewSearchRequestOptions(query, int(10), 0, true)
358-
searchRequest.Fields = []string{"type"}
359-
res, err = idx.Search(searchRequest)
393+
disQuery := NewDisjunctionQuery(NewMatchQuery("united"), NewMatchQuery("hotel"))
394+
searchRequest = NewSearchRequestOptions(disQuery, int(10), 0, true)
395+
_, err = idx.Search(searchRequest)
396+
if err != nil {
397+
t.Error(err)
398+
}
399+
// expectation is that the bytes read is roughly equal to sum of sub queries in
400+
// the disjunction query plus sum static value
401+
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
402+
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
403+
if bytesRead-prevBytesRead != 77 {
404+
t.Fatalf("expected bytes read for disjunction query is 77, got %v",
405+
bytesRead-prevBytesRead)
406+
}
407+
}
408+
409+
func getBatchFromData(idx Index, fileName string) (*Batch, error) {
410+
file, err := os.Open(fileName)
411+
defer file.Close()
412+
scanner := bufio.NewScanner(file)
413+
batch := idx.NewBatch()
414+
415+
type docStructure map[string]interface{}
416+
417+
for scanner.Scan() {
418+
var doc docStructure
419+
docContent := (scanner.Text())
420+
json.Unmarshal([]byte(docContent), &doc)
421+
err = batch.Index(fmt.Sprintf("%d", doc["id"]), doc)
422+
if err != nil {
423+
return nil, err
424+
}
425+
}
426+
427+
return batch, err
428+
}
429+
430+
func TestBytesReadStored(t *testing.T) {
431+
tmpIndexPath := createTmpIndexPath(t)
432+
defer cleanupTmpIndexPath(t, tmpIndexPath)
433+
434+
indexMapping := NewIndexMapping()
435+
indexMapping.TypeField = "type"
436+
indexMapping.DefaultAnalyzer = "en"
437+
documentMapping := NewDocumentMapping()
438+
indexMapping.AddDocumentMapping("hotel", documentMapping)
439+
440+
indexMapping.DocValuesDynamic = false
441+
indexMapping.StoreDynamic = false
442+
443+
contentFieldMapping := NewTextFieldMapping()
444+
contentFieldMapping.Store = true
445+
contentFieldMapping.IncludeInAll = false
446+
contentFieldMapping.IncludeTermVectors = false
447+
448+
reviewsMapping := NewDocumentMapping()
449+
reviewsMapping.AddFieldMappingsAt("content", contentFieldMapping)
450+
documentMapping.AddSubDocumentMapping("reviews", reviewsMapping)
451+
452+
typeFieldMapping := NewTextFieldMapping()
453+
typeFieldMapping.Store = false
454+
typeFieldMapping.IncludeInAll = false
455+
typeFieldMapping.IncludeTermVectors = false
456+
documentMapping.AddFieldMappingsAt("type", typeFieldMapping)
457+
idx, err := NewUsing(tmpIndexPath, indexMapping, Config.DefaultIndexType, Config.DefaultMemKVStore, nil)
458+
if err != nil {
459+
t.Fatal(err)
460+
}
461+
batch, err := getBatchFromData(idx, "sample.txt")
462+
if err != nil {
463+
t.Fatalf("failed to form a batch %v\n", err)
464+
}
465+
err = idx.Batch(batch)
466+
if err != nil {
467+
t.Fatalf("failed to index batch %v\n", err)
468+
}
469+
query := NewTermQuery("hotel")
470+
query.FieldVal = "reviews.content"
471+
searchRequest := NewSearchRequestOptions(query, int(10), 0, true)
472+
_, err = idx.Search(searchRequest)
360473
if err != nil {
361474
t.Error(err)
362475
}
363476

477+
stats, _ := idx.StatsMap()["index"].(map[string]interface{})
478+
bytesRead, _ := stats["num_bytes_read_query_time"].(uint64)
479+
if bytesRead != 15792 {
480+
t.Fatalf("expected the bytes read stat to be around 15792, got %v", err)
481+
}
482+
prevBytesRead := bytesRead
483+
484+
searchRequest = NewSearchRequestOptions(query, int(10), 0, true)
485+
_, err = idx.Search(searchRequest)
486+
if err != nil {
487+
t.Error(err)
488+
}
364489
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
365490
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
366-
fmt.Printf("bytes read %v\n", bytesRead-prevBytesRead)
491+
if bytesRead-prevBytesRead != 15 {
492+
t.Fatalf("expected the bytes read stat to be around 15, got %v", err)
493+
}
367494
prevBytesRead = bytesRead
368495

369496
searchRequest = NewSearchRequestOptions(query, int(10), 0, true)
370-
searchRequest.Fields = []string{"type"}
371-
res, err = idx.Search(searchRequest)
497+
searchRequest.Fields = []string{"*"}
498+
_, err = idx.Search(searchRequest)
372499
if err != nil {
373500
t.Error(err)
374501
}
375502

376503
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
377504
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
378-
fmt.Printf("bytes read %v\n", bytesRead-prevBytesRead)
379-
prevBytesRead = bytesRead
380505

381-
disQuery := NewDisjunctionQuery(NewMatchQuery("hotel"), NewMatchQuery("united"))
382-
searchRequest = NewSearchRequestOptions(disQuery, int(10), 0, true)
383-
res, err = idx.Search(searchRequest)
506+
if bytesRead-prevBytesRead != 38278 {
507+
t.Fatalf("expected the bytes read stat to be around 38278, got %v", err)
508+
}
509+
idx.Close()
510+
cleanupTmpIndexPath(t, tmpIndexPath)
511+
512+
// same type of querying but on field "type"
513+
contentFieldMapping.Store = false
514+
typeFieldMapping.Store = true
515+
516+
tmpIndexPath1 := createTmpIndexPath(t)
517+
defer cleanupTmpIndexPath(t, tmpIndexPath1)
518+
519+
idx1, err := NewUsing(tmpIndexPath1, indexMapping, Config.DefaultIndexType, Config.DefaultMemKVStore, nil)
520+
if err != nil {
521+
t.Fatal(err)
522+
}
523+
defer func() {
524+
err := idx1.Close()
525+
if err != nil {
526+
t.Fatal(err)
527+
}
528+
}()
529+
530+
batch, err = getBatchFromData(idx1, "sample.txt")
531+
if err != nil {
532+
t.Fatalf("failed to form a batch %v\n", err)
533+
}
534+
err = idx1.Batch(batch)
535+
if err != nil {
536+
t.Fatalf("failed to index batch %v\n", err)
537+
}
538+
539+
query = NewTermQuery("hotel")
540+
query.FieldVal = "type"
541+
searchRequest = NewSearchRequestOptions(query, int(10), 0, true)
542+
_, err = idx1.Search(searchRequest)
384543
if err != nil {
385544
t.Error(err)
386545
}
387546

388-
stats, _ = idx.StatsMap()["index"].(map[string]interface{})
547+
stats, _ = idx1.StatsMap()["index"].(map[string]interface{})
389548
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
390-
fmt.Printf("bytes read dis %v\n", bytesRead-prevBytesRead)
549+
if bytesRead != 167 {
550+
t.Fatalf("expected the bytes read stat to be around 167, got %v", bytesRead-prevBytesRead)
551+
}
552+
prevBytesRead = bytesRead
553+
554+
_, err = idx1.Search(searchRequest)
555+
if err != nil {
556+
t.Error(err)
557+
}
558+
stats, _ = idx1.StatsMap()["index"].(map[string]interface{})
559+
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
560+
if bytesRead-prevBytesRead != 12 {
561+
t.Fatalf("expected the bytes read stat to be around 12, got %v", err)
562+
}
391563
prevBytesRead = bytesRead
392564

393-
t.Errorf("erorr")
565+
searchRequest.Fields = []string{"*"}
566+
_, err = idx1.Search(searchRequest)
567+
if err != nil {
568+
t.Error(err)
569+
}
570+
571+
stats, _ = idx1.StatsMap()["index"].(map[string]interface{})
572+
bytesRead, _ = stats["num_bytes_read_query_time"].(uint64)
573+
574+
if bytesRead-prevBytesRead != 646 {
575+
t.Fatalf("expected the bytes read stat to be around 646, got %v", err)
576+
}
394577
}
395578

396579
func TestIndexCreateNewOverExisting(t *testing.T) {
397580
tmpIndexPath := createTmpIndexPath(t)
398-
defer cleanupTmpIndexPath(t, tmpIndexPath)
399581

400582
index, err := New(tmpIndexPath, NewIndexMapping())
401583
if err != nil {

0 commit comments

Comments
 (0)