From c91c3afd8700138f8eb8a25a4ae422c2c9e46cea Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 28 Jul 2026 17:37:06 +0200 Subject: refactored entire code base to be in the package core so i can later implement interfaces to interact with the engine --- search/search.go | 72 -------------------------------------------------------- 1 file changed, 72 deletions(-) delete mode 100644 search/search.go (limited to 'search') diff --git a/search/search.go b/search/search.go deleted file mode 100644 index 061c99c..0000000 --- a/search/search.go +++ /dev/null @@ -1,72 +0,0 @@ -package search - -import ( - "searchEngine/analyzer" - "searchEngine/index" - "slices" -) - -type Result struct { - DocID int - Score int -} - -func Search(idx *index.InvertedIndex, query string) []string { - tokenizedQuery := analyzer.ProcessText(query) - if len(tokenizedQuery) == 0 { - return nil - } - - idx.Mu.RLock() - defer idx.Mu.RUnlock() - - firstToken := tokenizedQuery[0] - basePostings, exists := idx.Data[firstToken] - if !exists { - return nil - } - - scores := make(map[int]int) - for _, p := range basePostings { - scores[p.DocId] = p.BodyCount - } - - for i := 1; i < len(tokenizedQuery); i++ { - token := tokenizedQuery[i] - nextPostings, exists := idx.Data[token] - if !exists { - return nil - } - - lookup := make(map[int]int) - for _, p := range nextPostings { - lookup[p.DocId] = p.BodyCount - } - - newScores := make(map[int]int) - for docID, currentScore := range scores { - if nextCount, found := lookup[docID]; found { - newScores[docID] = currentScore + nextCount - } - } - scores = newScores - } - - var results []Result - for docID, score := range scores { - results = append(results, Result{DocID: docID, Score: score}) - } - - slices.SortFunc(results, func(a, b Result) int { - return b.Score - a.Score - }) - - var titles []string - for _, res := range results { - if title, exists := idx.DocNames[res.DocID]; exists { - titles = append(titles, title) - } - } - - return titles -} -- cgit v1.2.3