summaryrefslogtreecommitdiff
path: root/search/search.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 /search/search.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 'search/search.go')
-rw-r--r--search/search.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/search/search.go b/search/search.go
new file mode 100644
index 0000000..ced94b7
--- /dev/null
+++ b/search/search.go
@@ -0,0 +1,58 @@
+package search
+
+import (
+ "searchEngine/analyzer"
+ "searchEngine/index"
+)
+
+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]
+ baseIDs, exists := idx.Data[firstToken]
+ if !exists {
+ return nil
+ }
+
+ intersection := make([]int, len(baseIDs))
+ copy(intersection, baseIDs)
+
+ for i := 1; i < len(tokenizedQuery); i++ {
+ token := tokenizedQuery[i]
+
+ nextIDs, exists := idx.Data[token]
+ if !exists {
+ return nil
+ }
+
+ lookup := make(map[int]struct{})
+ for _, id := range nextIDs {
+ lookup[id] = struct{}{}
+ }
+
+ var filtered []int
+ for _, id := range intersection {
+ if _, found := lookup[id]; found {
+ filtered = append(filtered, id)
+ }
+ }
+
+ intersection = filtered
+
+ }
+ var results []string
+ for _, id := range intersection {
+ if title, exists := idx.DocNames[id]; exists {
+ results = append(results, title)
+ }
+ }
+
+ return results
+}