summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.idea/vcs.xml6
-rw-r--r--index/index.go26
-rw-r--r--search/search.go10
3 files changed, 28 insertions, 14 deletions
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="VcsDirectoryMappings">
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
+ </component>
+</project> \ No newline at end of file
diff --git a/index/index.go b/index/index.go
index 7456213..53f9fca 100644
--- a/index/index.go
+++ b/index/index.go
@@ -2,31 +2,39 @@
import "sync"
+type Posting struct {
+ DocId int
+ Count int
+}
type InvertedIndex struct {
Mu sync.RWMutex
- Data map[string][]int
+ Data map[string][]Posting
DocNames map[int]string
}
func New() *InvertedIndex {
return &InvertedIndex{
- Data: make(map[string][]int),
+ Data: make(map[string][]Posting),
DocNames: make(map[int]string),
}
}
func (idx *InvertedIndex) Add(docID int, title string, tokens []string) {
+
+ tokenCounts := make(map[string]int)
+ for _, token := range tokens {
+ tokenCounts[token]++
+ }
+
idx.Mu.Lock()
defer idx.Mu.Unlock()
idx.DocNames[docID] = title
- seen := make(map[string]struct{})
-
- for _, token := range tokens {
- if _, exists := seen[token]; !exists {
- idx.Data[token] = append(idx.Data[token], docID)
- seen[token] = struct{}{}
- }
+ for token, count := range tokenCounts {
+ idx.Data[token] = append(idx.Data[token], Posting{
+ DocId: docID,
+ Count: count,
+ })
}
}
diff --git a/search/search.go b/search/search.go
index ced94b7..f0b1cce 100644
--- a/search/search.go
+++ b/search/search.go
@@ -21,7 +21,7 @@ func Search(idx *index.InvertedIndex, query string) []string {
return nil
}
- intersection := make([]int, len(baseIDs))
+ intersection := make([]index.Posting, len(baseIDs))
copy(intersection, baseIDs)
for i := 1; i < len(tokenizedQuery); i++ {
@@ -34,12 +34,12 @@ func Search(idx *index.InvertedIndex, query string) []string {
lookup := make(map[int]struct{})
for _, id := range nextIDs {
- lookup[id] = struct{}{}
+ lookup[id.DocId] = struct{}{}
}
- var filtered []int
+ var filtered []index.Posting
for _, id := range intersection {
- if _, found := lookup[id]; found {
+ if _, found := lookup[id.DocId]; found {
filtered = append(filtered, id)
}
}
@@ -49,7 +49,7 @@ func Search(idx *index.InvertedIndex, query string) []string {
}
var results []string
for _, id := range intersection {
- if title, exists := idx.DocNames[id]; exists {
+ if title, exists := idx.DocNames[id.DocId]; exists {
results = append(results, title)
}
}