This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
bleve_test.go
199 lines (176 loc) · 3.77 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
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
package gsebleve
import (
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"testing"
"github.com/blevesearch/bleve/v2"
)
func Example() {
INDEX_DIR := "bleve.gse"
message := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作"
mapping := bleve.NewIndexMapping()
os.RemoveAll(INDEX_DIR)
defer os.RemoveAll(INDEX_DIR)
if err := mapping.AddCustomTokenizer("gse", map[string]interface{}{
"type": "gse",
"user_dicts": "./data/dict/zh/dict.txt",
}); err != nil {
panic(err)
}
if err := mapping.AddCustomAnalyzer("gse", map[string]interface{}{
"type": "gse",
"tokenizer": "gse",
}); err != nil {
panic(err)
}
mapping.DefaultAnalyzer = "gse"
index, err := bleve.New(INDEX_DIR, mapping)
if err != nil {
panic(err)
}
if err := index.Index("1", message); err != nil {
panic(err)
}
query := "干事亲口交待"
req := bleve.NewSearchRequest(bleve.NewQueryStringQuery(query))
req.Highlight = bleve.NewHighlight()
res, err := index.Search(req)
if err != nil {
panic(err)
}
fmt.Printf("Result of: '%s': %d matches\n", query, res.Total)
for i, hit := range res.Hits {
rv := fmt.Sprintf("%d. %s, (%f)\n", i+res.Request.From+1, hit.ID, hit.Score)
for fragmentField, fragments := range hit.Fragments {
rv += fmt.Sprintf("%s: ", fragmentField)
for _, fragment := range fragments {
rv += fmt.Sprintf("%s", fragment)
}
}
fmt.Printf("%s\n", rv)
}
index.Close()
}
func BenchmarkExample(b *testing.B) {
cpuProfile, err := os.Create("cpu.prof")
if err != nil {
log.Fatal("could not create cpu profile: ", err)
}
pprof.StartCPUProfile(cpuProfile)
defer pprof.StopCPUProfile()
for i := 0; i < b.N; i++ {
Example()
}
memProfile, err := os.Create("mem.prof")
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer memProfile.Close()
runtime.GC()
if err := pprof.WriteHeapProfile(memProfile); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
type testCase struct {
query string
match uint64
}
func TestSearch(t *testing.T) {
testData := []testCase{
{
"干事亲口交待", 1,
},
{
"干事", 0, // ???!
},
{
"女干事", 1,
},
{
"事亲", 0,
},
{
"亲口", 1,
},
{
"口交", 0,
},
{
"交待", 0, // ???!
},
{
"口交待", 1, // !!?
},
{
"亲口交待", 1,
},
{
"工信交待", 0,
},
{
"干事交待", 0, // !!
},
{
"干事亲口", 1, // !!
},
{
"干事亲口交待", 1,
},
{
"每月经过", 1,
},
{
"每月交代", 1, // !!
},
{
"每月技术", 1, // !!
},
{
"安装技术", 1, // !!
},
}
INDEX_DIR := "bleve.gse"
message := "工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作"
mapping := bleve.NewIndexMapping()
os.RemoveAll(INDEX_DIR)
defer os.RemoveAll(INDEX_DIR)
if err := mapping.AddCustomTokenizer("gse", map[string]interface{}{
"type": "gse",
"user_dicts": "./data/dict/zh/dict.txt",
}); err != nil {
panic(err)
}
if err := mapping.AddCustomAnalyzer("gse", map[string]interface{}{
"type": "gse",
"tokenizer": "gse",
}); err != nil {
panic(err)
}
mapping.DefaultAnalyzer = "gse"
index, err := bleve.New(INDEX_DIR, mapping)
if err != nil {
panic(err)
}
if err := index.Index("1", message); err != nil {
panic(err)
}
for _, tc := range testData {
query := tc.query
req := bleve.NewSearchRequest(bleve.NewQueryStringQuery(query))
req.Highlight = bleve.NewHighlight()
res, err := index.Search(req)
if err != nil {
panic(err)
}
fmt.Printf("Search of: '%s' ", query)
if tc.match != res.Total {
t.Errorf(`expected "%d" but got "%d" matches`, tc.match, res.Total)
} else {
fmt.Println("matched")
}
}
index.Close()
}