From 68f62b4fd161f6563d29ac8a8e102218f59fae43 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 28 Jul 2026 14:45:43 +0200 Subject: added field score calcualtions giving the title a higher weight --- index/index.go | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'index') diff --git a/index/index.go b/index/index.go index 53f9fca..cceabeb 100644 --- a/index/index.go +++ b/index/index.go @@ -1,10 +1,14 @@ package index -import "sync" +import ( + "searchEngine/analyzer" + "sync" +) type Posting struct { - DocId int - Count int + DocId int + Count int + TitleCount int } type InvertedIndex struct { Mu sync.RWMutex @@ -19,11 +23,25 @@ func New() *InvertedIndex { } } -func (idx *InvertedIndex) Add(docID int, title string, tokens []string) { +func (idx *InvertedIndex) Add(docID int, title string, bodyTokens []string) { + titleTokens := analyzer.ProcessText(title) - tokenCounts := make(map[string]int) - for _, token := range tokens { - tokenCounts[token]++ + type fieldStats struct { + body int + title int + } + docStats := make(map[string]fieldStats) + + for _, token := range titleTokens { + stats := docStats[token] + stats.title++ + docStats[token] = stats + } + + for _, token := range bodyTokens { + stats := docStats[token] + stats.body++ + docStats[token] = stats } idx.Mu.Lock() @@ -31,10 +49,11 @@ func (idx *InvertedIndex) Add(docID int, title string, tokens []string) { idx.DocNames[docID] = title - for token, count := range tokenCounts { + for token, stats := range docStats { idx.Data[token] = append(idx.Data[token], Posting{ - DocId: docID, - Count: count, + DocId: docID, + Count: stats.body, + TitleCount: stats.title, }) } } -- cgit v1.2.3