Debian Codegraph @

docs/content/en/configuration/related-content.md

raw
1 ---
2 title: Configure related content
3 linkTitle: Related content
4 description: Configure related content.
5 categories: []
6 keywords: []
7 ---
8
9 > [!NOTE]
10 > Use the [`Pages.Related`][] method to render related content in your templates.
11
12 Hugo provides a sensible default configuration for identifying related content, but you can customize it in your project configuration, either globally or per language.
13
14 ## Default configuration
15
16 This is the default configuration:
17
18 {{< code-toggle config=related />}}
19
20 > [!NOTE]
21 > Adding a `related` section to your project configuration requires you to provide a full configuration. You cannot override individual default values without specifying all related settings.
22
23 ## Top-level settings
24
25 `threshold`
26 : (`int`) A value in the range `[0, 100]`. A lower value will return more, but maybe not so relevant, matches.
27
28 `includeNewer`
29 : (`bool`) Whether to include pages newer than the current page in the related content listing. The output for older posts may change as new related content is added. Default is `false`.
30
31 `toLower`
32 : (`bool`) Whether to transform keywords in both the indexes and the queries to lower case. This may give more accurate results at a slight performance penalty. Default is `false`.
33
34 ## Per-index settings
35
36 `applyFilter`
37 : (`bool`) Apply a `type` specific filter to the result of a search. This is only used for the `fragments` type. Default is `false`.
38
39 `cardinalityThreshold`
40 : (`int`) The percentage threshold, in the range `[1, 100]`, above which indexed values are removed from the index. For example, a value of `60` removes all indexed values that appear in more than 60% of the documents. A value of `0` disables filtering. Default is `0`.
41
42 `minTokenLength`
43 : {{< new-in 0.166.0 />}}
44 : (`int`) When [`tokenize`](#tokenize) is `true`, the minimum token length in Unicode [code points](g). Use this to exclude short words such as `a`, `in`, and `the`, regardless of their frequency. This setting is applied before [`cardinalityThreshold`](#cardinalitythreshold), so excluded tokens never enter the index. Default is `0`, meaning no minimum.
45
46 `name`
47 : (`string`) The index name. This value maps directly to a page parameter. Hugo supports string values such as `author`, lists such as `tags` and `keywords`, and time and date objects.
48
49 `pattern`
50 : (`string`) This is only relevant for dates. When listing related content, you may want to list content that is also close in time. Setting `2006`, the default value for date indexes, as the pattern for a date index will add weight to pages published in the same year. For busier blogs, `200601`, representing year and month, may be a better default.
51
52 `tokenize`
53 : {{< new-in 0.166.0 />}}
54 : (`bool`) Whether to tokenize string values by splitting on whitespace before indexing.
55
56 For indices of type `fragments`, non-heading fragments such as [description list terms][] are always indexed by their exact identifier.
57
58 Tokenizing increases index size in proportion to the number of words per value. Consider setting [`minTokenLength`](#mintokenlength) to exclude short words and [`cardinalityThreshold`](#cardinalitythreshold) to remove high-frequency words from the index.
59
60 Singular and plural forms such as `apple` and `apples` are treated as distinct words, and [CJK](g) languages and other scripts without whitespace word boundaries are not supported.
61
62 Default is `false`.
63
64 `toLower`
65 : (`bool`) Whether to transform keywords in both the indexes and the queries to lower case. This may give more accurate results at a slight performance penalty. Default is `false`.
66
67 `type`
68 : (`string`) One of `basic` or `fragments`. Default is `basic`.
69
70 `weight`
71 : (`int`) An integer weight that indicates how important this parameter is relative to the other parameters. It can be `0`, which has the effect of turning this index off, or even negative. Test with different values to see what fits your content best. Default is `0`.
72
73 ## Examples
74
75 The following examples demonstrate common configuration patterns.
76
77 ### Related by taxonomy terms
78
79 Imagine we're building a book review site. Our main content will be book reviews, and we'll use genres and authors as taxonomies. When someone views a book review, we want to show a short list of related reviews based on shared authors and genres.
80
81 Create the content:
82
83 ```tree
84 content/
85 └── book-reviews/
86 ├── book-review-1.md
87 ├── book-review-2.md
88 ├── book-review-3.md
89 ├── book-review-4.md
90 └── book-review-5.md
91 ```
92
93 Configure the taxonomies:
94
95 {{< code-toggle file=hugo >}}
96 [taxonomies]
97 author = 'authors'
98 genre = 'genres'
99 {{< /code-toggle >}}
100
101 Configure the related content identification:
102
103 {{< code-toggle file=hugo >}}
104 [related]
105 includeNewer = true
106 threshold = 80
107 toLower = true
108 [[related.indices]]
109 name = 'authors'
110 weight = 2
111 [[related.indices]]
112 name = 'genres'
113 weight = 1
114 {{< /code-toggle >}}
115
116 We've configured the `authors` index with a weight of `2` and the `genres` index with a weight of `1`. This means Hugo prioritizes shared `authors` as twice as significant as shared `genres`.
117
118 Then render a list of 5 related reviews with a _partial_ template like this:
119
120 ```go-html-template {file="layouts/_partials/related.html" copy=true}
121 {{ with site.RegularPages.Related . | first 5 }}
122 <p>Related content:</p>
123 <ul>
124 {{ range . }}
125 <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
126 {{ end }}
127 </ul>
128 {{ end }}
129 ```
130
131 ### Related by title words
132
133 Imagine we're building a blog. We want to show related posts based on shared words in the title. For example, a post titled "Hugo image processing" should match any post whose title contains `Hugo`, `image`, or `processing`.
134
135 Configure the related content identification:
136
137 {{< code-toggle file=hugo >}}
138 [related]
139 threshold = 50
140 toLower = true
141 [[related.indices]]
142 name = 'title'
143 weight = 1
144 tokenize = true
145 minTokenLength = 4
146 {{< /code-toggle >}}
147
148 With `tokenize` set to `true`, Hugo splits each page title on whitespace and indexes the individual words. Setting `minTokenLength` to `4` excludes short words such as `and`, `the`, and `for`. Then use the same partial template shown in the previous example to render the results.
149
150 [`Pages.Related`]: /methods/pages/related/
151 [description list terms]: /configuration/markup/#parserautodefinitiontermid