feat: Update Fiber version to v2 and update internal controller and search logic to use new index module

This commit is contained in:
2025-03-25 21:09:01 +01:00
parent 8746f789c4
commit ed201de318
9 changed files with 85 additions and 62 deletions

View File

@@ -1,4 +1,4 @@
package search
package index
import (
"bufio"
@@ -13,7 +13,7 @@ type DocumentFrequency map[string]int
var Index InvertedIndex
var DocFreq DocumentFrequency
var Files []string
var Documents []string
// BuildIndex reads files and builds an inverted index.
func BuildIndex(files []string) (InvertedIndex, DocumentFrequency, error) {
@@ -57,7 +57,42 @@ func TestIndex() {
log.Fatalf("Error building index: %v", err)
}
Files = files
Documents = files
Index = index
DocFreq = docFreq
}
func CreateIndex() {
index := make(InvertedIndex)
docFreq := make(DocumentFrequency)
var docs []string
Documents = docs
Index = index
DocFreq = docFreq
}
func AddDocToIndex(url string, content string) {
Documents = append(Documents, url)
reader := strings.NewReader(content)
seenTerms := make(map[string]bool) // Track terms in this document
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
word := strings.ToLower(strings.Trim(scanner.Text(), ",.!?"))
if Index[word] == nil {
Index[word] = make(map[string]int)
}
Index[word][url]++
if !seenTerms[word] {
DocFreq[word]++
seenTerms[word] = true
}
}
}

View File

@@ -3,6 +3,8 @@ package search
import (
"math"
"sort"
"git.dev-null.rocks/alexohneander/gosearch/pkg/index"
)
// SearchResult stores the document and its relevance score.
@@ -12,7 +14,7 @@ type SearchResult struct {
}
// Search processes different types of queries using TF-IDF scoring.
func Search(terms []string, queryType string, index InvertedIndex, docFreq DocumentFrequency, numDocs int) []SearchResult {
func Search(terms []string, queryType string, index index.InvertedIndex, docFreq index.DocumentFrequency, numDocs int) []SearchResult {
scores := make(map[string]float64)
if queryType == "AND" {
@@ -40,7 +42,7 @@ func Search(terms []string, queryType string, index InvertedIndex, docFreq Docum
}
// Helper function to score a single document based on terms
func scoreDoc(terms []string, doc string, index InvertedIndex, docFreq DocumentFrequency, numDocs int) float64 {
func scoreDoc(terms []string, doc string, index index.InvertedIndex, docFreq index.DocumentFrequency, numDocs int) float64 {
score := 0.0
for _, term := range terms {
tf := float64(index[term][doc])
@@ -52,7 +54,7 @@ func scoreDoc(terms []string, doc string, index InvertedIndex, docFreq DocumentF
}
// Helper function to intersect documents for AND logic
func intersectDocs(terms []string, index InvertedIndex) []string {
func intersectDocs(terms []string, index index.InvertedIndex) []string {
if len(terms) == 0 {
return nil
}