This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathsearch_server.go
executable file
·215 lines (187 loc) · 5.34 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// 一个微博搜索的例子。
package main
import (
"bufio"
"flag"
"io"
"log"
"os"
"reflect"
"strconv"
"strings"
"encoding/gob"
"encoding/json"
"net/http"
"os/signal"
"github.com/go-ego/riot"
"github.com/go-ego/riot/types"
)
const (
// SecondsInADay seconds in a day
SecondsInADay = 86400
// MaxTokenProximity max token proximity
MaxTokenProximity = 2
)
var (
searcher = riot.Engine{}
wbs = map[string]Weibo{}
weiboData = flag.String("weibo_data",
"../../testdata/weibo_data.txt", "微博数据文件")
dictFile = flag.String("dict_file",
"../../data/dict/dictionary.txt", "词典文件")
stopTokenFile = flag.String("stop_token_file",
"../../data/dict/stop_tokens.txt", "停用词文件")
staticFolder = flag.String("static_folder", "static", "静态文件目录")
)
// Weibo weibo json struct
type Weibo struct {
// Id uint64 `json:"id"`
Id string `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(*weiboData)
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.Id = data[0]
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.Index(docId, types.DocData{
Content: weibo.Text,
Fields: WeiboScoringFields{
Timestamp: weibo.Timestamp,
RepostsCount: weibo.RepostsCount,
},
})
}
searcher.Flush()
log.Printf("索引了%d条微博\n", len(wbs))
}
/*******************************************************************************
评分
*******************************************************************************/
// WeiboScoringFields weibo scoring fields
type WeiboScoringFields struct {
Timestamp uint64
RepostsCount uint64
}
// WeiboScoringCriteria custom weibo scoring criteria
type WeiboScoringCriteria struct {
}
// Score score and sort
func (criteria WeiboScoringCriteria) Score(
doc types.IndexedDoc, 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
*******************************************************************************/
// JsonResponse json response
type JsonResponse struct {
Docs []*Weibo `json:"docs"`
}
// JsonRpcServer json rpc server
func JsonRpcServer(w http.ResponseWriter, req *http.Request) {
query := req.URL.Query().Get("query")
output := searcher.SearchDoc(types.SearchReq{
Text: query,
RankOpts: &types.RankOpts{
ScoringCriteria: &WeiboScoringCriteria{},
OutputOffset: 0,
MaxOutputs: 100,
},
})
// 整理为输出格式
docs := []*Weibo{}
for _, doc := range output.Docs {
wb := wbs[doc.DocId]
wb.Text = doc.Content
// 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})
// fmt.Println("response...", response)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, string(response))
}
/*******************************************************************************
主函数
*******************************************************************************/
func main() {
// 解析命令行参数
flag.Parse()
// 初始化
gob.Register(WeiboScoringFields{})
log.Print("引擎开始初始化")
searcher.Init(types.EngineOpts{
Using: 1,
GseDict: *dictFile,
StopTokenFile: *stopTokenFile,
IndexerOpts: &types.IndexerOpts{
IndexType: types.LocsIndex,
},
// 如果你希望使用持久存储,启用下面的选项
// 默认使用leveldb持久化,如果你希望修改数据库类型
// 请用 StoreEngine: " " 或者修改 Riot_Store_Engine 环境变量
// UseStore: true,
// StoreFolder: "weibo_search",
// StoreEngine: "bg",
})
log.Println("引擎初始化完毕")
wbs = make(map[string]Weibo)
// 索引
log.Println("建索引开始")
go indexWeibo()
log.Println("建索引完毕")
// 捕获 ctrl-c
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
log.Println("捕获Ctrl-c,退出服务器")
searcher.Close()
os.Exit(0)
}
}()
http.HandleFunc("/json", JsonRpcServer)
http.Handle("/", http.FileServer(http.Dir(*staticFolder)))
log.Println("服务器启动")
log.Fatal(http.ListenAndServe(":8080", nil))
}