search

package
v1.35.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: Apache-2.0 Imports: 10 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 Document

type Document struct {
	Labels map[string]string
	// contains filtered or unexported fields
}

Document is a single document in the index. It contains the id, fields, and Labels.

func EmptyDocument

func EmptyDocument(id string) *Document

EmptyDocument returns a Document with the specified id and no fields or Labels

func (*Document) AddField

func (d *Document) AddField(name, value string)

AddField adds a field to the document. If the field already exists, the value is appended to the existing field.

func (*Document) BuildValues

func (d *Document) BuildValues() string

BuildValues builds a string containing all field values, label names, and label values, separated by newlines.

func (*Document) IndexFields

func (d *Document) IndexFields(index modelSearch.Indexer)

IndexFields iterates over the fields of the document and calls the callback for each field

func (*Document) IndexID

func (d *Document) IndexID() string

IndexID returns the id of the document

func (*Document) IndexLabels

func (d *Document) IndexLabels(index modelSearch.Indexer)

IndexLabels iterates over the Labels of the document and calls the callback for each Label

type Index

type Index interface {
	// Add or updates an indexed resource
	Upsert(context.Context, search.Indexed) error

	// Remove an index resource
	Remove(context.Context, search.Indexed) error

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

	// Matches returns true if the specified indexID matches the query exactly
	Matches(ctx context.Context, 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(ctx context.Context, query *Query) ([]*Suggestion, error)

	// Select returns the matching ids
	Select(ctx context.Context, 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 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

func NameSuggestion

func NameSuggestion(operator string, name string, score int) *Suggestion

NameSuggestion returns a list of suggestions for the specified name and value.

func ValueSuggestion

func ValueSuggestion(operator string, name string, value string, query *string, score int) *Suggestion

ValueSuggestion returns a Suggestion for the specified name and value and query.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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