summaryrefslogtreecommitdiff
path: root/index/index.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-28 14:45:43 +0200
committeralex <[email protected]>2026-07-28 14:45:43 +0200
commit68f62b4fd161f6563d29ac8a8e102218f59fae43 (patch)
treed6cd27a63fceeceeca10870233c589db2a1a56cc /index/index.go
parent152c73d8205a252d4311595ffba562110334fd0f (diff)
downloadsearch-engine-68f62b4fd161f6563d29ac8a8e102218f59fae43.tar.xz
search-engine-68f62b4fd161f6563d29ac8a8e102218f59fae43.zip
added field score calcualtions giving the title a higher weight
Diffstat (limited to 'index/index.go')
-rw-r--r--index/index.go39
1 files changed, 29 insertions, 10 deletions
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,
})
}
}