-
Notifications
You must be signed in to change notification settings - Fork 1
/
indexing.go
785 lines (728 loc) · 23.1 KB
/
indexing.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
package main
import (
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"math/rand"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/armon/go-radix"
"github.com/elliotchance/orderedmap"
)
// =============================================================================
// Globals
// =============================================================================
// Apps is the array of App Indexes
var Apps map[string]*appIndexes
// =============================================================================
// Structs
// =============================================================================
type indexMap struct {
Field string
index *radix.Tree
TotalDocuments int
}
type appIndexes struct {
Indexes []indexMap
Name string `json:"Name"`
TotalDocuments int `json:"TotalDocuments"`
}
type fuzzyItem struct {
key string
value interface{}
}
type listItem struct {
IndexName string
IndexValues []string
}
type DocumentObject struct {
Score float32 `json:"score"`
Data map[string]interface{} `json:"data"`
DocID uint64 `json:"docID"`
}
type SearchResponse struct {
Items []DocumentObject `json:"items"`
SearchTime time.Duration `json:"searchTimeNS"`
ScoreTime time.Duration `json:"scoreTimeNS"`
LoadTime time.Duration `json:"loadTimeNS"`
}
func initApp(name string) *appIndexes {
appindex := appIndexes{make([]indexMap, 0), name, 0}
if Apps == nil {
Apps = make(map[string]*appIndexes, 0)
}
Apps[name] = &appindex
return &appindex
}
func initIndexMap(indexmap *indexMap, name string) *indexMap {
newMap := indexMap{name, radix.New(), 0}
return &newMap
}
func tokenizeString(input string) []string {
return strings.Fields(input)
}
func lowercaseTokens(tokens []string) []string {
output := make([]string, len(tokens))
for i, token := range tokens {
output[i] = strings.ToLower(token)
}
return output
}
func CheckDocumentsFolder() {
if _, err := os.Stat("./documents"); os.IsNotExist(err) {
os.Mkdir("./documents", os.FileMode(0755))
}
}
func LoadAppsFromDisk() {
start := time.Now()
if _, err := os.Stat("./apps"); os.IsNotExist(err) { // Make sure serialized folder exists
os.Mkdir("./apps", os.FileMode(0755))
}
filepath.Walk("./apps", func(path string, info os.FileInfo, err error) error {
appName := filepath.Base(path)
if info == nil {
log.Printf("Error: ./apps/%s does not exist\n", appName)
return nil
}
if info.IsDir() {
return nil
}
appBytes, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
var newApp appIndexes
json.Unmarshal(appBytes, &newApp)
newApp.Name = appName
Apps[appName] = &newApp
return nil
})
end := time.Now()
log.Printf("### Loaded serialized apps in %v\n", end.Sub(start))
}
func createOrderMapKey(docID uint64, score float32) string {
return fmt.Sprintf("%v#%v", score, docID)
}
func parseOrderMapKey(compoundKey string) (docID uint64, score float32) {
arr := strings.Split(compoundKey, "#")
theScore, _ := strconv.ParseFloat(arr[0], 32)
theDocID, _ := strconv.ParseUint(arr[1], 10, 64)
return theDocID, float32(theScore)
}
func (app *appIndexes) LoadIndexesFromDisk() { // TODO: Change to search folders and load based on app
start := time.Now()
if _, err := os.Stat("./serialized"); os.IsNotExist(err) { // Make sure serialized folder exists
os.Mkdir("./serialized", os.FileMode(0755))
}
if _, err := os.Stat(fmt.Sprintf("./serialized/%s", app.Name)); os.IsNotExist(err) { // Make sure app folder exists
os.Mkdir(fmt.Sprintf("./serialized/%s", app.Name), os.FileMode(0755))
}
filepath.Walk(fmt.Sprintf("./serialized/%s", app.Name), func(path string, info os.FileInfo, err error) error {
if info == nil {
fmt.Printf("Error: ./serialized/%s does not exist\n", app.Name)
return nil
}
if info.IsDir() {
return nil
}
fieldName := filepath.Base(path)
var indexInd int
// Find which index is field
for indx, indexx := range app.Indexes {
if indexx.Field == fieldName {
indexInd = indx
break
}
}
decodeFile, err := os.Open(path)
defer decodeFile.Close()
d := gob.NewDecoder(decodeFile)
decoded := make(map[string]map[string]int)
err = d.Decode(&decoded)
converted := make(map[string]interface{})
for key, value := range decoded {
newOrdMap := convertNormalToOrderedMap(&value)
converted[key] = &newOrdMap
}
app.Indexes[indexInd].index = radix.NewFromMap(converted)
return nil
})
end := time.Now()
log.Printf("### Loaded serialized indexes in %v\n", end.Sub(start))
}
func fetchDocument(docID uint64) string {
dat, _ := ioutil.ReadFile(fmt.Sprintf("./documents/%v", docID))
return string(dat)
}
func loadDocuments(docObjs *SearchResponse) {
for idx, docObj := range docObjs.Items {
docString := fetchDocument(docObj.DocID)
docObjs.Items[idx].Data, _ = parseArbJSON(docString)
}
}
// ListApps lists the names of current apps
func ListApps() []string {
appArr := make([]string, 0)
for k, _ := range Apps {
appArr = append(appArr, k)
}
return appArr
}
func GetApp(appName string) (*appIndexes, error) {
app := Apps[appName]
if app == nil {
return nil, errors.New("App does not exist!")
} else {
return app, nil
}
}
// Dan's method uses less memory than https://play.golang.org/p/LjOrp4k6lqf because we aren't appending to an array (dan is faster on load but slower and serial)
func convertOrderedToNormalMap(ordMap *orderedmap.OrderedMap) map[string]int {
output := make(map[string]int)
for el := ordMap.Front(); el != nil; el = el.Next() {
output[el.Key.(string)] = el.Value.(int)
}
return output
}
func convertNormalToOrderedMap(normMap *map[string]int) orderedmap.OrderedMap {
newMap := orderedmap.NewOrderedMap()
for key, value := range *normMap {
newMap.Set(key, value)
}
return *newMap
}
// ########################################################################
// ######################## appIndexes functions ##########################
// ########################################################################
func (appIndex *appIndexes) listIndexes() []struct {
TotalDocuments int
Name string
} {
var output []struct {
TotalDocuments int
Name string
}
for _, i := range appIndex.Indexes {
output = append(output, struct {
TotalDocuments int
Name string
}{TotalDocuments: i.TotalDocuments, Name: i.Field})
}
return output
}
// addIndexMap creates a new index map (field) to be indexes
func (appindex *appIndexes) addIndexMap(name string) *indexMap {
newIndexMap := indexMap{name, radix.New(), 0}
oldLen := len(appindex.Indexes)
appindex.Indexes = append(appindex.Indexes, newIndexMap)
return &appindex.Indexes[oldLen]
}
func (appindex *appIndexes) addIndexFromDisk(parsed map[string]interface{}, filename string) (documentID uint64) {
// Format the input
rand.Seed(time.Now().UnixNano())
id, _ := strconv.ParseUint(filename, 10, 64)
var totalDocLen int
for _, v := range parsed {
totalDocLen += len(strings.FieldsFunc(v.(string), func(r rune) bool {
return r == ' ' || r == '"'
}))
}
for k, v := range parsed {
// Don't index ID
if strings.ToLower(k) == "docid" {
continue
}
// Find if indexMap already exists
var indexMapPointer *indexMap = nil
for i := 0; i < len(appindex.Indexes); i++ {
if k == appindex.Indexes[i].Field {
indexMapPointer = &appindex.Indexes[i]
break
}
}
if indexMapPointer == nil { // Create indexMap
indexMapPointer = appindex.addIndexMap(k)
}
// Add index to indexMap
indexMapPointer.addIndex(id, fmt.Sprintf("%v", v), totalDocLen)
}
// Write indexes document to disk
fmt.Sprintf("Writing out to: %s\n", fmt.Sprintf("./documents/%v", id))
sendback, _ := stringIndex(parsed)
ioutil.WriteFile(fmt.Sprintf("./documents/%v", id), []byte(sendback), os.FileMode(0660))
return id
}
func (appindex *appIndexes) addIndex(parsed map[string]interface{}, fromGossip bool) (documentID uint64) {
// log.Println("### Adding index...", fromGossip)
// Format the input
rand.Seed(time.Now().UnixNano())
var id uint64
if parsed["docID"] != nil {
if fromGossip { // docID is uint64 already
id = parsed["docID"].(uint64)
} else { // docID is json.Number
pre, _ := parsed["docID"].(json.Number).Int64()
id = uint64(pre)
}
} else {
id = rand.Uint64()
}
// log.Println("### ID:", id)
var totalDocLen int
for k, v := range parsed {
if k == "docID" { // Ignore docID
continue
}
totalDocLen += len(strings.FieldsFunc(v.(string), func(r rune) bool {
return r == ' ' || r == '"'
}))
}
for k, v := range parsed {
// Don't index ID
if strings.ToLower(k) == "docid" {
continue
}
// Find if indexMap already exists
var indexMapPointer *indexMap
for i := 0; i < len(appindex.Indexes); i++ {
if k == appindex.Indexes[i].Field {
indexMapPointer = &appindex.Indexes[i]
break
}
}
if indexMapPointer == nil { // Create indexMap
indexMapPointer = appindex.addIndexMap(k)
}
// Add index to indexMap
indexMapPointer.addIndex(id, fmt.Sprintf("%v", v), totalDocLen)
}
// Remove docID field
delete(parsed, "docID")
// Gossip addIndex
appindex.TotalDocuments++ // Increase document count
// Write indexes document to disk
sendback, _ := stringIndex(parsed)
ioutil.WriteFile(fmt.Sprintf("./documents/%v", id), []byte(sendback), os.FileMode(0660))
gossipStruct := GossipMessageTypeAddIndex{
DocID: id,
Fields: parsed,
App: appindex.Name,
}
gossipData, err := json.Marshal(gossipStruct)
if err != nil {
logger.Error("Error marshalling json data for adding index gossip")
logger.Error(err)
return
}
if !fromGossip {
// newID := rand.Uint64()
// BroadcastGossipMessage(gossipData, []string{}, "addIndex", 6, newID)
TellClusterAddIndex(gossipData)
}
return id
}
func addIndexFromGossip(docID uint64, appName string, fields map[string]interface{}) (documentID uint64) {
// TODO: check if app exists
// TODO: create app if not exists
fields["docID"] = docID
realID := Apps[appName].addIndex(fields, true)
return realID
}
func (appindex *appIndexes) search(input string, fields []string, bw bool) (documentIDs []uint64, response SearchResponse) {
output := make(map[uint64][]float32, 0) // docID: [score, docLen]
var avgDocLen float32
// Tokenize input
start := time.Now()
searchTokens := lowercaseTokens(tokenizeString(input))
for _, token := range searchTokens { // Initial scoring pass, covers TF weighting and IDF weighting
// Check fields
if len(fields) == 0 { // check all
// log.Println("### No fields given, searching all fields...")
for _, indexmap := range appindex.Indexes {
// log.Println("### Searching index:", indexmap.field, "for", token)
if bw {
searchItems := indexmap.beginsWithSearch(token)
for _, searchItem := range searchItems {
if searchItem != nil {
// FIXME: This is a hacky solution for getting number of docs with the term in it
numDocsWithTerm := 0
for el := searchItem.Front(); el != nil || numDocsWithTerm >= 100; el = el.Next() {
numDocsWithTerm++
}
// Only first 100 items in orderedmap
iterCount := 0
for el := searchItem.Front(); el != nil || iterCount >= 100; el = el.Next() {
docID, tfWeighting := parseOrderMapKey(el.Key.(string))
idfWeighting := math.Log(float64(1+indexmap.TotalDocuments) / float64(numDocsWithTerm))
fieldLen := float32(el.Value.(int))
newVal := make([]float32, 2)
if val, ok := output[docID]; ok { // Exists
prevScore := val[0]
prevLen := val[1]
newVal[0] = prevScore + (tfWeighting * float32(idfWeighting))
newVal[1] = prevLen + fieldLen
avgDocLen += fieldLen
output[docID] = newVal
} else {
newVal[0] = tfWeighting
newVal[1] = fieldLen
avgDocLen += fieldLen
output[docID] = newVal
}
iterCount++
}
}
}
} else {
searchItems := indexmap.search(token)
if searchItems != nil {
// FIXME: This is a hacky solution for getting number of docs with the term in it
numDocsWithTerm := 0
for el := searchItems.Front(); el != nil || numDocsWithTerm >= 100; el = el.Next() {
numDocsWithTerm++
}
// Only first 100 items in orderedmap
iterCount := 0
for el := searchItems.Front(); el != nil || iterCount >= 100; el = el.Next() {
docID, tfWeighting := parseOrderMapKey(el.Key.(string))
idfWeighting := math.Log(float64(1+indexmap.TotalDocuments) / float64(numDocsWithTerm))
fieldLen := float32(el.Value.(int))
newVal := make([]float32, 2)
if val, ok := output[docID]; ok { // Exists
prevScore := val[0]
prevLen := val[1]
newVal[0] = prevScore + (tfWeighting * float32(idfWeighting))
newVal[1] = prevLen
output[docID] = newVal
} else {
newVal[0] = tfWeighting
newVal[1] = fieldLen
avgDocLen += fieldLen
output[docID] = newVal
}
iterCount++
}
}
}
}
} else { // check given fields
for _, field := range fields {
// log.Println("### Searching index:", field, "for", token)
searchItems := appindex.searchByField(token, field, bw)
for _, searchItem := range searchItems {
if searchItem != nil {
// FIXME: This is a hacky solution for getting number of docs with the term in it
numDocsWithTerm := 0
for el := searchItem.Front(); el != nil || numDocsWithTerm >= 100; el = el.Next() {
numDocsWithTerm++
}
// Only first 100 items in orderedmap
iterCount := 0
for el := searchItem.Front(); el != nil || iterCount >= 100; el = el.Next() {
docID, tfWeighting := parseOrderMapKey(el.Key.(string))
// FIXME: Currently does not cover the IDF weighting
fieldLen := float32(el.Value.(int))
newVal := make([]float32, 2)
if val, ok := output[docID]; ok { // Exists
prevScore := val[0]
prevLen := val[1]
newVal[0] = prevScore + tfWeighting
newVal[1] = prevLen + float32(fieldLen)
avgDocLen += float32(fieldLen)
output[docID] = newVal
} else {
newVal[0] = tfWeighting
newVal[1] = float32(fieldLen)
avgDocLen += float32(fieldLen)
output[docID] = newVal
}
iterCount++
}
}
}
}
}
}
responseObj := SearchResponse{
Items: make([]DocumentObject, 0),
}
end := time.Now()
diff := end.Sub(start)
responseObj.SearchTime = diff
// -------------------
// Scoring and Sorting
// -------------------
start = time.Now()
// Calculate avg doc len
avgDocLen = avgDocLen / float32(len(output)) // Get average
// Now we have docID: [score, docLen]
for docID, scoreLen := range output {
termsInQueryLen := len(strings.Fields(input))
finalDocScore := (scoreLen[0] / ((1 - 0.1) * 0.1 * scoreLen[1] / (avgDocLen))) * float32(termsInQueryLen) // Length normalization and IDF weighting
responseObj.Items = append(responseObj.Items, DocumentObject{
Data: nil,
Score: finalDocScore,
DocID: docID,
})
}
sort.Slice(responseObj.Items, func(i int, j int) bool {
return responseObj.Items[i].Score > responseObj.Items[j].Score
})
// TODO: Only take first 100 documents for now
if len(responseObj.Items) > 100 {
responseObj.Items = responseObj.Items[:100]
}
end = time.Now()
responseObj.ScoreTime = end.Sub(start)
// -----------------
// Loading Documents
// -----------------
start = time.Now()
loadDocuments(&responseObj)
end = time.Now()
responseObj.LoadTime = end.Sub(start)
yeye := make([]uint64, 0) // FIXME: This is a temp solution obviously
return yeye, responseObj
}
func (appindex *appIndexes) searchByField(input string, field string, bw bool) (documents []*orderedmap.OrderedMap) {
// Check if field exists
output := make([]*orderedmap.OrderedMap, 0)
for _, indexmap := range appindex.Indexes {
if indexmap.Field == field {
if bw {
searchItems := indexmap.beginsWithSearch(input)
output = append(output, searchItems...)
} else {
searchItems := indexmap.search(input)
output = append(output, searchItems)
}
break
}
}
return output
}
func (appindex *appIndexes) SerializeApp() {
log.Printf("### Serializing App: %s\n", appindex.Name)
if _, err := os.Stat("./apps"); os.IsNotExist(err) { // Make sure apps folder exists
os.Mkdir("./apps", os.FileMode(0755))
}
// Wipe tree since serializing tree elsewhere
// appindex.Indexes = nil
serializedApp, err := json.Marshal(appindex)
if err != nil {
panic(err)
}
ioutil.WriteFile(fmt.Sprintf("./apps/%s", appindex.Name), serializedApp, os.FileMode(0755))
log.Printf("### Successfully Serialized App %s!\n", appindex.Name)
}
func (appindex *appIndexes) SerializeIndex() {
// Check folders and such
log.Printf("### Serializing %s Indexes...\n", appindex.Name)
if _, err := os.Stat("./serialized"); os.IsNotExist(err) { // Make sure serialized folder exists
os.Mkdir("./serialized", os.FileMode(0755))
}
if _, err := os.Stat(fmt.Sprintf("./serialized/%s", appindex.Name)); os.IsNotExist(err) { // Make sure app folder exists
os.Mkdir(fmt.Sprintf("./serialized/%s", appindex.Name), os.FileMode(0755))
}
// Do the serialization
for _, i := range appindex.Indexes {
serializedTree := i.index.ToMap()
encodeFile, err := os.Create(fmt.Sprintf("./serialized/%s/%s", appindex.Name, i.Field))
if err != nil {
panic(err)
}
e := gob.NewEncoder(encodeFile)
converted := make(map[string]map[string]int)
for key, value := range serializedTree {
var restoreMap map[string]int
restoreMap = convertOrderedToNormalMap(value.(*orderedmap.OrderedMap))
converted[key] = restoreMap
}
err = e.Encode(converted)
encodeFile.Close()
}
log.Printf("### Successfully Serialized %s Indexes!\n", appindex.Name)
}
func (appindex *appIndexes) deleteIndex(docID uint64) error {
var err error = nil
// Find document on disk
docPath := fmt.Sprintf("./documents/%v", docID)
if _, err := os.Stat(docPath); os.IsNotExist(err) {
log.Printf("DocID: %v does not exist\n", docID)
return err
}
docData, err := ioutil.ReadFile(docPath)
docString := string(docData)
docJSON, err := parseArbJSON(docString)
if err != nil {
return err
}
for k, v := range docJSON {
// Don't index ID
input := fmt.Sprintf("%v", v)
if k == "docID" {
continue
}
// Find the field index
for i := 0; i < len(appindex.Indexes); i++ {
if k == appindex.Indexes[i].Field {
indexmap := appindex.Indexes[i]
// Tokenize field
for _, token := range lowercaseTokens(tokenizeString(input)) {
tokenMap, _ := indexmap.index.Get(token)
if tokenMap == nil { // somehow not indexed
log.Printf("### Error: field not indexes from disk document (deleteIndex)\n")
continue
} else { // update node
docMap := tokenMap.(*orderedmap.OrderedMap)
documentTermScore := calculateTokenScoreByField(input, token, indexmap.TotalDocuments, docMap.Len())
deleted := docMap.Delete(createOrderMapKey(docID, documentTermScore))
if deleted != true {
log.Printf("### Error: Document entry not deleted for token %s\n", token)
}
if docMap.Len() == 0 {
_, deleted := indexmap.index.Delete(token) // this should delete the node if it is empty
if !deleted {
log.Printf("Node was not deleted\n")
}
} else {
_, updated := indexmap.index.Insert(token, docMap)
if !updated {
log.Printf("### SOMEHOW DIDN'T UPDATE WHEN DELETING INDEX ###\n")
continue
}
}
}
}
indexmap.TotalDocuments--
break
}
}
}
appindex.TotalDocuments--
// Delete document on disk
err = os.Remove(docPath)
if err != nil {
return err
}
return nil
}
func (appindex *appIndexes) updateIndex(parsed map[string]interface{}) (errrr error, created bool) {
var err error = nil
// Validate docID
var docID uint64
pre := parsed["docID"].(json.Number)
docID, err = strconv.ParseUint(pre.String(), 10, 64)
if err != nil {
return err, false
}
err = appindex.deleteIndex(docID)
if os.IsNotExist(err) {
appindex.addIndex(parsed, false)
err = nil
return err, true
} else if err == nil {
appindex.addIndex(parsed, false)
}
return err, false
}
func FuzzySearch(key string, t *radix.Tree) []fuzzyItem {
output := make([]fuzzyItem, 0)
split := strings.Split(key, "")
t.WalkPrefix(split[0], func(k string, value interface{}) bool {
// log.Println("### Checking", k)
includesAll := true
for _, char := range split {
// log.Println("### DOES", k, "include", char)
if !strings.Contains(k, char) {
// log.Println("NOPE")
includesAll = false
break
}
}
if includesAll {
output = append(output, fuzzyItem{k, value})
}
return false
})
return output
}
// ########################################################################
// ######################### indexMap functions ###########################
// ########################################################################
func calculateTokenScoreByField(fieldValue string, tokenValue string, totalDocuments int, orderMapLen int) (tokenScore float32) {
// BEGIN Precision word match
tokenPrecisionFreq := 0
docStringWord := strings.FieldsFunc(fieldValue, func(r rune) bool {
return r == ' ' || r == '"'
})
for _, word := range docStringWord {
if strings.ToLower(word) == tokenValue {
tokenPrecisionFreq++
}
}
termFreqWeight := 1 + (math.Log(1 + math.Log(1+float64(tokenPrecisionFreq))))
// END Precision word match
return float32(termFreqWeight)
}
func (indexmap *indexMap) addIndex(id uint64, value string, docLen int) {
CheckDocumentsFolder()
// Tokenize
for _, token := range lowercaseTokens(tokenizeString(value)) {
// log.Println("### INDEXING:", token)
// Check if index already exists
prenode, _ := indexmap.index.Get(token)
var ids *orderedmap.OrderedMap
if prenode == nil { // create new node
ids = orderedmap.NewOrderedMap()
// Calculate token score here
documentTermScore := calculateTokenScoreByField(value, token, indexmap.TotalDocuments, ids.Len())
ids.Set(createOrderMapKey(id, documentTermScore), docLen)
_, updated := indexmap.index.Insert(token, ids)
if updated {
fmt.Errorf("### SOMEHOW UPDATED WHEN INSERTING NEW ###\n")
}
// log.Printf("### ADDED %v WITH IDS: %v ###\n", token, ids)
} else { // update node
node := prenode.(*orderedmap.OrderedMap)
ids = node
documentTermScore := calculateTokenScoreByField(value, token, indexmap.TotalDocuments, ids.Len())
node.Set(createOrderMapKey(id, documentTermScore), docLen)
_, updated := indexmap.index.Insert(token, node)
if !updated {
fmt.Errorf("### SOMEHOW DIDN'T UPDATE WHEN UPDATING INDEX ###\n")
}
// log.Printf("### UPDATED %v WITH IDS: %v ###\n", token, ids)
}
}
indexmap.TotalDocuments++
}
func (indexmap *indexMap) search(input string) (documents *orderedmap.OrderedMap) {
search, _ := indexmap.index.Get(input)
if search == nil {
return nil
}
node := search.(*orderedmap.OrderedMap)
return node
}
func (indexmap *indexMap) beginsWithSearch(input string) (documents []*orderedmap.OrderedMap) {
output := make([]*orderedmap.OrderedMap, 0)
count := 0
indexmap.index.WalkPrefix(input, func(key string, value interface{}) bool {
if count >= 100 {
return true
}
node := value.(*orderedmap.OrderedMap)
output = append(output, node)
return false
})
return output
}