diff options
| author | alex <[email protected]> | 2026-07-28 14:57:49 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-28 14:57:49 +0200 |
| commit | 9d0499041410a9b0be3686f79b4e36d7e7355dee (patch) | |
| tree | c0261dc911406b8eb9512b48314b29a069d0a80c /index/index.go | |
| parent | 68f62b4fd161f6563d29ac8a8e102218f59fae43 (diff) | |
| download | search-engine-9d0499041410a9b0be3686f79b4e36d7e7355dee.tar.xz search-engine-9d0499041410a9b0be3686f79b4e36d7e7355dee.zip | |
added TitlePositions and BodyPositions to the posting struct to prepare for proximity scoring
Diffstat (limited to 'index/index.go')
| -rw-r--r-- | index/index.go | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/index/index.go b/index/index.go index cceabeb..60c3d9f 100644 --- a/index/index.go +++ b/index/index.go @@ -6,9 +6,11 @@ import ( ) type Posting struct { - DocId int - Count int - TitleCount int + DocId int + BodyCount int + TitleCount int + BodyPositions []int + TitlePositions []int } type InvertedIndex struct { Mu sync.RWMutex @@ -27,20 +29,24 @@ func (idx *InvertedIndex) Add(docID int, title string, bodyTokens []string) { titleTokens := analyzer.ProcessText(title) type fieldStats struct { - body int - title int + body int + bodyPositions []int + title int + titlePositions []int } docStats := make(map[string]fieldStats) - for _, token := range titleTokens { + for position, token := range titleTokens { stats := docStats[token] stats.title++ + stats.titlePositions = append(stats.titlePositions, position) docStats[token] = stats } - for _, token := range bodyTokens { + for position, token := range bodyTokens { stats := docStats[token] stats.body++ + stats.bodyPositions = append(stats.bodyPositions, position) docStats[token] = stats } @@ -51,9 +57,11 @@ func (idx *InvertedIndex) Add(docID int, title string, bodyTokens []string) { for token, stats := range docStats { idx.Data[token] = append(idx.Data[token], Posting{ - DocId: docID, - Count: stats.body, - TitleCount: stats.title, + DocId: docID, + BodyCount: stats.body, + TitleCount: stats.title, + BodyPositions: stats.bodyPositions, + TitlePositions: stats.titlePositions, }) } } |
