search

package
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package search provides a search engine with indexing and suggestions for the store

Index

Constants

View Source
const (
	// ScoreExact is for exact matches where the query matches the value
	ScoreExact int = 100
	// ScorePrefix is for matches where the query is a prefix of the value
	ScorePrefix = 50
	// ScoreSubstring is for matches where the query is a substring of the value
	ScoreSubstring = 25
	// ScoreFuzzy is for matches where the query is a fuzzy match of the value, e.g. "pe" matches apple because it matches
	// the letters in order -p--e.
	ScoreFuzzy = 10
)

Variables

This section is empty.

Functions

func Field

func Field(ctx context.Context, index Index, field, value string) ([]string, error)

Field is a helper function to search the given index with a field:value pair.

func SortSuggestions

func SortSuggestions(suggestions []*Suggestion)

SortSuggestions sorts alphabetically by score and label

Types

type Index

type Index interface {
	// Add or updates an indexed resource
	Upsert(Indexed) error

	// Remove an index resource
	Remove(Indexed) error

	// Query returns ids that match the specified query.
	Search(ctx context.Context, query *Query) ([]string, error)

	// Matches returns true if the specified indexID matches the query
	Matches(query *Query, indexID string) bool

	// Suggestions are either names of fields or labels or will be values if a field or label is already specified. If
	// there are no suggestions that match or there is one suggestion that is an exact match, no suggestions are returned.
	Suggestions(query *Query) ([]*Suggestion, error)

	// Select returns the matching ids
	Select(labels map[string]string) []string
}

Index provides a query interface for indexed resources. A separate index will be used for each resource type. Currently the functions on Index do not produce an error but some other implementation could.

func NewInMemoryIndex

func NewInMemoryIndex(name string) Index

NewInMemoryIndex returns a new implementation of the the search Index interface that stores the index in memory

type Indexed

type Indexed interface {
	// IndexID returns an ID used to identify the resource that is indexed
	IndexID() string

	// IndexFields should index the fields using the index function
	IndexFields(index Indexer)

	// IndexFields should index the labels using the index function
	IndexLabels(index Indexer)
}

Indexed must be implemented by resources that are indexed

type Indexer

type Indexer func(name string, value string)

Indexer is a function passed to the Index functions of

type LatestVersionProvider

type LatestVersionProvider interface {
	LatestVersionString(ctx context.Context) string
}

LatestVersionProvider provides the latest version string for an agent that matches version:latest

type Query

type Query struct {
	Original string
	Tokens   []*QueryToken
}

Query consists of a list of query tokens

func ParseQuery

func ParseQuery(query string) *Query

ParseQuery parses a query by splitting it into tokens

func (*Query) ApplySuggestion

func (q *Query) ApplySuggestion(s *Suggestion) string

ApplySuggestion returns a complete query replacing the last token with the suggestion

func (*Query) LastToken

func (q *Query) LastToken() *QueryToken

LastToken returns the last token of the Query or nil if there are no tokens in the query. The last token of a query is used for suggestions.

func (*Query) ReplaceVersionLatest

func (q *Query) ReplaceVersionLatest(ctx context.Context, latestVersionProvider LatestVersionProvider)

ReplaceVersionLatest allows us to support version:latest queries by replacing the keyword latest with the actual latest version.

type QueryToken

type QueryToken struct {
	Original string
	Operator string
	Name     string
	Value    string
}

QueryToken represents a string in one of name:value, name=value, or just value. In the case of a value, the Name field of the QueryToken will be "". Name and Value will both be lowercase forms of the original text to simplify case insensitive matching.

func (*QueryToken) Empty

func (t *QueryToken) Empty() bool

Empty returns true if there is no name or value

func (*QueryToken) IsNegated

func (t *QueryToken) IsNegated() bool

IsNegated returns true if the token is negated

type Suggestion

type Suggestion struct {
	// Label can be used to display the suggestion in an auto-complete field
	Label string `json:"label"`
	// Query is the query text that should be used if this suggestion is selected
	Query string `json:"query"`
	// Score provides an indication of how well the suggestion matches the query
	Score int `json:"-"`
}

Suggestion represents a single search suggestion for auto-complete

Jump to

Keyboard shortcuts

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