-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (74 loc) · 2.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
invertedindex "github.com/teamelehyean/brill/index"
"github.com/teamelehyean/brill/ranker"
"github.com/teamelehyean/brill/repository"
"github.com/teamelehyean/brill/tokenizer"
)
func main() {
invertedindex.DisplayInvertedIndex()
http.HandleFunc("/", serverRest)
http.HandleFunc("/notify", notify)
fmt.Println("Search engine started")
http.ListenAndServe("localhost:8800", nil)
// results := ranker.Rank("Lorem Ipsum discrete mathematics script")
// fmt.Println(results)
}
func getJSONResponse(query string) ([]byte, error) {
results := ranker.Rank(query)
url := "http://localhost:22000/documents"
data, err := json.Marshal(results)
if err != nil {
fmt.Println("Could not marshal data")
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
var documents []Payload
json.Unmarshal(body, &documents)
searchResult := SearchResult{Size: int64(len(results)), Documents: documents}
return json.MarshalIndent(searchResult, "", " ")
}
func serverRest(w http.ResponseWriter, r *http.Request) {
query := r.FormValue("q")
response, _ := getJSONResponse(query)
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, string(response))
}
func notify(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println("Error")
}
var p Payload
json.Unmarshal(body, &p)
content, err := repository.GetFileContents(p.FileName)
tokens := tokenizer.GetTokens(content)
fmt.Println("Finished Reading " + p.FileName)
fmt.Println("Started Indexing " + p.FileName)
for _, token := range tokens {
invertedindex.AddToIndex(token, p.ID)
}
fmt.Println("Finished Indexing " + p.FileName)
}
//Payload represents the result
type Payload struct {
ID int64 `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
FileName string `json:"filename"`
CoverImage string `json:"cover_img"`
Uploaded int64 `json:"uploaded"`
}
//SearchResult denotes search engine response
type SearchResult struct {
Size int64 `json:"size"`
Documents []Payload `json:"documents"`
}