diff options
Diffstat (limited to 'core/index')
| -rw-r--r-- | core/index/index.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/core/index/index.go b/core/index/index.go new file mode 100644 index 0000000..59b315c --- /dev/null +++ b/core/index/index.go @@ -0,0 +1,68 @@ +package index + +import ( + "searchEngine/core/analyzer" + "sync" +) + +type Posting struct { + DocId int + BodyCount int + TitleCount int + BodyPositions []int + TitlePositions []int +} + +type InvertedIndex struct { + Mu sync.RWMutex + Data map[string][]Posting + DocNames map[int]string +} + +func New() *InvertedIndex { + return &InvertedIndex{ + Data: make(map[string][]Posting), + DocNames: make(map[int]string), + } +} + +func (idx *InvertedIndex) Add(docID int, title string, bodyTokens []string) { + titleTokens := analyzer.ProcessText(title) + + type fieldStats struct { + body int + bodyPositions []int + title int + titlePositions []int + } + docStats := make(map[string]fieldStats) + + for position, token := range titleTokens { + stats := docStats[token] + stats.title++ + stats.titlePositions = append(stats.titlePositions, position) + docStats[token] = stats + } + + for position, token := range bodyTokens { + stats := docStats[token] + stats.body++ + stats.bodyPositions = append(stats.bodyPositions, position) + docStats[token] = stats + } + + idx.Mu.Lock() + defer idx.Mu.Unlock() + + idx.DocNames[docID] = title + + for token, stats := range docStats { + idx.Data[token] = append(idx.Data[token], Posting{ + DocId: docID, + BodyCount: stats.body, + TitleCount: stats.title, + BodyPositions: stats.bodyPositions, + TitlePositions: stats.titlePositions, + }) + } +} |
