From e973c9724829d962ff9e233ef16f489296f9fe3d Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 28 Jul 2026 00:30:55 +0200 Subject: added Posting type to the reverse index struct which will be later used for scoring results --- index/index.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'index/index.go') diff --git a/index/index.go b/index/index.go index 7456213..53f9fca 100644 --- a/index/index.go +++ b/index/index.go @@ -2,31 +2,39 @@ import "sync" +type Posting struct { + DocId int + Count int +} type InvertedIndex struct { Mu sync.RWMutex - Data map[string][]int + Data map[string][]Posting DocNames map[int]string } func New() *InvertedIndex { return &InvertedIndex{ - Data: make(map[string][]int), + Data: make(map[string][]Posting), DocNames: make(map[int]string), } } func (idx *InvertedIndex) Add(docID int, title string, tokens []string) { + + tokenCounts := make(map[string]int) + for _, token := range tokens { + tokenCounts[token]++ + } + idx.Mu.Lock() defer idx.Mu.Unlock() idx.DocNames[docID] = title - seen := make(map[string]struct{}) - - for _, token := range tokens { - if _, exists := seen[token]; !exists { - idx.Data[token] = append(idx.Data[token], docID) - seen[token] = struct{}{} - } + for token, count := range tokenCounts { + idx.Data[token] = append(idx.Data[token], Posting{ + DocId: docID, + Count: count, + }) } } -- cgit v1.2.3