| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package related |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"fmt" |
| 19 |
"math/rand" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
qt "github.com/frankban/quicktest" |
| 25 |
"github.com/gohugoio/hugo/config" |
| 26 |
) |
| 27 |
|
| 28 |
type testDoc struct { |
| 29 |
keywords map[string][]Keyword |
| 30 |
date time.Time |
| 31 |
name string |
| 32 |
} |
| 33 |
|
| 34 |
func (d *testDoc) String() string { |
| 35 |
var s strings.Builder |
| 36 |
s.WriteString("\n") |
| 37 |
for k, v := range d.keywords { |
| 38 |
s.WriteString(k + ":\t\t") |
| 39 |
for _, vv := range v { |
| 40 |
s.WriteString(" " + vv.String()) |
| 41 |
} |
| 42 |
s.WriteString("\n") |
| 43 |
} |
| 44 |
return s.String() |
| 45 |
} |
| 46 |
|
| 47 |
func (d *testDoc) Name() string { |
| 48 |
return d.name |
| 49 |
} |
| 50 |
|
| 51 |
func newTestDoc(name string, keywords ...string) *testDoc { |
| 52 |
time.Sleep(1 * time.Millisecond) |
| 53 |
return newTestDocWithDate(name, time.Now(), keywords...) |
| 54 |
} |
| 55 |
|
| 56 |
func newTestDocWithDate(name string, date time.Time, keywords ...string) *testDoc { |
| 57 |
km := make(map[string][]Keyword) |
| 58 |
|
| 59 |
kw := &testDoc{keywords: km, date: date} |
| 60 |
|
| 61 |
kw.addKeywords(name, keywords...) |
| 62 |
return kw |
| 63 |
} |
| 64 |
|
| 65 |
func (d *testDoc) addKeywords(name string, keywords ...string) *testDoc { |
| 66 |
keywordm := createTestKeywords(name, keywords...) |
| 67 |
|
| 68 |
for k, v := range keywordm { |
| 69 |
keywords := make([]Keyword, len(v)) |
| 70 |
for i := range v { |
| 71 |
keywords[i] = StringKeyword(v[i]) |
| 72 |
} |
| 73 |
d.keywords[k] = keywords |
| 74 |
} |
| 75 |
return d |
| 76 |
} |
| 77 |
|
| 78 |
func createTestKeywords(name string, keywords ...string) map[string][]string { |
| 79 |
return map[string][]string{ |
| 80 |
name: keywords, |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func (d *testDoc) RelatedKeywords(cfg IndexConfig) ([]Keyword, error) { |
| 85 |
return d.keywords[cfg.Name], nil |
| 86 |
} |
| 87 |
|
| 88 |
func (d *testDoc) PublishDate() time.Time { |
| 89 |
return d.date |
| 90 |
} |
| 91 |
|
| 92 |
func TestCardinalityThreshold(t *testing.T) { |
| 93 |
c := qt.New(t) |
| 94 |
config := Config{ |
| 95 |
Threshold: 90, |
| 96 |
IncludeNewer: false, |
| 97 |
Indices: IndicesConfig{ |
| 98 |
IndexConfig{Name: "tags", Weight: 50, CardinalityThreshold: 79}, |
| 99 |
IndexConfig{Name: "keywords", Weight: 65, CardinalityThreshold: 90}, |
| 100 |
}, |
| 101 |
} |
| 102 |
|
| 103 |
idx := NewInvertedIndex(config) |
| 104 |
hasKeyword := func(index, keyword string) bool { |
| 105 |
_, found := idx.index[index][StringKeyword(keyword)] |
| 106 |
return found |
| 107 |
} |
| 108 |
|
| 109 |
docs := []Document{ |
| 110 |
newTestDoc("tags", "a", "b", "c", "d"), |
| 111 |
newTestDoc("tags", "b", "d", "g"), |
| 112 |
newTestDoc("tags", "b", "d", "g"), |
| 113 |
newTestDoc("tags", "b", "h").addKeywords("keywords", "a"), |
| 114 |
newTestDoc("tags", "g", "h").addKeywords("keywords", "a", "b", "z"), |
| 115 |
} |
| 116 |
|
| 117 |
idx.Add(context.Background(), docs...) |
| 118 |
c.Assert(idx.Finalize(context.Background()), qt.IsNil) |
| 119 |
// Only tags=b should be removed. |
| 120 |
c.Assert(hasKeyword("tags", "a"), qt.Equals, true) |
| 121 |
c.Assert(hasKeyword("tags", "b"), qt.Equals, false) |
| 122 |
c.Assert(hasKeyword("tags", "d"), qt.Equals, true) |
| 123 |
c.Assert(hasKeyword("keywords", "b"), qt.Equals, true) |
| 124 |
} |
| 125 |
|
| 126 |
func TestSearch(t *testing.T) { |
| 127 |
config := Config{ |
| 128 |
Threshold: 90, |
| 129 |
IncludeNewer: false, |
| 130 |
Indices: IndicesConfig{ |
| 131 |
IndexConfig{Name: "tags", Weight: 50}, |
| 132 |
IndexConfig{Name: "keywords", Weight: 65}, |
| 133 |
}, |
| 134 |
} |
| 135 |
|
| 136 |
idx := NewInvertedIndex(config) |
| 137 |
// idx.debug = true |
| 138 |
|
| 139 |
docs := []Document{ |
| 140 |
newTestDoc("tags", "a", "b", "c", "d"), |
| 141 |
newTestDoc("tags", "b", "d", "g"), |
| 142 |
newTestDoc("tags", "b", "h").addKeywords("keywords", "a"), |
| 143 |
newTestDoc("tags", "g", "h").addKeywords("keywords", "a", "b"), |
| 144 |
} |
| 145 |
|
| 146 |
idx.Add(context.Background(), docs...) |
| 147 |
|
| 148 |
t.Run("count", func(t *testing.T) { |
| 149 |
c := qt.New(t) |
| 150 |
c.Assert(len(idx.index), qt.Equals, 2) |
| 151 |
set1, found := idx.index["tags"] |
| 152 |
c.Assert(found, qt.Equals, true) |
| 153 |
// 6 tags |
| 154 |
c.Assert(len(set1), qt.Equals, 6) |
| 155 |
|
| 156 |
set2, found := idx.index["keywords"] |
| 157 |
c.Assert(found, qt.Equals, true) |
| 158 |
c.Assert(len(set2), qt.Equals, 2) |
| 159 |
}) |
| 160 |
|
| 161 |
t.Run("search-tags", func(t *testing.T) { |
| 162 |
c := qt.New(t) |
| 163 |
var cfg IndexConfig |
| 164 |
m, err := idx.search(context.Background(), newQueryElement("tags", cfg.StringsToKeywords("a", "b", "d", "z")...)) |
| 165 |
c.Assert(err, qt.IsNil) |
| 166 |
c.Assert(len(m), qt.Equals, 2) |
| 167 |
c.Assert(m[0], qt.Equals, docs[0]) |
| 168 |
c.Assert(m[1], qt.Equals, docs[1]) |
| 169 |
}) |
| 170 |
|
| 171 |
t.Run("search-tags-and-keywords", func(t *testing.T) { |
| 172 |
c := qt.New(t) |
| 173 |
var cfg IndexConfig |
| 174 |
m, err := idx.search(context.Background(), |
| 175 |
newQueryElement("tags", cfg.StringsToKeywords("a", "b", "z")...), |
| 176 |
newQueryElement("keywords", cfg.StringsToKeywords("a", "b")...)) |
| 177 |
c.Assert(err, qt.IsNil) |
| 178 |
c.Assert(len(m), qt.Equals, 3) |
| 179 |
c.Assert(m[0], qt.Equals, docs[3]) |
| 180 |
c.Assert(m[1], qt.Equals, docs[2]) |
| 181 |
c.Assert(m[2], qt.Equals, docs[0]) |
| 182 |
}) |
| 183 |
|
| 184 |
t.Run("searchdoc-all", func(t *testing.T) { |
| 185 |
c := qt.New(t) |
| 186 |
doc := newTestDoc("tags", "a").addKeywords("keywords", "a") |
| 187 |
m, err := idx.Search(context.Background(), SearchOpts{Document: doc}) |
| 188 |
c.Assert(err, qt.IsNil) |
| 189 |
c.Assert(len(m), qt.Equals, 2) |
| 190 |
c.Assert(m[0], qt.Equals, docs[3]) |
| 191 |
c.Assert(m[1], qt.Equals, docs[2]) |
| 192 |
}) |
| 193 |
|
| 194 |
t.Run("searchdoc-tags", func(t *testing.T) { |
| 195 |
c := qt.New(t) |
| 196 |
doc := newTestDoc("tags", "a", "b", "d", "z").addKeywords("keywords", "a", "b") |
| 197 |
m, err := idx.Search(context.Background(), SearchOpts{Document: doc, Indices: []string{"tags"}}) |
| 198 |
c.Assert(err, qt.IsNil) |
| 199 |
c.Assert(len(m), qt.Equals, 2) |
| 200 |
c.Assert(m[0], qt.Equals, docs[0]) |
| 201 |
c.Assert(m[1], qt.Equals, docs[1]) |
| 202 |
}) |
| 203 |
|
| 204 |
t.Run("searchdoc-keywords-date", func(t *testing.T) { |
| 205 |
c := qt.New(t) |
| 206 |
doc := newTestDoc("tags", "a", "b", "d", "z").addKeywords("keywords", "a", "b") |
| 207 |
// This will get a date newer than the others. |
| 208 |
newDoc := newTestDoc("keywords", "a", "b") |
| 209 |
idx.Add(context.Background(), newDoc) |
| 210 |
|
| 211 |
m, err := idx.Search(context.Background(), SearchOpts{Document: doc, Indices: []string{"keywords"}}) |
| 212 |
c.Assert(err, qt.IsNil) |
| 213 |
c.Assert(len(m), qt.Equals, 2) |
| 214 |
c.Assert(m[0], qt.Equals, docs[3]) |
| 215 |
}) |
| 216 |
|
| 217 |
t.Run("searchdoc-keywords-same-date", func(t *testing.T) { |
| 218 |
c := qt.New(t) |
| 219 |
idx := NewInvertedIndex(config) |
| 220 |
|
| 221 |
date := time.Now() |
| 222 |
|
| 223 |
doc := newTestDocWithDate("keywords", date, "a", "b") |
| 224 |
doc.name = "thedoc" |
| 225 |
|
| 226 |
for i := range 10 { |
| 227 |
docc := *doc |
| 228 |
docc.name = fmt.Sprintf("doc%d", i) |
| 229 |
idx.Add(context.Background(), &docc) |
| 230 |
} |
| 231 |
|
| 232 |
m, err := idx.Search(context.Background(), SearchOpts{Document: doc, Indices: []string{"keywords"}}) |
| 233 |
c.Assert(err, qt.IsNil) |
| 234 |
c.Assert(len(m), qt.Equals, 10) |
| 235 |
for i := range 10 { |
| 236 |
c.Assert(m[i].Name(), qt.Equals, fmt.Sprintf("doc%d", i)) |
| 237 |
} |
| 238 |
}) |
| 239 |
} |
| 240 |
|
| 241 |
func TestToKeywordsToLower(t *testing.T) { |
| 242 |
c := qt.New(t) |
| 243 |
slice := []string{"A", "B", "C"} |
| 244 |
config := IndexConfig{ToLower: true} |
| 245 |
keywords, err := config.ToKeywords(slice) |
| 246 |
c.Assert(err, qt.IsNil) |
| 247 |
c.Assert(slice, qt.DeepEquals, []string{"A", "B", "C"}) |
| 248 |
c.Assert(keywords, qt.DeepEquals, []Keyword{ |
| 249 |
StringKeyword("a"), |
| 250 |
StringKeyword("b"), |
| 251 |
StringKeyword("c"), |
| 252 |
}) |
| 253 |
} |
| 254 |
|
| 255 |
func TestDecodeConfig(t *testing.T) { |
| 256 |
c := qt.New(t) |
| 257 |
|
| 258 |
configToml := ` |
| 259 |
[related] |
| 260 |
includeNewer = true |
| 261 |
threshold = 32 |
| 262 |
toLower = false |
| 263 |
[[related.indices]] |
| 264 |
applyFilter = false |
| 265 |
cardinalityThreshold = 0 |
| 266 |
name = 'KeyworDs' |
| 267 |
pattern = '' |
| 268 |
toLower = false |
| 269 |
type = 'basic' |
| 270 |
weight = 100 |
| 271 |
[[related.indices]] |
| 272 |
applyFilter = true |
| 273 |
cardinalityThreshold = 32 |
| 274 |
name = 'date' |
| 275 |
pattern = '' |
| 276 |
toLower = false |
| 277 |
type = 'basic' |
| 278 |
weight = 10 |
| 279 |
[[related.indices]] |
| 280 |
applyFilter = false |
| 281 |
cardinalityThreshold = 0 |
| 282 |
name = 'tags' |
| 283 |
pattern = '' |
| 284 |
toLower = false |
| 285 |
type = 'fragments' |
| 286 |
weight = 80 |
| 287 |
` |
| 288 |
|
| 289 |
m, err := config.FromConfigString(configToml, "toml") |
| 290 |
c.Assert(err, qt.IsNil) |
| 291 |
conf, err := DecodeConfig(m.GetParams("related")) |
| 292 |
|
| 293 |
c.Assert(err, qt.IsNil) |
| 294 |
c.Assert(conf.IncludeNewer, qt.IsTrue) |
| 295 |
first := conf.Indices[0] |
| 296 |
c.Assert(first.Name, qt.Equals, "keywords") |
| 297 |
} |
| 298 |
|
| 299 |
func TestToKeywordsAnySlice(t *testing.T) { |
| 300 |
c := qt.New(t) |
| 301 |
var config IndexConfig |
| 302 |
slice := []any{"A", 32, "C"} |
| 303 |
keywords, err := config.ToKeywords(slice) |
| 304 |
c.Assert(err, qt.IsNil) |
| 305 |
c.Assert(keywords, qt.DeepEquals, []Keyword{ |
| 306 |
StringKeyword("A"), |
| 307 |
StringKeyword("32"), |
| 308 |
StringKeyword("C"), |
| 309 |
}) |
| 310 |
} |
| 311 |
|
| 312 |
func BenchmarkRelatedNewIndex(b *testing.B) { |
| 313 |
pages := make([]*testDoc, 100) |
| 314 |
numkeywords := 30 |
| 315 |
allKeywords := make([]string, numkeywords) |
| 316 |
for i := range numkeywords { |
| 317 |
allKeywords[i] = fmt.Sprintf("keyword%d", i+1) |
| 318 |
} |
| 319 |
|
| 320 |
for i := range pages { |
| 321 |
start := rand.Intn(len(allKeywords)) |
| 322 |
end := start + 3 |
| 323 |
if end >= len(allKeywords) { |
| 324 |
end = start + 1 |
| 325 |
} |
| 326 |
|
| 327 |
kw := newTestDoc("tags", allKeywords[start:end]...) |
| 328 |
if i%5 == 0 { |
| 329 |
start := rand.Intn(len(allKeywords)) |
| 330 |
end := start + 3 |
| 331 |
if end >= len(allKeywords) { |
| 332 |
end = start + 1 |
| 333 |
} |
| 334 |
kw.addKeywords("keywords", allKeywords[start:end]...) |
| 335 |
} |
| 336 |
|
| 337 |
pages[i] = kw |
| 338 |
} |
| 339 |
|
| 340 |
cfg := Config{ |
| 341 |
Threshold: 50, |
| 342 |
Indices: IndicesConfig{ |
| 343 |
IndexConfig{Name: "tags", Weight: 100}, |
| 344 |
IndexConfig{Name: "keywords", Weight: 200}, |
| 345 |
}, |
| 346 |
} |
| 347 |
|
| 348 |
b.Run("singles", func(b *testing.B) { |
| 349 |
for b.Loop() { |
| 350 |
idx := NewInvertedIndex(cfg) |
| 351 |
for _, doc := range pages { |
| 352 |
idx.Add(context.Background(), doc) |
| 353 |
} |
| 354 |
} |
| 355 |
}) |
| 356 |
|
| 357 |
b.Run("all", func(b *testing.B) { |
| 358 |
for b.Loop() { |
| 359 |
idx := NewInvertedIndex(cfg) |
| 360 |
docs := make([]Document, len(pages)) |
| 361 |
for i := range pages { |
| 362 |
docs[i] = pages[i] |
| 363 |
} |
| 364 |
idx.Add(context.Background(), docs...) |
| 365 |
} |
| 366 |
}) |
| 367 |
} |
| 368 |
|
| 369 |
func BenchmarkRelatedMatchesIn(b *testing.B) { |
| 370 |
var icfg IndexConfig |
| 371 |
q1 := newQueryElement("tags", icfg.StringsToKeywords("keyword2", "keyword5", "keyword32", "asdf")...) |
| 372 |
q2 := newQueryElement("keywords", icfg.StringsToKeywords("keyword3", "keyword4")...) |
| 373 |
|
| 374 |
docs := make([]*testDoc, 1000) |
| 375 |
numkeywords := 20 |
| 376 |
allKeywords := make([]string, numkeywords) |
| 377 |
for i := range numkeywords { |
| 378 |
allKeywords[i] = fmt.Sprintf("keyword%d", i+1) |
| 379 |
} |
| 380 |
|
| 381 |
cfg := Config{ |
| 382 |
Threshold: 20, |
| 383 |
Indices: IndicesConfig{ |
| 384 |
IndexConfig{Name: "tags", Weight: 100}, |
| 385 |
IndexConfig{Name: "keywords", Weight: 200}, |
| 386 |
}, |
| 387 |
} |
| 388 |
|
| 389 |
idx := NewInvertedIndex(cfg) |
| 390 |
|
| 391 |
for i := range docs { |
| 392 |
start := rand.Intn(len(allKeywords)) |
| 393 |
end := start + 3 |
| 394 |
if end >= len(allKeywords) { |
| 395 |
end = start + 1 |
| 396 |
} |
| 397 |
|
| 398 |
index := "tags" |
| 399 |
if i%5 == 0 { |
| 400 |
index = "keywords" |
| 401 |
} |
| 402 |
|
| 403 |
idx.Add(context.Background(), newTestDoc(index, allKeywords[start:end]...)) |
| 404 |
} |
| 405 |
|
| 406 |
ctx := context.Background() |
| 407 |
for i := 0; b.Loop(); i++ { |
| 408 |
if i%10 == 0 { |
| 409 |
idx.search(ctx, q2) |
| 410 |
} else { |
| 411 |
idx.search(ctx, q1) |
| 412 |
} |
| 413 |
} |
| 414 |
} |