summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--index/index.go28
-rw-r--r--scoring/field.go2
-rw-r--r--search/search.go4
3 files changed, 21 insertions, 13 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,
})
}
}
diff --git a/scoring/field.go b/scoring/field.go
index 9637950..d84ab3b 100644
--- a/scoring/field.go
+++ b/scoring/field.go
@@ -6,5 +6,5 @@ var TitleWeight = 5.0
var BodyWeight = 1.0
func FieldScore(p index.Posting) float64 {
- return (float64(p.TitleCount) * TitleWeight) + (float64(p.Count) * BodyWeight)
+ return (float64(p.TitleCount) * TitleWeight) + (float64(p.BodyCount) * BodyWeight)
}
diff --git a/search/search.go b/search/search.go
index 5fb96f3..061c99c 100644
--- a/search/search.go
+++ b/search/search.go
@@ -28,7 +28,7 @@ func Search(idx *index.InvertedIndex, query string) []string {
scores := make(map[int]int)
for _, p := range basePostings {
- scores[p.DocId] = p.Count
+ scores[p.DocId] = p.BodyCount
}
for i := 1; i < len(tokenizedQuery); i++ {
@@ -40,7 +40,7 @@ func Search(idx *index.InvertedIndex, query string) []string {
lookup := make(map[int]int)
for _, p := range nextPostings {
- lookup[p.DocId] = p.Count
+ lookup[p.DocId] = p.BodyCount
}
newScores := make(map[int]int)