-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.go
208 lines (168 loc) · 4.23 KB
/
index.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
package srchx
import (
"errors"
"math"
"strings"
"time"
"github.com/blevesearch/bleve/search/query"
"github.com/blevesearch/bleve"
"github.com/imdario/mergo"
"github.com/satori/go.uuid"
)
// Index - the index wrapper
type Index struct {
bleve bleve.Index
}
// NewIndex - initialize a new index wrapper
func NewIndex(ndx bleve.Index) *Index {
i := new(Index)
i.bleve = ndx
return i
}
// Delete - delete a document from the index
func (i *Index) Delete(id string) {
i.bleve.Delete(id)
}
// Get - loads a document from the index
func (i *Index) Get(id string) (map[string]interface{}, error) {
res, err := i.Search(&Query{
Query: query.Query(bleve.NewDocIDQuery([]string{id})),
})
if err != nil {
return nil, err
}
if res.Totals < 1 {
return nil, errors.New("no data found")
}
return res.Docs[0], nil
}
// Put - set/update a document
func (i *Index) Put(data map[string]interface{}) (document map[string]interface{}, err error) {
if _, found := data["id"]; !found || data["id"] == "" {
uid, err := uuid.NewV4()
if err != nil {
return nil, err
}
data["id"] = uid.String()
data["created_at"] = time.Now().UnixNano()
}
id, ok := data["id"].(string)
if !ok {
return nil, errors.New("invalid id specified, it must be string")
}
if document, err = i.Get(id); err == nil && document != nil {
document["id"] = id
}
if err = mergo.Map(&document, data, mergo.WithOverride); err != nil {
return nil, err
}
document["updated_at"] = time.Now().UnixNano()
if err = i.bleve.Index(id, document); err != nil {
return nil, err
}
return document, nil
}
// Aggregate - aggregates (count, sum and avg) using the specified query and field
func (i *Index) Aggregate(q *Query, field, fn string) (result float64) {
scroll := func(from, size int) *bleve.SearchResult {
searchRequest := bleve.NewSearchRequest(q.Query)
searchRequest.Fields = []string{field}
searchRequest.From = from
searchRequest.Size = size
res, _ := i.bleve.Search(searchRequest)
return res
}
frst := scroll(0, 1)
if nil == frst || frst.Total < 1 {
return 0
}
switch strings.ToLower(fn) {
case "count":
result = float64(frst.Total)
case "sum":
all := scroll(0, math.MaxInt32)
if nil != all && all.Total > 0 {
for _, doc := range all.Hits {
val, ok := doc.Fields[field].(float64)
if !ok {
continue
}
result += val
}
}
case "avg":
all := scroll(0, math.MaxInt32)
sum := float64(0)
if nil != all && all.Total > 0 {
for _, doc := range all.Hits {
val, ok := doc.Fields[field].(float64)
if !ok {
continue
}
sum += val
}
}
result = sum / float64(frst.Total)
}
return result
}
// Search - search in the index for the specified query
func (i *Index) Search(q *Query) (*SearchResult, error) {
if q.Size < 1 {
q.Size = 10
}
searchRequest := bleve.NewSearchRequest(q.Query)
searchRequest.Fields = []string{"*"}
searchRequest.IncludeLocations = true
searchRequest.From = q.Offset
searchRequest.Size = q.Size
searchRequest.SortBy(q.Sort)
res, err := i.bleve.Search(searchRequest)
if err != nil {
return nil, err
}
docs := []map[string]interface{}{}
for _, doc := range res.Hits {
doc.Fields["_score"] = doc.Score
doc.Fields["_size"] = doc.Size()
doc.Fields["_offset"] = doc.HitNumber
docs = append(docs, doc.Fields)
}
ret := &SearchResult{
Totals: res.Total,
Docs: docs,
Time: int64(res.Took),
}
i.applyJOIN(ret, q)
return ret, nil
}
// ApplyJOIN - apply joins on the specified search result
func (i *Index) applyJOIN(res *SearchResult, q *Query) {
if len(q.Join) < 1 {
return
}
for x, doc := range res.Docs {
for _, join := range q.Join {
if join.Where == nil {
join.Where = &Query{}
}
if join.On == "" || join.As == "" {
continue
}
if doc[join.On] == nil {
continue
}
join.Where.Query = bleve.NewDocIDQuery([]string{doc[join.On].(string)})
join.Where.Join = q.Join
if join.Where.Query != nil {
join.Where.Query = bleve.NewConjunctionQuery(join.Where.Query, bleve.NewDocIDQuery([]string{doc[join.On].(string)}))
} else {
join.Where.Query = bleve.NewDocIDQuery([]string{doc[join.On].(string)})
}
sub, _ := i.Search(join.Where)
delete(doc, join.On)
doc[join.As] = sub.Docs
res.Docs[x] = doc
}
}
}