forked from huichen/wukong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_server.go
162 lines (144 loc) · 4 KB
/
search_server.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 一个微博搜索的例子。
package main
import (
"bufio"
"encoding/json"
"flag"
"github.com/huichen/wukong/engine"
"github.com/huichen/wukong/types"
"io"
"log"
"net/http"
"os"
"reflect"
"strconv"
"strings"
)
const (
SecondsInADay = 86400
MaxTokenProximity = 2
)
var (
searcher = engine.Engine{}
wbs = map[uint64]Weibo{}
)
type Weibo struct {
Id uint64 `json:"id"`
Timestamp uint64 `json:"timestamp"`
UserName string `json:"user_name"`
RepostsCount uint64 `json:"reposts_count"`
Text string `json:"text"`
}
/*******************************************************************************
索引
*******************************************************************************/
func indexWeibo() {
// 读入微博数据
file, err := os.Open("../../testdata/weibo_data.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
data := strings.Split(scanner.Text(), "||||")
if len(data) != 10 {
continue
}
wb := Weibo{}
wb.Id, _ = strconv.ParseUint(data[0], 10, 64)
wb.Timestamp, _ = strconv.ParseUint(data[1], 10, 64)
wb.UserName = data[3]
wb.RepostsCount, _ = strconv.ParseUint(data[4], 10, 64)
wb.Text = data[9]
wbs[wb.Id] = wb
}
log.Print("添加索引")
for docId, weibo := range wbs {
searcher.IndexDocument(docId, types.DocumentIndexData{
Content: weibo.Text,
Fields: WeiboScoringFields{
Timestamp: weibo.Timestamp,
RepostsCount: weibo.RepostsCount,
},
})
}
searcher.FlushIndex()
log.Printf("索引了%d条微博\n", len(wbs))
}
/*******************************************************************************
评分
*******************************************************************************/
type WeiboScoringFields struct {
Timestamp uint64
RepostsCount uint64
}
type WeiboScoringCriteria struct {
}
func (criteria WeiboScoringCriteria) Score(
doc types.IndexedDocument, fields interface{}) []float32 {
if reflect.TypeOf(fields) != reflect.TypeOf(WeiboScoringFields{}) {
return []float32{}
}
wsf := fields.(WeiboScoringFields)
output := make([]float32, 3)
if doc.TokenProximity > MaxTokenProximity {
output[0] = 1.0 / float32(doc.TokenProximity)
} else {
output[0] = 1.0
}
output[1] = float32(wsf.Timestamp / (SecondsInADay * 3))
output[2] = float32(doc.BM25 * (1 + float32(wsf.RepostsCount)/10000))
return output
}
/*******************************************************************************
JSON-RPC
*******************************************************************************/
type JsonResponse struct {
Docs []*Weibo `json:"docs"`
}
func JsonRpcServer(w http.ResponseWriter, req *http.Request) {
query := req.URL.Query().Get("query")
output := searcher.Search(types.SearchRequest{
Text: query,
RankOptions: &types.RankOptions{
ScoringCriteria: &WeiboScoringCriteria{},
OutputOffset: 0,
MaxOutputs: 100,
},
})
// 整理为输出格式
docs := []*Weibo{}
for _, doc := range output.Docs {
wb := wbs[doc.DocId]
for _, t := range output.Tokens {
wb.Text = strings.Replace(wb.Text, t, "<font color=red>"+t+"</font>", -1)
}
docs = append(docs, &wb)
}
response, _ := json.Marshal(&JsonResponse{Docs: docs})
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, string(response))
}
/*******************************************************************************
主函数
*******************************************************************************/
func main() {
// 解析命令行参数
flag.Parse()
// 初始化
searcher.Init(types.EngineInitOptions{
SegmenterDictionaries: "../../data/dictionary.txt",
StopTokenFile: "../../data/stop_tokens.txt",
IndexerInitOptions: &types.IndexerInitOptions{
IndexType: types.LocationsIndex,
},
})
wbs = make(map[uint64]Weibo)
// 索引
go indexWeibo()
http.HandleFunc("/json", JsonRpcServer)
http.Handle("/", http.FileServer(http.Dir("static")))
log.Print("服务器启动")
http.ListenAndServe("localhost:8080", nil)
}