| 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 |
"slices" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
qt "github.com/frankban/quicktest" |
| 26 |
"github.com/gohugoio/hugo/config" |
| 27 |
) |
| 28 |
|
| 29 |
type testDoc struct { |
| 30 |
keywords map[string][]string |
| 31 |
date time.Time |
| 32 |
name string |
| 33 |
} |
| 34 |
|
| 35 |
func (d *testDoc) String() string { |
| 36 |
var s strings.Builder |
| 37 |
s.WriteString("\n") |
| 38 |
for k, v := range d.keywords { |
| 39 |
s.WriteString(k + ":\t\t") |
| 40 |
for _, vv := range v { |
| 41 |
s.WriteString(" " + vv) |
| 42 |
} |
| 43 |
s.WriteString("\n") |
| 44 |
} |
| 45 |
return s.String() |
| 46 |
} |
| 47 |
|
| 48 |
func (d *testDoc) Name() string { |
| 49 |
return d.name |
| 50 |
} |
| 51 |
|
| 52 |
func newTestDoc(name string, keywords ...string) *testDoc { |
| 53 |
time.Sleep(1 * time.Millisecond) |
| 54 |
return newTestDocWithDate(name, time.Now(), keywords...) |
| 55 |
} |
| 56 |
|
| 57 |
func newTestDocWithDate(name string, date time.Time, keywords ...string) *testDoc { |
| 58 |
km := make(map[string][]string) |
| 59 |
|
| 60 |
kw := &testDoc{keywords: km, date: date} |
| 61 |
|
| 62 |
kw.addKeywords(name, keywords...) |
| 63 |
return kw |
| 64 |
} |
| 65 |
|
| 66 |
func (d *testDoc) addKeywords(name string, keywords ...string) *testDoc { |
| 67 |
keywordm := createTestKeywords(name, keywords...) |
| 68 |
|
| 69 |
for k, v := range keywordm { |
| 70 |
d.keywords[k] = v |
| 71 |
} |
| 72 |
return d |
| 73 |
} |
| 74 |
|
| 75 |
func createTestKeywords(name string, keywords ...string) map[string][]string { |
| 76 |
return map[string][]string{ |
| 77 |
name: keywords, |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func (d *testDoc) RelatedKeywords(cfg IndexConfig) ([]string, error) { |
| 82 |
return cfg.ToKeywords(d.keywords[cfg.Name]) |
| 83 |
} |
| 84 |
|
| 85 |
func (d *testDoc) PublishDate() time.Time { |
| 86 |
return d.date |
| 87 |
} |
| 88 |
|
| 89 |
func TestCardinalityThreshold(t *testing.T) { |
| 90 |
c := qt.New(t) |
| 91 |
config := Config{ |
| 92 |
Threshold: 90, |
| 93 |
IncludeNewer: false, |
| 94 |
Indices: IndicesConfig{ |
| 95 |
IndexConfig{Name: "tags", Weight: 50, CardinalityThreshold: 79}, |
| 96 |
IndexConfig{Name: "keywords", Weight: 65, CardinalityThreshold: 90}, |
| 97 |
}, |
| 98 |
} |
| 99 |
|
| 100 |
idx := NewInvertedIndex(config) |
| 101 |
hasKeyword := func(index, keyword string) bool { |
| 102 |
_, found := idx.index[index][keyword] |
| 103 |
return found |
| 104 |
} |
| 105 |
|
| 106 |
docs := []Document{ |
| 107 |
newTestDoc("tags", "a", "b", "c", "d"), |
| 108 |
newTestDoc("tags", "b", "d", "g"), |
| 109 |
newTestDoc("tags", "b", "d", "g"), |
| 110 |
newTestDoc("tags", "b", "h").addKeywords("keywords", "a"), |
| 111 |
newTestDoc("tags", "g", "h").addKeywords("keywords", "a", "b", "z"), |
| 112 |
} |
| 113 |
|
| 114 |
idx.Add(context.Background(), docs...) |
| 115 |
c.Assert(idx.Finalize(context.Background()), qt.IsNil) |
| 116 |
// Only tags=b should be removed. |
| 117 |
c.Assert(hasKeyword("tags", "a"), qt.Equals, true) |
| 118 |
c.Assert(hasKeyword("tags", "b"), qt.Equals, false) |
| 119 |
c.Assert(hasKeyword("tags", "d"), qt.Equals, true) |
| 120 |
c.Assert(hasKeyword("keywords", "b"), qt.Equals, true) |
| 121 |
} |
| 122 |
|
| 123 |
func TestSearch(t *testing.T) { |
| 124 |
config := Config{ |
| 125 |
Threshold: 90, |
| 126 |
IncludeNewer: false, |
| 127 |
Indices: IndicesConfig{ |
| 128 |
IndexConfig{Name: "tags", Weight: 50}, |
| 129 |
IndexConfig{Name: "keywords", Weight: 65}, |
| 130 |
}, |
| 131 |
} |
| 132 |
|
| 133 |
idx := NewInvertedIndex(config) |
| 134 |
// idx.debug = true |
| 135 |
|
| 136 |
docs := []Document{ |
| 137 |
newTestDoc("tags", "a", "b", "c", "d"), |
| 138 |
newTestDoc("tags", "b", "d", "g"), |
| 139 |
newTestDoc("tags", "b", "h").addKeywords("keywords", "a"), |
| 140 |
newTestDoc("tags", "g", "h").addKeywords("keywords", "a", "b"), |
| 141 |
} |
| 142 |
|
| 143 |
idx.Add(context.Background(), docs...) |
| 144 |
|
| 145 |
t.Run("count", func(t *testing.T) { |
| 146 |
c := qt.New(t) |
| 147 |
c.Assert(len(idx.index), qt.Equals, 2) |
| 148 |
set1, found := idx.index["tags"] |
| 149 |
c.Assert(found, qt.Equals, true) |
| 150 |
// 6 tags |
| 151 |
c.Assert(len(set1), qt.Equals, 6) |
| 152 |
|
| 153 |
set2, found := idx.index["keywords"] |
| 154 |
c.Assert(found, qt.Equals, true) |
| 155 |
c.Assert(len(set2), qt.Equals, 2) |
| 156 |
}) |
| 157 |
|
| 158 |
t.Run("search-tags", func(t *testing.T) { |
| 159 |
c := qt.New(t) |
| 160 |
m, err := idx.search(context.Background(), newQueryElement("tags", "a", "b", "d", "z")) |
| 161 |
c.Assert(err, qt.IsNil) |
| 162 |
c.Assert(len(m), qt.Equals, 2) |
| 163 |
c.Assert(m[0], qt.Equals, docs[0]) |
| 164 |
c.Assert(m[1], qt.Equals, docs[1]) |
| 165 |
}) |
| 166 |
|
| 167 |
t.Run("search-tags-and-keywords", func(t *testing.T) { |
| 168 |
c := qt.New(t) |
| 169 |
m, err := idx.search(context.Background(), |
| 170 |
newQueryElement("tags", "a", "b", "z"), |
| 171 |
newQueryElement("keywords", "a", "b")) |
| 172 |
c.Assert(err, qt.IsNil) |
| 173 |
c.Assert(len(m), qt.Equals, 3) |
| 174 |
c.Assert(m[0], qt.Equals, docs[3]) |
| 175 |
c.Assert(m[1], qt.Equals, docs[2]) |
| 176 |
c.Assert(m[2], qt.Equals, docs[0]) |
| 177 |
}) |
| 178 |
|
| 179 |
t.Run("searchdoc-all", func(t *testing.T) { |
| 180 |
c := qt.New(t) |
| 181 |
doc := newTestDoc("tags", "a").addKeywords("keywords", "a") |
| 182 |
m, err := idx.Search(context.Background(), SearchOpts{Document: doc}) |
| 183 |
c.Assert(err, qt.IsNil) |
| 184 |
c.Assert(len(m), qt.Equals, 2) |
| 185 |
c.Assert(m[0], qt.Equals, docs[3]) |
| 186 |
c.Assert(m[1], qt.Equals, docs[2]) |
| 187 |
}) |
| 188 |
|
| 189 |
t.Run("searchdoc-tags", func(t *testing.T) { |
| 190 |
c := qt.New(t) |
| 191 |
doc := newTestDoc("tags", "a", "b", "d", "z").addKeywords("keywords", "a", "b") |
| 192 |
m, err := idx.Search(context.Background(), SearchOpts{Document: doc, Indices: []string{"tags"}}) |
| 193 |
c.Assert(err, qt.IsNil) |
| 194 |
c.Assert(len(m), qt.Equals, 2) |
| 195 |
c.Assert(m[0], qt.Equals, docs[0]) |
| 196 |
c.Assert(m[1], qt.Equals, docs[1]) |
| 197 |
}) |
| 198 |
|
| 199 |
t.Run("searchdoc-keywords-date", func(t *testing.T) { |
| 200 |
c := qt.New(t) |
| 201 |
doc := newTestDoc("tags", "a", "b", "d", "z").addKeywords("keywords", "a", "b") |
| 202 |
// This will get a date newer than the others. |
| 203 |
newDoc := newTestDoc("keywords", "a", "b") |
| 204 |
idx.Add(context.Background(), newDoc) |
| 205 |
|
| 206 |
m, err := idx.Search(context.Background(), SearchOpts{Document: doc, Indices: []string{"keywords"}}) |
| 207 |
c.Assert(err, qt.IsNil) |
| 208 |
c.Assert(len(m), qt.Equals, 2) |
| 209 |
c.Assert(m[0], qt.Equals, docs[3]) |
| 210 |
}) |
| 211 |
|
| 212 |
t.Run("searchdoc-keywords-same-date", func(t *testing.T) { |
| 213 |
c := qt.New(t) |
| 214 |
idx := NewInvertedIndex(config) |
| 215 |
|
| 216 |
date := time.Now() |
| 217 |
|
| 218 |
doc := newTestDocWithDate("keywords", date, "a", "b") |
| 219 |
doc.name = "thedoc" |
| 220 |
|
| 221 |
for i := range 10 { |
| 222 |
docc := *doc |
| 223 |
docc.name = fmt.Sprintf("doc%d", i) |
| 224 |
idx.Add(context.Background(), &docc) |
| 225 |
} |
| 226 |
|
| 227 |
m, err := idx.Search(context.Background(), SearchOpts{Document: doc, Indices: []string{"keywords"}}) |
| 228 |
c.Assert(err, qt.IsNil) |
| 229 |
c.Assert(len(m), qt.Equals, 10) |
| 230 |
for i := range 10 { |
| 231 |
c.Assert(m[i].Name(), qt.Equals, fmt.Sprintf("doc%d", i)) |
| 232 |
} |
| 233 |
}) |
| 234 |
} |
| 235 |
|
| 236 |
func TestToKeywordsToLower(t *testing.T) { |
| 237 |
c := qt.New(t) |
| 238 |
slice := []string{"A", "B", "C"} |
| 239 |
config := IndexConfig{ToLower: true} |
| 240 |
keywords, err := config.ToKeywords(slice) |
| 241 |
c.Assert(err, qt.IsNil) |
| 242 |
c.Assert(slice, qt.DeepEquals, []string{"A", "B", "C"}) |
| 243 |
c.Assert(keywords, qt.DeepEquals, []string{"a", "b", "c"}) |
| 244 |
} |
| 245 |
|
| 246 |
func TestDecodeConfig(t *testing.T) { |
| 247 |
c := qt.New(t) |
| 248 |
|
| 249 |
configToml := ` |
| 250 |
[related] |
| 251 |
includeNewer = true |
| 252 |
threshold = 32 |
| 253 |
toLower = false |
| 254 |
[[related.indices]] |
| 255 |
applyFilter = false |
| 256 |
cardinalityThreshold = 0 |
| 257 |
name = 'KeyworDs' |
| 258 |
pattern = '' |
| 259 |
toLower = false |
| 260 |
type = 'basic' |
| 261 |
weight = 100 |
| 262 |
[[related.indices]] |
| 263 |
applyFilter = true |
| 264 |
cardinalityThreshold = 32 |
| 265 |
name = 'date' |
| 266 |
pattern = '' |
| 267 |
toLower = false |
| 268 |
type = 'basic' |
| 269 |
weight = 10 |
| 270 |
[[related.indices]] |
| 271 |
applyFilter = false |
| 272 |
cardinalityThreshold = 0 |
| 273 |
name = 'tags' |
| 274 |
pattern = '' |
| 275 |
toLower = false |
| 276 |
type = 'fragments' |
| 277 |
weight = 80 |
| 278 |
` |
| 279 |
|
| 280 |
m, err := config.FromConfigString(configToml, "toml") |
| 281 |
c.Assert(err, qt.IsNil) |
| 282 |
conf, err := DecodeConfig(m.GetParams("related")) |
| 283 |
|
| 284 |
c.Assert(err, qt.IsNil) |
| 285 |
c.Assert(conf.IncludeNewer, qt.IsTrue) |
| 286 |
first := conf.Indices[0] |
| 287 |
c.Assert(first.Name, qt.Equals, "keywords") |
| 288 |
} |
| 289 |
|
| 290 |
func TestToKeywordsAnySlice(t *testing.T) { |
| 291 |
c := qt.New(t) |
| 292 |
var config IndexConfig |
| 293 |
slice := []any{"A", 32, "C"} |
| 294 |
keywords, err := config.ToKeywords(slice) |
| 295 |
c.Assert(err, qt.IsNil) |
| 296 |
c.Assert(keywords, qt.DeepEquals, []string{"A", "32", "C"}) |
| 297 |
} |
| 298 |
|
| 299 |
func TestSplitWords(t *testing.T) { |
| 300 |
c := qt.New(t) |
| 301 |
|
| 302 |
tests := []struct { |
| 303 |
in string |
| 304 |
want []string |
| 305 |
}{ |
| 306 |
{"", nil}, |
| 307 |
{" ", nil}, |
| 308 |
{"hello world", []string{"hello", "world"}}, |
| 309 |
// leading/trailing punctuation stripped |
| 310 |
{"...hello...", []string{"hello"}}, |
| 311 |
{",hello, world!", []string{"hello", "world"}}, |
| 312 |
// digits preserved |
| 313 |
{"go1.24", []string{"go1.24"}}, |
| 314 |
// HTML entities decoded |
| 315 |
{"foo &bar", []string{"foo", "bar"}}, |
| 316 |
// fast path: single token, no whitespace |
| 317 |
{"&foo&", []string{"foo"}}, |
| 318 |
// and similar entities that decode to whitespace must not use the fast path |
| 319 |
{"Apples and oranges!", []string{"Apples", "and", "oranges"}}, |
| 320 |
// punctuation-only tokens dropped |
| 321 |
{"hello --- world", []string{"hello", "world"}}, |
| 322 |
// mixed spaces |
| 323 |
{" foo bar ", []string{"foo", "bar"}}, |
| 324 |
} |
| 325 |
|
| 326 |
for _, tt := range tests { |
| 327 |
c.Assert(slices.Collect(splitWordsSeq(tt.in)), qt.DeepEquals, tt.want, qt.Commentf("input: %q", tt.in)) |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func BenchmarkRelatedNewIndex(b *testing.B) { |
| 332 |
rnd := rand.New(rand.NewSource(32)) |
| 333 |
pages := make([]*testDoc, 100) |
| 334 |
numkeywords := 30 |
| 335 |
allKeywords := make([]string, numkeywords) |
| 336 |
for i := range numkeywords { |
| 337 |
allKeywords[i] = fmt.Sprintf("keyword%d", i+1) |
| 338 |
} |
| 339 |
|
| 340 |
for i := range pages { |
| 341 |
start := rnd.Intn(len(allKeywords)) |
| 342 |
end := start + 3 |
| 343 |
if end >= len(allKeywords) { |
| 344 |
end = start + 1 |
| 345 |
} |
| 346 |
|
| 347 |
kw := newTestDoc("tags", allKeywords[start:end]...) |
| 348 |
if i%5 == 0 { |
| 349 |
start := rnd.Intn(len(allKeywords)) |
| 350 |
end := start + 3 |
| 351 |
if end >= len(allKeywords) { |
| 352 |
end = start + 1 |
| 353 |
} |
| 354 |
kw.addKeywords("keywords", allKeywords[start:end]...) |
| 355 |
} |
| 356 |
|
| 357 |
pages[i] = kw |
| 358 |
} |
| 359 |
|
| 360 |
cfg := Config{ |
| 361 |
Threshold: 50, |
| 362 |
Indices: IndicesConfig{ |
| 363 |
IndexConfig{Name: "tags", Weight: 100}, |
| 364 |
IndexConfig{Name: "keywords", Weight: 200}, |
| 365 |
}, |
| 366 |
} |
| 367 |
|
| 368 |
b.Run("singles", func(b *testing.B) { |
| 369 |
for b.Loop() { |
| 370 |
idx := NewInvertedIndex(cfg) |
| 371 |
for _, doc := range pages { |
| 372 |
idx.Add(context.Background(), doc) |
| 373 |
} |
| 374 |
} |
| 375 |
}) |
| 376 |
|
| 377 |
b.Run("all", func(b *testing.B) { |
| 378 |
for b.Loop() { |
| 379 |
idx := NewInvertedIndex(cfg) |
| 380 |
docs := make([]Document, len(pages)) |
| 381 |
for i := range pages { |
| 382 |
docs[i] = pages[i] |
| 383 |
} |
| 384 |
idx.Add(context.Background(), docs...) |
| 385 |
} |
| 386 |
}) |
| 387 |
} |
| 388 |
|
| 389 |
func BenchmarkRelatedMatchesIn(b *testing.B) { |
| 390 |
q1 := newQueryElement("tags", "keyword2", "keyword5", "keyword32", "asdf") |
| 391 |
q2 := newQueryElement("keywords", "keyword3", "keyword4") |
| 392 |
|
| 393 |
rnd := rand.New(rand.NewSource(32)) |
| 394 |
docs := make([]*testDoc, 1000) |
| 395 |
numkeywords := 20 |
| 396 |
allKeywords := make([]string, numkeywords) |
| 397 |
for i := range numkeywords { |
| 398 |
allKeywords[i] = fmt.Sprintf("keyword%d", i+1) |
| 399 |
} |
| 400 |
|
| 401 |
cfg := Config{ |
| 402 |
Threshold: 20, |
| 403 |
Indices: IndicesConfig{ |
| 404 |
IndexConfig{Name: "tags", Weight: 100}, |
| 405 |
IndexConfig{Name: "keywords", Weight: 200}, |
| 406 |
}, |
| 407 |
} |
| 408 |
|
| 409 |
idx := NewInvertedIndex(cfg) |
| 410 |
|
| 411 |
for i := range docs { |
| 412 |
start := rnd.Intn(len(allKeywords)) |
| 413 |
end := start + 3 |
| 414 |
if end >= len(allKeywords) { |
| 415 |
end = start + 1 |
| 416 |
} |
| 417 |
|
| 418 |
index := "tags" |
| 419 |
if i%5 == 0 { |
| 420 |
index = "keywords" |
| 421 |
} |
| 422 |
|
| 423 |
idx.Add(context.Background(), newTestDoc(index, allKeywords[start:end]...)) |
| 424 |
} |
| 425 |
|
| 426 |
ctx := context.Background() |
| 427 |
for i := 0; b.Loop(); i++ { |
| 428 |
if i%10 == 0 { |
| 429 |
idx.search(ctx, q2) |
| 430 |
} else { |
| 431 |
idx.search(ctx, q1) |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
func BenchmarkRelatedTokenizeCreateIndex(b *testing.B) { |
| 437 |
rnd := rand.New(rand.NewSource(32)) |
| 438 |
keywordsCfg := IndexConfig{Name: "keywords", Weight: 100, Tokenize: true} |
| 439 |
|
| 440 |
docs := make([]*testDoc, 100) |
| 441 |
numGroups := 5 |
| 442 |
numKeywords := 3 |
| 443 |
allKeywords := make([]string, numGroups) |
| 444 |
for i := range numGroups { |
| 445 |
for j := range numKeywords { |
| 446 |
allKeywords[i] += fmt.Sprintf("keyword%d ", i*numKeywords+j+1) |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
for i := range docs { |
| 451 |
start := rnd.Intn(len(allKeywords)) |
| 452 |
end := start + 3 |
| 453 |
if end >= len(allKeywords) { |
| 454 |
end = start + 1 |
| 455 |
} |
| 456 |
|
| 457 |
kws := slices.Clone(allKeywords[start:end]) |
| 458 |
kws = append(kws, "keyword3") |
| 459 |
docs[i] = newTestDoc("keywords", kws...) |
| 460 |
} |
| 461 |
|
| 462 |
for b.Loop() { |
| 463 |
cfg := Config{ |
| 464 |
Threshold: 10, |
| 465 |
Indices: IndicesConfig{keywordsCfg}, |
| 466 |
} |
| 467 |
|
| 468 |
idx := NewInvertedIndex(cfg) |
| 469 |
|
| 470 |
for j := range docs { |
| 471 |
idx.Add(context.Background(), docs[j]) |
| 472 |
} |
| 473 |
if err := idx.Finalize(context.Background()); err != nil { |
| 474 |
b.Fatal(err) |
| 475 |
} |
| 476 |
} |
| 477 |
} |