summaryrefslogtreecommitdiff
path: root/index/index.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-28 00:30:55 +0200
committeralex <[email protected]>2026-07-28 00:30:55 +0200
commite973c9724829d962ff9e233ef16f489296f9fe3d (patch)
tree324df28e63c5f550bfb2c4f84daa99b554c72eb5 /index/index.go
parent3038483c570af7b923dda39e857b9cab8f89e5dd (diff)
downloadsearch-engine-e973c9724829d962ff9e233ef16f489296f9fe3d.tar.xz
search-engine-e973c9724829d962ff9e233ef16f489296f9fe3d.zip
added Posting type to the reverse index struct which will be later used for scoring results
Diffstat (limited to 'index/index.go')
-rw-r--r--index/index.go26
1 files changed, 17 insertions, 9 deletions
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,
+ })
}
}