-
Notifications
You must be signed in to change notification settings - Fork 2
/
blockmaxconjunctionscorer.go
325 lines (271 loc) · 7.12 KB
/
blockmaxconjunctionscorer.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
package search
import (
"github.com/geange/lucene-go/core/types"
"io"
"sort"
)
var _ Scorer = &BlockMaxConjunctionScorer{}
type BlockMaxConjunctionScorer struct {
*BaseScorer
scorers []Scorer
approximations []types.DocIdSetIterator
twoPhases []TwoPhaseIterator
maxScorePropagator *MaxScoreSumPropagator
minScore float64
}
func NewBlockMaxConjunctionScorer(weight Weight, scorersList []Scorer) (*BlockMaxConjunctionScorer, error) {
res := &BlockMaxConjunctionScorer{
BaseScorer: NewScorer(weight),
scorers: scorersList,
approximations: make([]types.DocIdSetIterator, len(scorersList)),
twoPhases: nil,
maxScorePropagator: nil,
minScore: 0,
}
// Sort res by cost
sort.Sort(sortScorerByCost(res.scorers))
maxScorePropagator, err := NewMaxScoreSumPropagator(res.scorers)
if err != nil {
return nil, err
}
res.maxScorePropagator = maxScorePropagator
twoPhaseList := make([]TwoPhaseIterator, 0)
for i := range res.scorers {
scorer := res.scorers[i]
twoPhase := scorer.TwoPhaseIterator()
if twoPhase != nil {
twoPhaseList = append(twoPhaseList, twoPhase)
res.approximations[i] = twoPhase.Approximation()
} else {
res.approximations[i] = scorer.Iterator()
}
if _, err := scorer.AdvanceShallow(0); err != nil {
return nil, err
}
}
res.twoPhases = twoPhaseList
sort.Sort(sortTwoPhaseIteratorByMatchCost(res.twoPhases))
return res, nil
}
var _ sort.Interface = &sortScorerByCost{}
type sortScorerByCost []Scorer
func (s sortScorerByCost) Len() int {
return len(s)
}
func (s sortScorerByCost) Less(i, j int) bool {
return s[i].Iterator().Cost() < s[j].Iterator().Cost()
}
func (s sortScorerByCost) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
var _ sort.Interface = &sortTwoPhaseIteratorByMatchCost{}
type sortTwoPhaseIteratorByMatchCost []TwoPhaseIterator
func (s sortTwoPhaseIteratorByMatchCost) Len() int {
return len(s)
}
func (s sortTwoPhaseIteratorByMatchCost) Less(i, j int) bool {
return s[i].MatchCost() < s[j].MatchCost()
}
func (s sortTwoPhaseIteratorByMatchCost) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (b *BlockMaxConjunctionScorer) Score() (float64, error) {
score := 0.0
for _, scorer := range b.scorers {
num, err := scorer.Score()
if err != nil {
return 0, err
}
score += num
}
return score, nil
}
func (b *BlockMaxConjunctionScorer) DocID() int {
return b.scorers[0].DocID()
}
func (b *BlockMaxConjunctionScorer) Iterator() types.DocIdSetIterator {
if len(b.twoPhases) == 0 {
return b.approximation()
}
return AsDocIdSetIterator(b.TwoPhaseIterator())
}
func (b *BlockMaxConjunctionScorer) GetMaxScore(upTo int) (float64, error) {
sum := 0.0
for _, scorer := range b.scorers {
score, err := scorer.GetMaxScore(upTo)
if err != nil {
return 0, err
}
sum += score
}
return sum, nil
}
func (b *BlockMaxConjunctionScorer) SetMinCompetitiveScore(score float64) error {
b.minScore = score
return b.maxScorePropagator.SetMinCompetitiveScore(score)
}
func (b *BlockMaxConjunctionScorer) GetChildren() ([]ChildScorable, error) {
children := make([]ChildScorable, 0)
for _, scorer := range b.scorers {
scorables, err := scorer.GetChildren()
if err != nil {
return nil, err
}
children = append(children, scorables...)
}
return children, nil
}
func (b *BlockMaxConjunctionScorer) TwoPhaseIterator() TwoPhaseIterator {
if len(b.twoPhases) == 0 {
return nil
}
cost := 0.0
for _, phase := range b.twoPhases {
cost += phase.MatchCost()
}
approx := b.approximation()
return &bmcTwoPhaseIterator{
approx: approx,
matchCost: cost,
}
}
var _ TwoPhaseIterator = &bmcTwoPhaseIterator{}
type bmcTwoPhaseIterator struct {
approx types.DocIdSetIterator
matchCost float64
p *BlockMaxConjunctionScorer
}
func (b *bmcTwoPhaseIterator) Approximation() types.DocIdSetIterator {
return b.approx
}
func (b *bmcTwoPhaseIterator) Matches() (bool, error) {
for _, twoPhase := range b.p.twoPhases {
if ok, err := twoPhase.Matches(); err != nil {
return false, err
} else if !ok {
return false, nil
}
}
return true, nil
}
func (b *bmcTwoPhaseIterator) MatchCost() float64 {
return b.matchCost
}
func (b *BlockMaxConjunctionScorer) approximation() types.DocIdSetIterator {
lead := b.approximations[0]
return &bmcDocIdSetIterator{
maxScore: 0,
upTo: -1,
lead: lead,
p: b,
}
}
var _ types.DocIdSetIterator = &bmcDocIdSetIterator{}
type bmcDocIdSetIterator struct {
maxScore float64
upTo int
lead types.DocIdSetIterator
p *BlockMaxConjunctionScorer
}
func (b *bmcDocIdSetIterator) DocID() int {
return b.lead.DocID()
}
func (b *bmcDocIdSetIterator) NextDoc() (int, error) {
return b.Advance(b.DocID() + 1)
}
func (b *bmcDocIdSetIterator) Advance(target int) (int, error) {
advanceTarget, err := b.advanceTarget(target)
if err != nil {
return 0, err
}
advance, err := b.lead.Advance(advanceTarget)
if err != nil {
return 0, err
}
return b.doNext(advance)
}
func (b *bmcDocIdSetIterator) doNext(doc int) (int, error) {
advanceHead:
for {
if doc == types.NO_MORE_DOCS {
return types.NO_MORE_DOCS, io.EOF
}
if doc > b.upTo {
// This check is useful when scorers return information about blocks
// that do not actually have any matches. Otherwise `doc` will always
// be in the current block already since it is always the result of
// lead.advance(advanceTarget(some_doc_id))
nextTarget, err := b.advanceTarget(doc)
if err != nil {
return 0, err
}
if nextTarget != doc {
doc, err = b.lead.Advance(nextTarget)
if err != nil {
return 0, err
}
continue
}
}
// then find agreement with other iterators
for i := 0; i < len(b.p.approximations); i++ {
other := b.p.approximations[i]
// other.doc may already be equal to doc if we "continued advanceHead"
// on the previous iteration and the advance on the lead scorer exactly matched.
if other.DocID() < doc {
next, err := other.Advance(doc)
if err != nil {
return 0, err
}
if next > doc {
// iterator beyond the current doc - advance lead and continue to the new highest doc.
advanceTarget, err := b.advanceTarget(next)
if err != nil {
return 0, err
}
doc, err = b.lead.Advance(advanceTarget)
if err != nil {
return 0, err
}
continue advanceHead
}
}
}
return doc, nil
}
}
func (b *bmcDocIdSetIterator) SlowAdvance(target int) (int, error) {
return types.SlowAdvance(b, target)
}
func (b *bmcDocIdSetIterator) Cost() int64 {
return b.lead.Cost()
}
func (b *bmcDocIdSetIterator) moveToNextBlock(target int) error {
upTo, err := b.p.AdvanceShallow(target)
if err != nil {
return err
}
b.upTo = upTo
maxScore, err := b.p.GetMaxScore(b.upTo)
if err != nil {
return err
}
b.maxScore = maxScore
return nil
}
func (b *bmcDocIdSetIterator) advanceTarget(target int) (int, error) {
if target > b.upTo {
if err := b.moveToNextBlock(target); err != nil {
return 0, err
}
}
for {
if b.maxScore >= b.p.minScore {
return target, nil
}
target = b.upTo + 1
if err := b.moveToNextBlock(target); err != nil {
return 0, err
}
}
}