forked from weberhong/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Searcher.go
85 lines (66 loc) · 1.79 KB
/
Searcher.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
package goose
import (
. "github.com/getwe/goose/database"
. "github.com/getwe/goose/utils"
)
type Searcher struct {
// 只读数据库
db DataBaseReader
// 检索策略逻辑
strategy SearchStrategy
}
func (this *Searcher) Search(context *StyContext, reqbuf []byte, resbuf []byte) (reslen int, err error) {
// 解析请求
termInQList, queryInfo, err := this.strategy.ParseQuery(reqbuf, context)
if err != nil {
return 0, err
}
// 构建查询树
me, err := NewMergeEngine(this.db, termInQList)
if err != nil {
return 0, err
}
result := make([]SearchResult, 0, GOOSE_DEFAULT_SEARCH_RESULT_CAPACITY)
// term命中doc的情况
termInDocList := make([]TermInDoc, len(termInQList))
var allfinish bool = false
for allfinish != true {
var inId InIdType
var currValid bool
inId, currValid, allfinish = me.Next(termInDocList)
if currValid != true {
continue
}
outId, err := this.db.GetOutID(inId)
if err != nil {
context.Log.Warn("GetOutId fail [%s] InId[%d] OutId[%d]", err, inId, outId)
continue
}
if inId == 0 || outId == 0 {
context.Log.Warn("MergeEngine get illeagl doc InId[%d] OutId[%d]", inId, outId)
continue
}
weight, err := this.strategy.CalWeight(queryInfo, inId, outId,
termInQList, termInDocList, uint32(len(termInQList)), context)
if err != nil {
context.Log.Warn("CalWeight fail %s", err)
continue
}
result = append(result, SearchResult{
InId: inId,
OutId: outId,
Weight: weight})
}
// 完成
reslen, err = this.strategy.Response(queryInfo, result, this.db, this.db, resbuf, context)
if err != nil {
}
return reslen, nil
}
func NewSearcher(db DataBaseReader, sty SearchStrategy) (*Searcher, error) {
var s Searcher
s.db = db
s.strategy = sty
return &s, nil
}
/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */