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 > To understand Hugo's related content identification, please refer to the [related content][] page.
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 between 0-100, inclusive. 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. This will mean that the output for older posts may change as new related content gets 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`) If between `1` and `100`, this is a percentage. All keywords that are used in more than this percentage of documents are removed. For example, setting this to `60` will remove all keywords that are used in more than 60% of the documents in the index. If `0`, no keyword is removed from the index. Default is `0`.
41
42 `name`
43 : (`string`) The index name. This value maps directly to a page parameter. Hugo supports string values (`author` in the example) and lists (`tags`, `keywords` etc.) and time and date objects.
44
45 `pattern`
46 : (`string`) This is currently only relevant for dates. When listing related content, we may want to list content that is also close in time. Setting "2006" (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" (year and month) may be a better default.
47
48 `toLower`
49 : (`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`.
50
51 `type`
52 : (`string`) One of `basic` or `fragments`. Default is `basic`.
53
54 `weight`
55 : (`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`.
56
57 ## Example
58
59 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.
60
61 Create the content:
62
63 ```tree
64 content/
65 └── book-reviews/
66 ├── book-review-1.md
67 ├── book-review-2.md
68 ├── book-review-3.md
69 ├── book-review-4.md
70 └── book-review-5.md
71 ```
72
73 Configure the taxonomies:
74
75 {{< code-toggle file=hugo >}}
76 [taxonomies]
77 author = 'authors'
78 genre = 'genres'
79 {{< /code-toggle >}}
80
81 Configure the related content identification:
82
83 {{< code-toggle file=hugo >}}
84 [related]
85 includeNewer = true
86 threshold = 80
87 toLower = true
88 [[related.indices]]
89 name = 'authors'
90 weight = 2
91 [[related.indices]]
92 name = 'genres'
93 weight = 1
94 {{< /code-toggle >}}
95
96 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`.
97
98 Then render a list of 5 related reviews with a _partial_ template like this:
99
100 ```go-html-template {file="layouts/_partials/related.html" copy=true}
101 {{ with site.RegularPages.Related . | first 5 }}
102 <p>Related content:</p>
103 <ul>
104 {{ range . }}
105 <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
106 {{ end }}
107 </ul>
108 {{ end }}
109 ```
110
111 [related content]: /content-management/related-content/