-
Notifications
You must be signed in to change notification settings - Fork 0
/
sour.go
241 lines (206 loc) · 5.95 KB
/
sour.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
// Copyright (c) 2021 The Bluge Authors.
//
// 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 sour
import (
"context"
"fmt"
"sort"
"github.com/blugelabs/bluge"
"github.com/blugelabs/bluge/analysis"
"github.com/blugelabs/bluge/search"
segment "github.com/blugelabs/bluge_segment_api"
)
const internalDocNumber uint64 = 0
type Sour struct {
cfg bluge.Config
doc *bluge.Document
// always built during analysis
fieldIndexes map[string]int
fieldNames []string
fieldTokenFreqs []analysis.TokenFrequencies
fieldLens []int
// deferred build and cache
sortedTerms map[string][]string
}
func New(cfg bluge.Config) *Sour {
return &Sour{
cfg: cfg,
sortedTerms: make(map[string][]string),
}
}
func NewWithDocument(cfg bluge.Config, doc *bluge.Document) *Sour {
rv := New(cfg)
rv.Reset(doc)
return rv
}
func (s *Sour) Search(ctx context.Context, req bluge.SearchRequest) (search.DocumentMatchIterator, error) {
collector := req.Collector()
searcher, err := req.Searcher(s, s.cfg)
if err != nil {
return nil, err
}
return collector.Collect(ctx, req.Aggregations(), searcher)
}
func (s *Sour) DocumentValueReader(fields []string) (segment.DocumentValueReader, error) {
return &DocValueReader{
s: s,
fields: fields,
}, nil
}
func (s *Sour) VisitStoredFields(number uint64, visitor segment.StoredFieldVisitor) error {
// FIXME no stored fields
return nil
}
func (s *Sour) CollectionStats(field string) (segment.CollectionStats, error) {
return &collStats, nil
}
func (s *Sour) DictionaryLookup(field string) (segment.DictionaryLookup, error) {
if s.doc == nil {
return fieldDictContainsEmpty, nil
}
atf, _, err := s.TokenFreqsAndLen(field)
if err != nil {
// only error is field doesn't exist in doc
return fieldDictContainsEmpty, nil
}
return &Dictionary{
s: s,
field: field,
atf: atf,
}, nil
}
func automatonMatch(la segment.Automaton, termStr string) bool {
state := la.Start()
for i := range []byte(termStr) {
state = la.Accept(state, termStr[i])
if !la.CanMatch(state) {
return false
}
}
return la.IsMatch(state)
}
// 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.
func (s *Sour) DictionaryIterator(field string, automaton segment.Automaton, start,
end []byte) (segment.DictionaryIterator, error) {
if s.doc == nil {
return fieldDictEmpty, nil
}
fieldSortedTerms, err := s.SortedTermsForField(field)
if err != nil {
// only error is field doesn't exist in doc
return fieldDictEmpty, nil
}
return NewDictionaryIteratorWithTerms(fieldSortedTerms, func(s string) bool {
return automatonMatch(automaton, s)
}), nil
}
// PostingsIterator provides a way to find information about all documents
// that use the specified term in the specified field.
func (s *Sour) PostingsIterator(term []byte, field string, includeFreq, includeNorm,
includeTermVectors bool) (segment.PostingsIterator, error) {
if s.doc == nil {
return termFieldReaderEmpty, nil
}
atf, l, err := s.TokenFreqsAndLen(field)
if err != nil {
// only error is field doesn't exist in doc
return termFieldReaderEmpty, nil
}
tf, ok := atf[string(term)]
if !ok {
return termFieldReaderEmpty, nil
}
return NewTermFieldReaderFromTokenFreqAndLen(tf, l, includeFreq, includeNorm, includeTermVectors), nil
}
func (s *Sour) Close() error {
return nil
}
// Add ***********
func (s *Sour) newField(field bluge.Field) {
af := field.AnalyzedTokenFrequencies()
// bleve analysis will leave field empty for non-composite fields, fix that here
for _, tf := range af {
for _, loc := range tf.Locations {
if loc.FieldVal == "" {
loc.FieldVal = field.Name()
}
}
}
fieldIdx, exists := s.fieldIndexes[field.Name()]
if !exists {
s.fieldIndexes[field.Name()] = len(s.fieldTokenFreqs)
s.fieldNames = append(s.fieldNames, field.Name())
s.fieldTokenFreqs = append(s.fieldTokenFreqs, af)
s.fieldLens = append(s.fieldLens, field.Length())
} else {
s.fieldTokenFreqs[fieldIdx].MergeAll(field.Name(), af)
s.fieldLens[fieldIdx] += field.Length()
}
}
func (s *Sour) analyze() {
// let bluge do analysis
s.doc.Analyze()
for _, field := range *s.doc {
s.newField(field)
}
}
func (s *Sour) Reset(doc *bluge.Document) {
// clear analysis
s.fieldIndexes = make(map[string]int, len(s.fieldNames))
s.fieldNames = s.fieldNames[:0]
s.fieldTokenFreqs = s.fieldTokenFreqs[:0]
s.fieldLens = s.fieldLens[:0]
// clear cache
for k := range s.sortedTerms {
s.sortedTerms[k] = s.sortedTerms[k][:0]
}
// init new doc
s.doc = doc
s.analyze()
}
func (s *Sour) fieldIndex(name string) (int, error) {
if idx, ok := s.fieldIndexes[name]; ok {
return idx, nil
}
return 0, fmt.Errorf("no field named: %s", name)
}
func (s *Sour) Fields() []string {
return s.fieldNames
}
func (s *Sour) SortedTermsForField(fieldName string) ([]string, error) {
fieldIdx, err := s.fieldIndex(fieldName)
if err != nil {
return nil, err
}
terms, ok := s.sortedTerms[fieldName]
if ok && len(terms) > 0 {
return terms, nil
}
atf := s.fieldTokenFreqs[fieldIdx]
for k := range atf {
terms = append(terms, k)
}
sort.Strings(terms)
s.sortedTerms[fieldName] = terms
return terms, nil
}
func (s *Sour) TokenFreqsAndLen(fieldName string) (analysis.TokenFrequencies, int, error) {
fieldIdx, err := s.fieldIndex(fieldName)
if err != nil {
return nil, 0, err
}
return s.fieldTokenFreqs[fieldIdx], s.fieldLens[fieldIdx], nil
}