related

package
v0.95.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2022 License: Apache-2.0 Imports: 9 Imported by: 36

Documentation

Overview

Package related holds code to help finding related content.

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultConfig is the default related config.
	DefaultConfig = Config{
		Threshold: 80,
		Indices: IndexConfigs{
			IndexConfig{Name: "keywords", Weight: 100},
			IndexConfig{Name: "date", Weight: 10},
		},
	}
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Only include matches >= threshold, a normalized rank between 0 and 100.
	Threshold int

	// To get stable "See also" sections we, by default, exclude newer related pages.
	IncludeNewer bool

	// Will lower case all string values and queries to the indices.
	// May get better results, but at a slight performance cost.
	ToLower bool

	Indices IndexConfigs
}

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"

func DecodeConfig

func DecodeConfig(m maps.Params) (Config, error)

DecodeConfig decodes a slice of map into Config.

func (*Config) Add

func (c *Config) Add(index IndexConfig)

Add adds a given index.

type Document

type Document interface {
	// RelatedKeywords returns a list of keywords for the given index config.
	RelatedKeywords(cfg IndexConfig) ([]Keyword, error)

	// When this document was or will be published.
	PublishDate() time.Time

	// Name is used as an tiebreaker if both Weight and PublishDate are
	// the same.
	Name() string
}

Document is the interface an indexable document in Hugo must fulfill.

type IndexConfig

type IndexConfig struct {
	// The index name. This directly maps to a field or Param name.
	Name string

	// Contextual pattern used to convert the Param value into a string.
	// Currently only used for dates. Can be used to, say, bump posts in the same
	// time frame when searching for related documents.
	// For dates it follows Go's time.Format patterns, i.e.
	// "2006" for YYYY and "200601" for YYYYMM.
	Pattern string

	// This field's weight when doing multi-index searches. Higher is "better".
	Weight int

	// Will lower case all string values in and queries tothis index.
	// May get better accurate results, but at a slight performance cost.
	ToLower bool
}

IndexConfig configures an index.

func (IndexConfig) ToKeywords

func (cfg IndexConfig) ToKeywords(v interface{}) ([]Keyword, error)

ToKeywords returns a Keyword slice of the given input.

type IndexConfigs

type IndexConfigs []IndexConfig

IndexConfigs holds a set of index configurations.

type InvertedIndex

type InvertedIndex struct {
	// contains filtered or unexported fields
}

InvertedIndex holds an inverted index, also sometimes named posting list, which lists, for every possible search term, the documents that contain that term.

func NewInvertedIndex

func NewInvertedIndex(cfg Config) *InvertedIndex

NewInvertedIndex creates a new InvertedIndex. Documents to index must be added in Add.

func (*InvertedIndex) Add

func (idx *InvertedIndex) Add(docs ...Document) error

Add documents to the inverted index. The value must support == and !=.

func (*InvertedIndex) SearchDoc

func (idx *InvertedIndex) SearchDoc(doc Document, indices ...string) ([]Document, error)

SearchDoc finds the documents matching any of the keywords in the given indices against the given document. The resulting document set will be sorted according to number of matches and the index weights, and any matches with a rank below the configured threshold (normalize to 0..100) will be removed. If an index name is provided, only that index will be queried.

func (*InvertedIndex) SearchKeyValues

func (idx *InvertedIndex) SearchKeyValues(args ...types.KeyValues) ([]Document, error)

SearchKeyValues finds the documents matching any of the keywords in the given indices. The resulting document set will be sorted according to number of matches and the index weights, and any matches with a rank below the configured threshold (normalize to 0..100) will be removed.

type Keyword

type Keyword interface {
	String() string
}

Keyword is the interface a keyword in the search index must implement.

func StringsToKeywords

func StringsToKeywords(s ...string) []Keyword

StringsToKeywords converts the given slice of strings to a slice of Keyword.

type StringKeyword

type StringKeyword string

StringKeyword is a string search keyword.

func (StringKeyword) String

func (s StringKeyword) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL