package index import ( "searchEngine/analyzer" "sync" ) type Posting struct { DocId int Count int TitleCount 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 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() defer idx.Mu.Unlock() idx.DocNames[docID] = title for token, stats := range docStats { idx.Data[token] = append(idx.Data[token], Posting{ DocId: docID, Count: stats.body, TitleCount: stats.title, }) } }