-
Notifications
You must be signed in to change notification settings - Fork 3
/
doc_test.go
243 lines (207 loc) · 5.66 KB
/
doc_test.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
// Copyright (c) 2021 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 sear
import (
"reflect"
"strings"
"testing"
index "github.com/blevesearch/bleve_index_api"
)
func TestDocument(t *testing.T) {
// the document obj we test with
doc := NewDocument()
// start with simple doc
bleveDoc := newTestDoc("a")
bleveDoc.AddField(newTestField("name", []byte("marty")))
doc.Reset(bleveDoc)
// assert we see expected field
assertAllAndOnlyValues(t, []string{"name", "_all"}, doc.Fields())
// now reset to new document
bleveDoc = newTestDoc("b")
bleveDoc.AddField(newTestField("project", []byte("bleve")))
doc.Reset(bleveDoc)
// assert we see expected field
assertAllAndOnlyValues(t, []string{"project", "_all"}, doc.Fields())
// now reset to more realistic document
bleveDoc = newTestDoc("c")
bleveDoc.AddField(newTestField("username", []byte("mather")))
bleveDoc.AddField(newTestField("description", []byte("phrase bleve bleve bleve")))
doc.Reset(bleveDoc)
tfs, l, err := doc.TokenFreqsAndLen("description")
if err != nil {
t.Fatal(err)
}
if l != 4 {
t.Errorf("expected description field len 4, got %d", l)
}
if len(tfs) != 2 {
t.Errorf("expected description tfs to be len 2, got %d", len(tfs))
t.Logf("%#v", tfs)
}
for term, tf := range tfs {
switch term {
case "phrase":
if tf.Frequency() != 1 {
t.Errorf("expected 'phrase' to freq 1, got %d", tf.Frequency())
}
case "bleve":
if tf.Frequency() != 3 {
t.Errorf("expected 'phrase' to freq 3, got %d", tf.Frequency())
}
}
}
// expect field names not to be sorted yet
if len(doc.sortedTerms) > 0 {
t.Errorf("expected no sorted terms yet, got %#v", doc.sortedTerms)
}
// get the sorted terms
st, err := doc.SortedTermsForField("description")
if err != nil {
t.Fatal(err)
}
expectedSortedTerms := []string{"bleve", "phrase"}
if !reflect.DeepEqual(st, expectedSortedTerms) {
t.Errorf("expected sorted terms: %v, got %v", expectedSortedTerms, st)
}
if len(doc.sortedTerms) != 1 {
t.Errorf("expected 1 cached sorted terms, got %#v", doc.sortedTerms)
}
}
type testDoc struct {
id string
fields []index.Field
composites []index.CompositeField
}
func newTestDoc(id string) *testDoc {
return &testDoc{
id: id,
composites: []index.CompositeField{
&testField{
name: "_all",
analyzedTokenFreqs: make(index.TokenFrequencies),
},
},
}
}
func (t *testDoc) AddField(f index.Field) {
t.fields = append(t.fields, f)
}
func (t *testDoc) ID() string {
return t.id
}
func (t *testDoc) Size() int {
return 0
}
func (t *testDoc) VisitFields(visitor index.FieldVisitor) {
for _, field := range t.fields {
visitor(field)
}
}
func (t *testDoc) VisitComposite(visitor index.CompositeFieldVisitor) {
for _, composite := range t.composites {
visitor(composite)
}
}
func (t *testDoc) HasComposite() bool {
return len(t.composites) > 0
}
func (t *testDoc) NumPlainTextBytes() uint64 {
return 0
}
func (t *testDoc) AddIDField() {}
func (t *testDoc) StoredFieldsBytes() uint64 {
return 0
}
type testField struct {
name string
val []byte
ap []uint64
options index.FieldIndexingOptions
analyzedLen int
analyzedTokenFreqs index.TokenFrequencies
}
func newTestField(name string, val []byte) *testField {
return &testField{
name: name,
val: val,
ap: []uint64{},
options: index.IndexField | index.DocValues,
analyzedTokenFreqs: make(index.TokenFrequencies),
}
}
func (t *testField) Name() string {
return t.name
}
func (t *testField) Value() []byte {
return t.val
}
func (t *testField) ArrayPositions() []uint64 {
return t.ap
}
func (t *testField) EncodedFieldType() byte {
return 't'
}
func (t *testField) Analyze() {
tokens := strings.Split(string(t.val), " ")
var currPos int
for i, token := range tokens {
tokenLower := strings.ToLower(token)
if i != 0 {
currPos++ // space
}
tf, ok := t.analyzedTokenFreqs[tokenLower]
if ok {
tf.SetFrequency(tf.Frequency() + 1)
tf.Locations = append(tf.Locations, &index.TokenLocation{
Field: t.name,
ArrayPositions: t.ap,
Start: currPos,
End: currPos + len(tokenLower),
Position: i + 1,
})
} else {
t.analyzedTokenFreqs[tokenLower] = &index.TokenFreq{
Term: []byte(tokenLower),
Locations: []*index.TokenLocation{
{
Field: t.name,
ArrayPositions: t.ap,
Start: currPos,
End: currPos + len(tokenLower),
Position: i + 1,
},
},
}
t.analyzedTokenFreqs[tokenLower].SetFrequency(1)
}
currPos += len(tokenLower)
}
t.analyzedLen = len(tokens)
}
func (t *testField) Options() index.FieldIndexingOptions {
return t.options
}
func (t *testField) AnalyzedLength() int {
return t.analyzedLen
}
func (t *testField) AnalyzedTokenFrequencies() index.TokenFrequencies {
return t.analyzedTokenFreqs
}
func (t *testField) NumPlainTextBytes() uint64 {
return 0
}
func (t *testField) Compose(field string, length int, freq index.TokenFrequencies) {
t.analyzedLen += length
t.analyzedTokenFreqs.MergeAll(field, freq)
}