-
Notifications
You must be signed in to change notification settings - Fork 121
/
search.go
372 lines (297 loc) · 9.42 KB
/
search.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (c) 2020 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package search
import (
"fmt"
"sort"
segment "github.com/blugelabs/bluge_segment_api"
"github.com/blugelabs/bluge/analysis"
)
type Location struct {
Pos int
Start int
End int
}
func (l *Location) Size() int {
return reflectStaticSizeLocation
}
type Locations []*Location
func (p Locations) Len() int { return len(p) }
func (p Locations) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p Locations) Less(i, j int) bool {
return p[i].Pos < p[j].Pos
}
func (p Locations) Dedupe() Locations { // destructive!
if len(p) <= 1 {
return p
}
sort.Sort(p)
slow := 0
for _, pfast := range p {
pslow := p[slow]
if pslow.Pos == pfast.Pos &&
pslow.Start == pfast.Start &&
pslow.End == pfast.End {
continue // duplicate, so only move fast ahead
}
slow++
p[slow] = pfast
}
return p[:slow+1]
}
type TermLocationMap map[string]Locations
func (t TermLocationMap) AddLocation(term string, location *Location) {
t[term] = append(t[term], location)
}
type FieldTermLocationMap map[string]TermLocationMap
type FieldTermLocation struct {
Field string
Term string
Location Location
}
type FieldFragmentMap map[string][]string
type DocumentMatch struct {
reader MatchReader
Number uint64
Score float64
Explanation *Explanation
Locations FieldTermLocationMap
SortValue [][]byte
docValues map[string][][]byte
// used to maintain natural index order
HitNumber int
// used to temporarily hold field term location information during
// search processing in an efficient, recycle-friendly manner, to
// be later incorporated into the Locations map when search
// results are completed
FieldTermLocations []FieldTermLocation
}
func (dm *DocumentMatch) SetReader(r MatchReader) {
dm.reader = r
}
func (dm *DocumentMatch) addDocValue(name string, value []byte) {
if dm.docValues == nil {
dm.docValues = make(map[string][][]byte)
}
dm.docValues[name] = append(dm.docValues[name], value)
}
func (dm *DocumentMatch) LoadDocumentValues(ctx *Context, fields []string) error {
dvReader, err := ctx.DocValueReaderForReader(dm.reader, fields)
if err != nil {
return err
}
return dvReader.VisitDocumentValues(dm.Number, dm.addDocValue)
}
func (dm *DocumentMatch) DocValues(field string) [][]byte {
if dm.docValues != nil {
return dm.docValues[field]
}
return nil
}
func (dm *DocumentMatch) VisitStoredFields(visitor segment.StoredFieldVisitor) error {
return dm.reader.VisitStoredFields(dm.Number, visitor)
}
// Reset allows an already allocated DocumentMatch to be reused
func (dm *DocumentMatch) Reset() *DocumentMatch {
// remember the [][]byte used for sort
sortValue := dm.SortValue
// remember the FieldTermLocations backing array
ftls := dm.FieldTermLocations
// idiom to copy over from empty DocumentMatch (0 allocations)
*dm = DocumentMatch{}
// reuse the [][]byte already allocated (and reset len to 0)
dm.SortValue = sortValue[:0]
// reuse the FieldTermLocations already allocated (and reset len to 0)
dm.FieldTermLocations = ftls[:0]
return dm
}
func (dm *DocumentMatch) Size() int {
sizeInBytes := reflectStaticSizeDocumentMatch + sizeOfPtr
if dm.Explanation != nil {
sizeInBytes += dm.Explanation.Size()
}
for k, v := range dm.Locations {
sizeInBytes += sizeOfString + len(k)
for k1, v1 := range v {
sizeInBytes += sizeOfString + len(k1) +
sizeOfSlice
for _, entry := range v1 {
sizeInBytes += entry.Size()
}
}
}
for _, entry := range dm.SortValue {
sizeInBytes += sizeOfSlice + len(entry)
}
return sizeInBytes
}
// Complete performs final preparation & transformation of the
// DocumentMatch at the end of search processing, also allowing the
// caller to provide an optional preallocated locations slice
func (dm *DocumentMatch) Complete(prealloc []Location) []Location {
// transform the FieldTermLocations slice into the Locations map
nlocs := len(dm.FieldTermLocations)
if nlocs > 0 {
if cap(prealloc) < nlocs {
prealloc = make([]Location, nlocs)
}
prealloc = prealloc[:nlocs]
var lastField string
var tlm TermLocationMap
var needsDedupe bool
for i, ftl := range dm.FieldTermLocations {
if lastField != ftl.Field {
lastField = ftl.Field
if dm.Locations == nil {
dm.Locations = make(FieldTermLocationMap)
}
tlm = dm.Locations[ftl.Field]
if tlm == nil {
tlm = make(TermLocationMap)
dm.Locations[ftl.Field] = tlm
}
}
loc := &prealloc[i]
*loc = ftl.Location
locs := tlm[ftl.Term]
// if the loc is before or at the last location, then there
// might be duplicates that need to be deduplicated
if !needsDedupe && len(locs) > 0 {
last := locs[len(locs)-1]
if loc.Pos <= last.Pos {
needsDedupe = true
}
}
tlm[ftl.Term] = append(locs, loc)
dm.FieldTermLocations[i] = FieldTermLocation{ // recycle
Location: Location{},
}
}
if needsDedupe {
for _, tlm := range dm.Locations {
for term, locs := range tlm {
tlm[term] = locs.Dedupe()
}
}
}
}
dm.FieldTermLocations = dm.FieldTermLocations[:0] // recycle
return prealloc
}
func (dm *DocumentMatch) String() string {
return fmt.Sprintf("[%d-%f]", dm.Number, dm.Score)
}
type DocumentMatchCollection []*DocumentMatch
func (c DocumentMatchCollection) Len() int { return len(c) }
func (c DocumentMatchCollection) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c DocumentMatchCollection) Less(i, j int) bool { return c[i].Score > c[j].Score }
type Searcher interface {
Next(ctx *Context) (*DocumentMatch, error)
Advance(ctx *Context, number uint64) (*DocumentMatch, error)
Close() error
Count() uint64
Min() int
Size() int
DocumentMatchPoolSize() int
}
type SearcherOptions struct {
SimilarityForField func(field string) Similarity
DefaultSearchField string
DefaultAnalyzer *analysis.Analyzer
Explain bool
IncludeTermVectors bool
Score string
}
// Context represents the context around a single search
type Context struct {
DocumentMatchPool *DocumentMatchPool
dvReaders map[DocumentValueReadable]segment.DocumentValueReader
}
func NewSearchContext(size, sortSize int) *Context {
return &Context{
DocumentMatchPool: NewDocumentMatchPool(size, sortSize),
dvReaders: make(map[DocumentValueReadable]segment.DocumentValueReader),
}
}
func (sc *Context) DocValueReaderForReader(r DocumentValueReadable, fields []string) (segment.DocumentValueReader, error) {
dvReader := sc.dvReaders[r]
if dvReader == nil {
var err error
dvReader, err = r.DocumentValueReader(fields)
if err != nil {
return nil, err
}
sc.dvReaders[r] = dvReader
}
return dvReader, nil
}
func (sc *Context) Size() int {
sizeInBytes := reflectStaticSizeSearchContext + sizeOfPtr +
reflectStaticSizeDocumentMatchPool + sizeOfPtr
if sc.DocumentMatchPool != nil {
for _, entry := range sc.DocumentMatchPool.avail {
if entry != nil {
sizeInBytes += entry.Size()
}
}
}
return sizeInBytes
}
type DocumentValueReadable interface {
// DocumentValueReader provides a way to find all of the document
// values stored in the specified fields. The returned
// DocumentValueReader provides a means to visit specific document
// numbers.
DocumentValueReader(fields []string) (segment.DocumentValueReader, error)
}
type StoredFieldVisitable interface {
// VisitStoredFields will call the visitor for each stored field
// of the specified document number.
VisitStoredFields(number uint64, visitor segment.StoredFieldVisitor) error
}
type MatchReader interface {
DocumentValueReadable
StoredFieldVisitable
}
type Reader interface {
DocumentValueReadable
StoredFieldVisitable
CollectionStats(field string) (segment.CollectionStats, error)
// DictionaryLookup provides a way to quickly determine if a term is
// in the dictionary for the specified field.
DictionaryLookup(field string) (segment.DictionaryLookup, error)
// DictionaryIterator provides a way to explore the terms used in the
// specified field. You can optionally filter these terms
// by the provided Automaton, or start/end terms.
DictionaryIterator(field string, automaton segment.Automaton, start,
end []byte) (segment.DictionaryIterator, error)
// PostingsIterator provides a way to find information about all documents
// that use the specified term in the specified field.
PostingsIterator(term []byte, field string, includeFreq, includeNorm,
includeTermVectors bool) (segment.PostingsIterator, error)
// Close releases all resources associated with this Reader
Close() error
}
type Similarity interface {
ComputeNorm(numTerms int) float32
Scorer(boost float64, collectionStats segment.CollectionStats, termStats segment.TermStats) Scorer
}
type Scorer interface {
Score(freq int, norm float64) float64
Explain(freq int, norm float64) *Explanation
}
type CompositeScorer interface {
ScoreComposite(constituents []*DocumentMatch) float64
ExplainComposite(constituents []*DocumentMatch) *Explanation
}