forked from yanyiwu/gojieba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bleve_test.go
107 lines (97 loc) · 1.98 KB
/
bleve_test.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
package bleve
import (
"encoding/json"
"fmt"
"os"
"github.com/blevesearch/bleve"
"github.com/yanyiwu/gojieba"
)
func Example() {
INDEX_DIR := "gojieba.bleve"
messages := []struct {
Id string
Body string
}{
{
Id: "1",
Body: "你好",
},
{
Id: "2",
Body: "交代",
},
{
Id: "3",
Body: "长江大桥",
},
}
indexMapping := bleve.NewIndexMapping()
os.RemoveAll(INDEX_DIR)
// clean index when example finished
defer os.RemoveAll(INDEX_DIR)
err := indexMapping.AddCustomTokenizer("gojieba",
map[string]interface{}{
"dictpath": gojieba.DICT_PATH,
"hmmpath": gojieba.HMM_PATH,
"userdictpath": gojieba.USER_DICT_PATH,
"idf": gojieba.IDF_PATH,
"stop_words": gojieba.STOP_WORDS_PATH,
"type": "gojieba",
},
)
if err != nil {
panic(err)
}
err = indexMapping.AddCustomAnalyzer("gojieba",
map[string]interface{}{
"type": "gojieba",
"tokenizer": "gojieba",
},
)
if err != nil {
panic(err)
}
indexMapping.DefaultAnalyzer = "gojieba"
index, err := bleve.New(INDEX_DIR, indexMapping)
if err != nil {
panic(err)
}
for _, msg := range messages {
if err := index.Index(msg.Id, msg); err != nil {
panic(err)
}
}
querys := []string{
"你好世界",
"亲口交代",
"长江",
}
for _, q := range querys {
req := bleve.NewSearchRequest(bleve.NewQueryStringQuery(q))
req.Highlight = bleve.NewHighlight()
res, err := index.Search(req)
if err != nil {
panic(err)
}
fmt.Println(prettify(res))
}
// Output:
// [{"id":"1","score":0.27650412875470115}]
// [{"id":"2","score":0.27650412875470115}]
// [{"id":"3","score":0.7027325540540822}]
}
func prettify(res *bleve.SearchResult) string {
type Result struct {
Id string `json:"id"`
Score float64 `json:"score"`
}
results := []Result{}
for _, item := range res.Hits {
results = append(results, Result{item.ID, item.Score})
}
b, err := json.Marshal(results)
if err != nil {
panic(err)
}
return string(b)
}