aboutsummaryrefslogtreecommitdiffhomepage
path: root/related
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2023-01-04 18:24:36 +0100
committerBjørn Erik Pedersen <[email protected]>2023-05-16 18:01:29 +0200
commit241b21b0fd34d91fccb2ce69874110dceae6f926 (patch)
treed4e0118eac7e9c42f065815447a70805f8d6ad3e /related
parent6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff)
downloadhugo-241b21b0fd34d91fccb2ce69874110dceae6f926.tar.gz
hugo-241b21b0fd34d91fccb2ce69874110dceae6f926.zip
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code. Also, * Lower case the default output format names; this is in line with the custom ones (map keys) and how it's treated all the places. This avoids doing `stringds.EqualFold` everywhere. Closes #10896 Closes #10620
Diffstat (limited to 'related')
-rw-r--r--related/inverted_index.go36
-rw-r--r--related/inverted_index_test.go8
2 files changed, 15 insertions, 29 deletions
diff --git a/related/inverted_index.go b/related/inverted_index.go
index ae894e522..fcebdc716 100644
--- a/related/inverted_index.go
+++ b/related/inverted_index.go
@@ -53,32 +53,15 @@ var (
// DefaultConfig is the default related config.
DefaultConfig = Config{
Threshold: 80,
- Indices: IndexConfigs{
+ Indices: IndicesConfig{
IndexConfig{Name: "keywords", Weight: 100, Type: TypeBasic},
IndexConfig{Name: "date", Weight: 10, Type: TypeBasic},
},
}
)
-/*
-Config is the top level configuration element used to configure how to retrieve
-related content in Hugo.
-
-An example site config.toml:
-
- [related]
- threshold = 1
- [[related.indices]]
- name = "keywords"
- weight = 200
- [[related.indices]]
- name = "tags"
- weight = 100
- [[related.indices]]
- name = "date"
- weight = 1
- pattern = "2006"
-*/
+// Config is the top level configuration element used to configure how to retrieve
+// related content in Hugo.
type Config struct {
// Only include matches >= threshold, a normalized rank between 0 and 100.
Threshold int
@@ -90,7 +73,7 @@ type Config struct {
// May get better results, but at a slight performance cost.
ToLower bool
- Indices IndexConfigs
+ Indices IndicesConfig
}
// Add adds a given index.
@@ -110,8 +93,8 @@ func (c *Config) HasType(s string) bool {
return false
}
-// IndexConfigs holds a set of index configurations.
-type IndexConfigs []IndexConfig
+// IndicesConfig holds a set of index configurations.
+type IndicesConfig []IndexConfig
// IndexConfig configures an index.
type IndexConfig struct {
@@ -366,13 +349,13 @@ func (idx *InvertedIndex) Search(ctx context.Context, opts SearchOpts) ([]Docume
var (
queryElements []queryElement
- configs IndexConfigs
+ configs IndicesConfig
)
if len(opts.Indices) == 0 {
configs = idx.cfg.Indices
} else {
- configs = make(IndexConfigs, len(opts.Indices))
+ configs = make(IndicesConfig, len(opts.Indices))
for i, indexName := range opts.Indices {
cfg, found := idx.getIndexCfg(indexName)
if !found {
@@ -396,12 +379,14 @@ func (idx *InvertedIndex) Search(ctx context.Context, opts SearchOpts) ([]Docume
keywords = append(keywords, FragmentKeyword(fragment))
}
if opts.Document != nil {
+
if fp, ok := opts.Document.(FragmentProvider); ok {
for _, fragment := range fp.Fragments(ctx).Identifiers {
keywords = append(keywords, FragmentKeyword(fragment))
}
}
}
+
}
queryElements = append(queryElements, newQueryElement(cfg.Name, keywords...))
}
@@ -553,6 +538,7 @@ func (idx *InvertedIndex) searchDate(ctx context.Context, self Document, upperDa
for i, m := range matches {
result[i] = m.Doc
+
if len(fragmentsFilter) > 0 {
if dp, ok := result[i].(FragmentProvider); ok {
result[i] = dp.ApplyFilterToHeadings(ctx, func(h *tableofcontents.Heading) bool {
diff --git a/related/inverted_index_test.go b/related/inverted_index_test.go
index c7348e088..72b2f3252 100644
--- a/related/inverted_index_test.go
+++ b/related/inverted_index_test.go
@@ -91,7 +91,7 @@ func TestCardinalityThreshold(t *testing.T) {
config := Config{
Threshold: 90,
IncludeNewer: false,
- Indices: IndexConfigs{
+ Indices: IndicesConfig{
IndexConfig{Name: "tags", Weight: 50, CardinalityThreshold: 79},
IndexConfig{Name: "keywords", Weight: 65, CardinalityThreshold: 90},
},
@@ -125,7 +125,7 @@ func TestSearch(t *testing.T) {
config := Config{
Threshold: 90,
IncludeNewer: false,
- Indices: IndexConfigs{
+ Indices: IndicesConfig{
IndexConfig{Name: "tags", Weight: 50},
IndexConfig{Name: "keywords", Weight: 65},
},
@@ -293,7 +293,7 @@ func BenchmarkRelatedNewIndex(b *testing.B) {
cfg := Config{
Threshold: 50,
- Indices: IndexConfigs{
+ Indices: IndicesConfig{
IndexConfig{Name: "tags", Weight: 100},
IndexConfig{Name: "keywords", Weight: 200},
},
@@ -334,7 +334,7 @@ func BenchmarkRelatedMatchesIn(b *testing.B) {
cfg := Config{
Threshold: 20,
- Indices: IndexConfigs{
+ Indices: IndicesConfig{
IndexConfig{Name: "tags", Weight: 100},
IndexConfig{Name: "keywords", Weight: 200},
},