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 --- .idea/vcs.xml | 6 ++++++ index/index.go | 26 +++++++++++++++++--------- search/search.go | 10 +++++----- 3 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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, + }) } } diff --git a/search/search.go b/search/search.go index ced94b7..f0b1cce 100644 --- a/search/search.go +++ b/search/search.go @@ -21,7 +21,7 @@ func Search(idx *index.InvertedIndex, query string) []string { return nil } - intersection := make([]int, len(baseIDs)) + intersection := make([]index.Posting, len(baseIDs)) copy(intersection, baseIDs) for i := 1; i < len(tokenizedQuery); i++ { @@ -34,12 +34,12 @@ func Search(idx *index.InvertedIndex, query string) []string { lookup := make(map[int]struct{}) for _, id := range nextIDs { - lookup[id] = struct{}{} + lookup[id.DocId] = struct{}{} } - var filtered []int + var filtered []index.Posting for _, id := range intersection { - if _, found := lookup[id]; found { + if _, found := lookup[id.DocId]; found { filtered = append(filtered, id) } } @@ -49,7 +49,7 @@ func Search(idx *index.InvertedIndex, query string) []string { } var results []string for _, id := range intersection { - if title, exists := idx.DocNames[id]; exists { + if title, exists := idx.DocNames[id.DocId]; exists { results = append(results, title) } } -- cgit v1.2.3