related

package
v0.0.0-...-42d6f5d Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package related holds code to help finding related content.

Index

Constants

View Source
const (
	TypeBasic     = "basic"
	TypeFragments = "fragments"
)

Variables

View Source
var (

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

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 IndicesConfig
}

Config is the top level configuration element used to configure how to retrieve related content in Hugo.

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.

func (*Config) HasType

func (c *Config) HasType(s string) bool

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 FragmentKeyword

type FragmentKeyword string

FragmentKeyword represents a document fragment.

func (FragmentKeyword) String

func (f FragmentKeyword) String() string

type FragmentProvider

type FragmentProvider interface {
	Fragments(context.Context) *tableofcontents.Fragments

	// For internal use.
	ApplyFilterToHeadings(context.Context, func(*tableofcontents.Heading) bool) Document
}

FragmentProvider is an optional interface that can be implemented by a Document.

type IndexConfig

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

	// The index type.
	Type string

	// Enable to apply a type specific filter to the results.
	// This is currently only used for the "fragments" type.
	ApplyFilter bool

	// 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

	// A percentage (0-100) used to remove common keywords from the index.
	// As an example, setting this to 50 will remove all keywords that are
	// used in more than 50% of the documents in the index.
	CardinalityThreshold 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) StringsToKeywords

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

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

func (IndexConfig) ToKeywords

func (cfg IndexConfig) ToKeywords(v any) ([]Keyword, error)

ToKeywords returns a Keyword slice of the given input.

type IndicesConfig

type IndicesConfig []IndexConfig

IndicesConfig 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(ctx context.Context, docs ...Document) error

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

func (*InvertedIndex) Finalize

func (idx *InvertedIndex) Finalize(ctx context.Context) error

func (*InvertedIndex) Search

func (idx *InvertedIndex) Search(ctx context.Context, opts SearchOpts) ([]Document, error)

Search finds the documents matching any of the keywords in the given indices against query options in opts. 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.

type Keyword

type Keyword interface {
	String() string
}

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

type SearchOpts

type SearchOpts struct {
	// The Document to search for related content for.
	Document Document

	// The keywords to search for.
	NamedSlices []types.KeyValues

	// The indices to search in.
	Indices []string

	// Fragments holds a a list of special keywords that is used
	// for indices configured as type "fragments".
	// This will match the fragment identifiers of the documents.
	Fragments []string
}

SearchOpts holds the options for a related search.

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