-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpostings.go
229 lines (177 loc) · 3.85 KB
/
postings.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
package postings
import (
"sort"
)
// https://blog.twitter.com/2016/omnisearch-index-formats
// https://www.umiacs.umd.edu/~jimmylin/publications/Busch_etal_ICDE2012.pdf
// http://www.slideshare.net/lucenerevolution/twitter-search-lucenerevolutioneu2013-copy
type DocID uint32
type TermID uint32
type Index struct {
p map[TermID]Postings
nextDocID DocID
stop map[TermID]struct{}
}
func NewIndex(stop []TermID) *Index {
p := make(map[TermID]Postings)
var mstop map[TermID]struct{}
if len(stop) > 0 {
mstop = make(map[TermID]struct{}, len(stop))
for _, s := range stop {
mstop[s] = struct{}{}
}
}
return &Index{p: p, stop: mstop}
}
func (idx *Index) AddDocument(terms []TermID) DocID {
id := idx.nextDocID
for _, t := range terms {
if _, ok := idx.stop[t]; ok {
continue
}
idxt := idx.p[t]
if len(idxt) == 0 || idxt[len(idxt)-1] != id {
idx.p[t] = append(idx.p[t], id)
}
}
idx.nextDocID++
return id
}
func (idx *Index) Postings(t TermID) (Iterator, int) {
p := idx.p[t]
return newIter(p), len(p)
}
type tfList struct {
terms []TermID
freq []int
iters []Iterator
}
func (tf tfList) Len() int { return len(tf.terms) }
func (tf tfList) Swap(i, j int) {
tf.terms[i], tf.terms[j] = tf.terms[j], tf.terms[i]
tf.freq[i], tf.freq[j] = tf.freq[j], tf.freq[i]
tf.iters[i], tf.iters[j] = tf.iters[j], tf.iters[i]
}
func (tf tfList) Less(i, j int) bool { return tf.freq[i] < tf.freq[j] }
type InvertedIndex interface {
Postings(t TermID) (Iterator, int)
}
// Query returns a list of postings that match the terms
func Query(idx InvertedIndex, ts []TermID) Postings {
if len(ts) == 1 {
iter, f := idx.Postings(ts[0])
result := make(Postings, 0, f)
for !iter.end() {
result = append(result, iter.at())
iter.next()
}
return result
}
freq := make([]int, len(ts))
terms := make([]TermID, len(ts))
iters := make([]Iterator, len(ts))
for i, t := range ts {
d, f := idx.Postings(t)
if d.end() {
return nil
}
terms[i] = t
freq[i] = f
iters[i] = d
}
tf := tfList{terms, freq, iters}
sort.Sort(tf)
var docs Iterator = iters[0]
result := make(Postings, freq[0])
for _, t := range iters[1:] {
result = intersect(result[:0], docs, t)
if len(result) == 0 {
return nil
}
docs = newIter(result)
}
return result
}
// Postings is a list of documents
type Postings []DocID
// piter is a posting list iterator
type piter struct {
list Postings
idx int
}
func newIter(l Postings) *piter {
return &piter{list: l}
}
func (it *piter) next() bool {
it.idx++
return !it.end()
}
func (it *piter) advance(d DocID) bool {
// galloping search
bound := 1
for it.idx+bound < len(it.list) && d > it.list[it.idx+bound] {
bound *= 2
}
// inlined binary search between the last two steps
n := d
low, high := it.idx+bound/2, it.idx+bound
if high > len(it.list) {
high = len(it.list)
}
for low < high {
mid := low + (high-low)/2
if it.list[mid] >= n {
high = mid
} else {
low = mid + 1
}
}
it.idx = low
return !it.end()
}
func (it *piter) end() bool {
return it.idx >= len(it.list)
}
func (it *piter) at() DocID {
return it.list[it.idx]
}
type Iterator interface {
at() DocID
end() bool
advance(DocID) bool
next() bool
}
// intersect returns the intersection of two posting lists
// postings are returned deduplicated.
func intersect(result Postings, ait, bit Iterator) Postings {
scan:
for !ait.end() && !bit.end() {
for ait.at() == bit.at() {
result = append(result, bit.at())
var d DocID
d = ait.at()
for ait.at() == d {
if !ait.next() {
break scan
}
}
d = bit.at()
for bit.at() == d {
if !bit.next() {
break scan
}
}
}
for ait.at() < bit.at() {
if !ait.advance(bit.at()) {
break scan
}
}
for !bit.end() && ait.at() > bit.at() {
if !bit.advance(ait.at()) {
break scan
}
}
}
return result
}