summaryrefslogtreecommitdiff
path: root/index/index.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-27 23:55:14 +0200
committeralex <[email protected]>2026-07-27 23:55:14 +0200
commit3038483c570af7b923dda39e857b9cab8f89e5dd (patch)
tree62df1d164f8364d02d5bb811f4ac95b20a77654e /index/index.go
downloadsearch-engine-3038483c570af7b923dda39e857b9cab8f89e5dd.tar.xz
search-engine-3038483c570af7b923dda39e857b9cab8f89e5dd.zip
init commit, basic tokenizing and indexing logic without any ranking of results on a simple wikipedia dump
Diffstat (limited to 'index/index.go')
-rw-r--r--index/index.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/index/index.go b/index/index.go
new file mode 100644
index 0000000..7456213
--- /dev/null
+++ b/index/index.go
@@ -0,0 +1,32 @@
+package index
+
+import "sync"
+
+type InvertedIndex struct {
+ Mu sync.RWMutex
+ Data map[string][]int
+ DocNames map[int]string
+}
+
+func New() *InvertedIndex {
+ return &InvertedIndex{
+ Data: make(map[string][]int),
+ DocNames: make(map[int]string),
+ }
+}
+
+func (idx *InvertedIndex) Add(docID int, title string, tokens []string) {
+ 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{}{}
+ }
+ }
+}