-
Notifications
You must be signed in to change notification settings - Fork 2
/
topdocscollector.go
151 lines (127 loc) · 5.72 KB
/
topdocscollector.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
package search
import (
"github.com/geange/lucene-go/core/util/structure"
)
// TopDocsCollector
// A base class for all collectors that return a TopDocs output. This collector allows easy extension
// by providing a single constructor which accepts a PriorityQueue as well as protected members for
// that priority queue and a counter of the number of total hits. Extending classes can override any
// of the methods to provide their own implementation, as well as avoid the use of the priority queue
// entirely by passing null to TopDocsCollector(PriorityQueue). In that case however, you might want
// to consider overriding all methods, in order to avoid a NullPointerException.
type TopDocsCollector interface {
Collector
// PopulateResults
// Populates the results array with the ScoreDoc instances.
// This can be overridden in case a different ScoreDoc type should be returned.
PopulateResults(results []ScoreDoc, howMany int) error
// NewTopDocs
// Returns a TopDocs instance containing the given results.
// If results is null it means there are no results to return, either because
// there were 0 calls to collect() or because the arguments to topDocs were invalid.
NewTopDocs(results []ScoreDoc, howMany int) (TopDocs, error)
// GetTotalHits
// The total number of documents that matched this query.
GetTotalHits() int
// TopDocsSize
// The number of valid PQ entries
TopDocsSize() int
// TopDocs
// Returns the top docs that were collected by this collector.
TopDocs() (TopDocs, error)
// TopDocsFrom
// Returns the documents in the range [start .. pq.size()) that were collected by this collector.
// Note that if start >= pq.size(), an empty TopDocs is returned. This method is convenient to
// call if the application always asks for the last results, starting from the last 'page'.
// NOTE: you cannot call this method more than once for each search execution.
// If you need to call it more than once, passing each time a different start,
// you should call topDocs() and work with the returned TopDocs object,
// which will contain all the results this search execution collected.
TopDocsFrom(start int) (TopDocs, error)
// TopDocsRange
// Returns the documents in the range [start .. start+howMany) that were collected by this collector.
// Note that if start >= pq.size(), an empty TopDocs is returned, and if pq.size() - start < howMany,
// then only the available documents in [start .. pq.size()) are returned.
// This method is useful to call in case pagination of search results is allowed by the search application,
// as well as it attempts to optimize the memory used by allocating only as much as requested by howMany.
// NOTE: you cannot call this method more than once for each search execution.
// If you need to call it more than once, passing each time a different range,
// you should call topDocs() and work with the returned TopDocs object,
// which will contain all the results this search execution collected.
TopDocsRange(start, howMany int) (TopDocs, error)
}
var EMPTY_TOPDOCS = &BaseTopDocs{
totalHits: NewTotalHits(0, EQUAL_TO),
scoreDocs: make([]ScoreDoc, 0),
}
type TopDocsCollectorDefault[T ScoreDoc] struct {
pq *structure.PriorityQueue[T]
totalHits int
totalHitsRelation TotalHitsRelation
}
func newTopDocsCollectorDefault[T ScoreDoc](pq *structure.PriorityQueue[T]) *TopDocsCollectorDefault[T] {
return &TopDocsCollectorDefault[T]{pq: pq}
}
func (t *TopDocsCollectorDefault[T]) PopulateResults(results []ScoreDoc, howMany int) error {
for i := howMany - 1; i >= 0; i-- {
n, err := t.pq.Pop()
if err != nil {
return err
}
results[i] = n
}
return nil
}
func (t *TopDocsCollectorDefault[T]) NewTopDocs(results []ScoreDoc, howMany int) (TopDocs, error) {
if len(results) == 0 {
return EMPTY_TOPDOCS, nil
}
return NewTopDocs(NewTotalHits(int64(t.totalHits), t.totalHitsRelation), results), nil
}
func (t *TopDocsCollectorDefault[T]) GetTotalHits() int {
return t.totalHits
}
func (t *TopDocsCollectorDefault[T]) TopDocsSize() int {
// In case pq was populated with sentinel values, there might be less
// results than pq.size(). Therefore return all results until either
// pq.size() or totalHits.
if t.totalHits < t.pq.Size() {
return t.totalHits
}
return t.pq.Size()
}
func (t *TopDocsCollectorDefault[T]) TopDocs() (TopDocs, error) {
return t.TopDocsRange(0, t.TopDocsSize())
}
func (t *TopDocsCollectorDefault[T]) TopDocsFrom(start int) (TopDocs, error) {
return t.TopDocsRange(start, t.TopDocsSize())
}
func (t *TopDocsCollectorDefault[T]) TopDocsRange(start, howMany int) (TopDocs, error) {
// In case pq was populated with sentinel values, there might be less
// results than pq.size(). Therefore return all results until either
// pq.size() or totalHits.
size := t.TopDocsSize()
// Don't bother to throw an exception, just return an empty TopDocs in case
// the parameters are invalid or out of range.
// TODO: shouldn't we throw IAE if apps give bad params here so they dont
// have sneaky silent bugs?
if start < 0 || start >= size || howMany <= 0 {
return t.NewTopDocs(nil, start)
}
// We know that start < pqsize, so just fix howMany.
howMany = min(size-start, howMany)
results := make([]ScoreDoc, howMany)
// pq's pop() returns the 'least' element in the queue, therefore need
// to discard the first ones, until we reach the requested range.
// Note that this loop will usually not be executed, since the common usage
// should be that the caller asks for the last howMany results. However it's
// needed here for completeness.
for i := t.pq.Size() - start - howMany; i > 0; i-- {
t.pq.Pop()
}
// Get the requested results from pq.
if err := t.PopulateResults(results, howMany); err != nil {
return nil, err
}
return t.NewTopDocs(results, start)
}