Documentation
¶
Overview ¶
Package search is a full-text search abstraction: documents go into named indexes, and queries come back as ranked, filtered, faceted, paginated results. Engines in sibling packages implement it over SQL (search/nock) or Meilisearch (search/meilisearch); search/searchtest has an in-memory engine and a contract suite for any Engine. This package imports only the standard library.
Index ¶
- Constants
- Variables
- func DecodeFields[T any](h Hit) (T, error)
- func FilterValues(v any) ([]any, error)
- func Highlight(text string, terms []string, pre, post string) string
- func Tokens(s string) []string
- func ValidField(name string) error
- func ValidIndexName(name string) error
- type Document
- type Engine
- type FacetCount
- type Hit
- type Index
- type Manager
- func (m *Manager) DeleteRecord(ctx context.Context, r Searchable) error
- func (m *Manager) Engine() Engine
- func (m *Manager) IndexBatch(ctx context.Context, records ...Searchable) error
- func (m *Manager) IndexRecord(ctx context.Context, r Searchable) error
- func (m *Manager) Search(ctx context.Context, name string, q Query) (Result, error)
- type Query
- type Result
- type Searchable
- type SortKey
- type TypedResult
Constants ¶
const ( DefaultPerPage = 20 MaxPerPage = 1000 DefaultPreTag = "<em>" DefaultPostTag = "</em>" HighlightTextKey = "text" )
Pagination and highlighting defaults.
const ScoreField = "_score"
ScoreField is the SortKey field that means relevance.
Variables ¶
var ( ErrInvalidIndexName = errors.New("search: invalid index name") ErrInvalidField = errors.New("search: invalid field name") ErrEmptyID = errors.New("search: document id is empty") )
Errors returned by engines and the Manager.
Functions ¶
func DecodeFields ¶
DecodeFields unmarshals the Fields of h's document into T through JSON.
func FilterValues ¶
FilterValues returns the scalar values a Query.Filters entry matches: v itself for a scalar, or the elements of a slice. Scalars are normalized to string, bool, int64 or float64. A nil value, a nested slice, a map, or any other type is an error.
func Highlight ¶
Highlight returns text with every word that equals one of terms (case-insensitively) wrapped in pre and post. It returns "" when no word matched. terms are lower-case words, as Tokens returns them.
func Tokens ¶
Tokens splits s into lower-cased words: maximal runs of letters and digits. Engines without their own analyzer use it so they agree on what a word is.
func ValidField ¶
ValidField reports whether name is a legal field name: dot-separated segments of letters, digits, '_' and '-', each starting with a letter or '_'. "text" is allowed here because it also names the body in highlights.
func ValidIndexName ¶
ValidIndexName reports whether name is a legal index name: 1 to 64 letters, digits, '_' or '-', starting with a letter or digit.
Types ¶
type Document ¶
type Document struct {
ID string `json:"id"`
Fields map[string]any `json:"fields,omitempty"`
Text string `json:"text,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
}
Document is one searchable record.
Text is the full-text body. String values in Fields (and string elements of slice values) are searched as well. Fields also drive filters, facets, and sorting. Metadata is stored and returned with hits, and is never searched or filtered.
func (Document) FieldStrings ¶
FieldStrings returns the string values of d.Fields (and string elements of slice values), in sorted field order.
func (Document) SearchableStrings ¶
SearchableStrings returns the text an engine without field-level indexing should search for d: Text plus the string values of Fields, with slice elements included, in sorted field order.
type Engine ¶
type Engine interface {
// Index returns the index called name. It does not contact the backend;
// an invalid name makes the Index's methods fail with ErrInvalidIndexName.
Index(name string) Index
// DeleteIndex removes the index and all its documents.
DeleteIndex(ctx context.Context, name string) error
Close() error
}
Engine is a search backend holding named indexes.
type FacetCount ¶
FacetCount is one value of a facet and the number of matching documents that have it. Values are formatted with fmt.Sprint.
type Index ¶
type Index interface {
// Upsert inserts docs, replacing documents with the same ID.
Upsert(ctx context.Context, docs ...Document) error
// Delete removes the documents with ids; unknown ids are ignored.
Delete(ctx context.Context, ids ...string) error
// Flush removes every document and keeps the index.
Flush(ctx context.Context) error
Search(ctx context.Context, q Query) (Result, error)
}
Index is one named collection of documents.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager indexes and searches Searchable records through an Engine.
func (*Manager) DeleteRecord ¶
func (m *Manager) DeleteRecord(ctx context.Context, r Searchable) error
DeleteRecord removes the document of r from its index.
func (*Manager) IndexBatch ¶
func (m *Manager) IndexBatch(ctx context.Context, records ...Searchable) error
IndexBatch upserts records, one Upsert per index.
func (*Manager) IndexRecord ¶
func (m *Manager) IndexRecord(ctx context.Context, r Searchable) error
IndexRecord upserts the document of r into its index.
type Query ¶
type Query struct {
Text string
Filters map[string]any
Facets []string
Sort []SortKey
Page int
PerPage int
HighlightFields []string
HighlightPreTag string
HighlightPostTag string
}
Query describes a search.
Text is matched as a set of words that must all occur; empty Text matches every document. Each Filters entry restricts a field: a scalar value matches documents whose field equals it (or, for a slice field, contains it), and a slice value matches any of its elements. Entries are ANDed. Facets names the fields to count over the whole filtered result set, not only the requested page.
Page is 1-based. Zero Page and PerPage mean page 1 and DefaultPerPage. HighlightFields names the fields to highlight ("text" is Document.Text); the tags default to <em> and </em>.
type Result ¶
type Result struct {
Hits []Hit
Total int64
Page int
PerPage int
TotalPages int
Facets map[string][]FacetCount
Took time.Duration
}
Result is one page of search results.
type Searchable ¶
Searchable is a record that knows its index and how to become a Document.
type TypedResult ¶
TypedResult is a Result with each hit decoded into T. Items[i] belongs to Result.Hits[i].
func Search ¶
func Search[T any](ctx context.Context, m *Manager, name string, q Query, decode func(Hit) (T, error)) (TypedResult[T], error)
Search runs q on the index called name and decodes every hit with decode. A nil decode unmarshals Document.Fields into T through JSON, with the ID available under the field "id" only if the Fields hold it.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package meilisearch is a search.Engine on a Meilisearch server, spoken to with net/http and encoding/json only.
|
Package meilisearch is a search.Engine on a Meilisearch server, spoken to with net/http and encoding/json only. |
|
Package nock is a search.Engine on a SQL database reached through github.com/fletchhq/fletch/nock.
|
Package nock is a search.Engine on a SQL database reached through github.com/fletchhq/fletch/nock. |
|
Package quiver indexes search documents in the background through a github.com/fletchhq/fletch/quiver queue.
|
Package quiver indexes search documents in the background through a github.com/fletchhq/fletch/quiver queue. |
|
Package searchtest has an in-memory search.Engine and a contract suite that every search.Engine must pass.
|
Package searchtest has an in-memory search.Engine and a contract suite that every search.Engine must pass. |