-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.go
351 lines (304 loc) · 7.75 KB
/
mem.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package index
import (
"fmt"
"sort"
"sync"
iq "github.com/rekki/go-query"
analyzer "github.com/rekki/go-query-analyze"
)
// MemOnlyIndex is representation of an index stored in the memory
type MemOnlyIndex struct {
perField map[string]*analyzer.Analyzer
postings map[string]map[string][]int32
forward []Document
// stored twice, but just for convinience
forwardByID map[string]int32
IDField string
sync.RWMutex
}
// NewMemOnlyIndex creates new in-memory index with the specified perField analyzer by default DefaultAnalyzer is used
func NewMemOnlyIndex(perField map[string]*analyzer.Analyzer) *MemOnlyIndex {
if perField == nil {
perField = map[string]*analyzer.Analyzer{}
}
m := &MemOnlyIndex{postings: map[string]map[string][]int32{}, perField: perField, forwardByID: map[string]int32{}, IDField: "_id"}
return m
}
func (m *MemOnlyIndex) MergeInto(b *MemOnlyIndex) {
m.Lock()
defer m.Unlock()
b.RLock()
defer b.RUnlock()
offset := int32(len(m.forward))
for k, v := range b.perField {
m.perField[k] = v
}
for field, terms := range b.postings {
for term, ps := range terms {
ms := m.postings[field][term]
for _, docId := range ps {
ms = append(ms, docId+offset)
}
m.postings[field][term] = ms
}
}
for uuid, docId := range b.forwardByID {
m.forwardByID[uuid] = docId + offset
}
m.forward = append(m.forward, b.forward...)
}
func (m *MemOnlyIndex) Get(id int32) Document {
return m.forward[id]
}
func (m *MemOnlyIndex) GetByID(uuid string) Document {
m.RLock()
id, ok := m.forwardByID[uuid]
m.RUnlock()
if ok {
return m.forward[id]
}
return nil
}
func (m *MemOnlyIndex) DeleteByID(uuid string) {
m.Lock()
defer m.Unlock()
id, ok := m.forwardByID[uuid]
if ok {
m.deleteLocked(id)
}
}
func (m *MemOnlyIndex) Delete(id int32) {
m.Lock()
defer m.Unlock()
m.deleteLocked(id)
}
func (m *MemOnlyIndex) deleteLocked(id int32) {
d := m.forward[id]
fields := d.IndexableFields()
for field, value := range fields {
if field == m.IDField {
for _, v := range value {
delete(m.forwardByID, v)
}
}
analyzer, ok := m.perField[field]
if !ok {
if field == m.IDField || field == "id" || field == "uuid" {
analyzer = IDAnalyzer
} else {
analyzer = DefaultAnalyzer
}
}
for _, v := range value {
tokens := analyzer.AnalyzeIndex(v)
for _, t := range tokens {
m.deletePostings(field, t, id)
}
}
}
m.forward[id] = nil
}
// Index a bunch of documents
func (m *MemOnlyIndex) Index(docs ...Document) {
m.Lock()
defer m.Unlock()
for _, d := range docs {
fields := d.IndexableFields()
did := len(m.forward)
m.forward = append(m.forward, d)
for field, value := range fields {
if field == m.IDField {
for _, v := range value {
m.forwardByID[v] = int32(did)
}
}
analyzer, ok := m.perField[field]
if !ok {
if field == m.IDField || field == "id" || field == "uuid" {
analyzer = IDAnalyzer
} else {
analyzer = DefaultAnalyzer
}
}
for _, v := range value {
tokens := analyzer.AnalyzeIndex(v)
for _, t := range tokens {
m.addPostings(field, t, int32(did))
}
}
}
}
}
func (m *MemOnlyIndex) addPostings(k, v string, did int32) {
pk, ok := m.postings[k]
if !ok {
pk = map[string][]int32{}
m.postings[k] = pk
}
current, ok := pk[v]
if !ok || len(current) == 0 {
pk[v] = []int32{did}
} else {
if current[len(current)-1] != did {
pk[v] = append(current, did)
}
}
}
func (m *MemOnlyIndex) deletePostings(k, v string, did int32) {
pk, ok := m.postings[k]
if !ok {
return
}
current, ok := pk[v]
if !ok || len(current) == 0 {
return
}
// find the index where this documentID is and cut the slice
found := sort.Search(len(current), func(i int) bool {
return current[i] >= did
})
if found < len(current) && current[found] == did {
pk[v] = append(current[:found], current[found+1:]...)
}
}
// Terms generates array of queries from the tokenized term for this field, using the perField analyzer
func (m *MemOnlyIndex) Terms(field string, term string) []iq.Query {
m.RLock()
defer m.RUnlock()
analyzer, ok := m.perField[field]
if !ok {
analyzer = DefaultAnalyzer
}
tokens := analyzer.AnalyzeSearch(term)
queries := []iq.Query{}
for _, t := range tokens {
queries = append(queries, m.NewTermQuery(field, t))
}
return queries
}
func (m *MemOnlyIndex) NewTermQuery(field string, term string) iq.Query {
m.RLock()
defer m.RUnlock()
s := fmt.Sprintf("%s:%s", field, term)
pk, ok := m.postings[field]
if !ok {
return iq.Term(len(m.forward), s, []int32{})
}
pv, ok := pk[term]
if !ok {
return iq.Term(len(m.forward), s, []int32{})
}
// there are allocation in iq.Term(), so dont just defer unlock, otherwise it will be locked while term is created
return iq.Term(len(m.forward), s, pv)
}
// Foreach matching document
// Example:
// query := iq.And(
// iq.Or(m.Terms("name", "aMS u")...),
// iq.Or(m.Terms("country", "NL BG")...),
// )
// m.Foreach(query, func(did int32, score float32, doc index.Document) {
// city := doc.(*ExampleCity)
// log.Printf("%v matching with score %f", city, score)
// })
func (m *MemOnlyIndex) Foreach(query iq.Query, cb func(int32, float32, Document)) {
m.RLock()
defer m.RUnlock()
for query.Next() != iq.NO_MORE {
did := query.GetDocId()
score := query.Score()
doc := m.forward[did]
if doc == nil {
// deleted
// there is a race here between m.Terms() and m.Foreach()
// m.Terms() will return a slice that might have document id inside
// that is in parallel deleted before the caller calls m.Foreach
// and it can return null document
// ther is no point in locking, as the next m.Terms will hold the right
// value
continue
}
cb(did, score, doc)
}
}
// TopN documents
// The following texample gets top5 results and also check add 100 to the score of cities that have NL in the score.
// usually the score of your search is some linear combination of f(a*text + b*popularity + c*context..)
//
// Example:
// query := iq.And(
// iq.Or(m.Terms("name", "ams university")...),
// iq.Or(m.Terms("country", "NL BG")...),
// )
// top := m.TopN(5, q, func(did int32, score float32, doc Document) float32 {
// city := doc.(*ExampleCity)
// if city.Country == "NL" {
// score += 100
// }
// n++
// return score
// })
// the SearchResult structure looks like
// {
// "total": 3,
// "hits": [
// {
// "score": 101.09861,
// "id": 0,
// "doc": {
// "Name": "Amsterdam",
// "Country": "NL"
// }
// }
// ...
// ]
// }
// If the callback is null, then the original score is used (1*idf at the moment)
func (m *MemOnlyIndex) TopN(limit int, query iq.Query, cb func(int32, float32, Document) float32) *SearchResult {
out := &SearchResult{}
scored := []Hit{}
m.Foreach(query, func(did int32, originalScore float32, d Document) {
out.Total++
if limit == 0 {
return
}
score := originalScore
if cb != nil {
score = cb(did, originalScore, d)
}
// just keep the list sorted
// FIXME: use bounded priority queue
doInsert := false
if len(scored) < limit {
doInsert = true
} else if scored[len(scored)-1].Score < score {
doInsert = true
}
if doInsert {
hit := Hit{Score: score, ID: did, Document: d}
if len(scored) < limit {
scored = append(scored, hit)
}
for i := 0; i < len(scored); i++ {
if scored[i].Score < hit.Score {
copy(scored[i+1:], scored[i:])
scored[i] = hit
break
}
}
}
})
out.Hits = scored
return out
}
// Hit is struct result for `TopN` method
type Hit struct {
Score float32 `json:"score"`
ID int32 `json:"id"`
Document Document `json:"doc"`
}
// SearchResult is the search result for the `TopN` method
type SearchResult struct {
Total int `json:"total"`
Hits []Hit `json:"hits"`
}