search

package
v1.40.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package search provides a search abstraction layer with pluggable backends. This replaces the google.golang.org/appengine/search dependency with an interface-based approach supporting multiple backends (Meilisearch, Elasticsearch, SQL LIKE queries, or in-memory).

Index

Constants

This section is empty.

Variables

View Source
var (
	// Done is returned by Iterator.Next when the iteration is complete.
	Done = errors.New("search: no more results")

	// ErrNoBackend is returned when no search backend is configured.
	ErrNoBackend = errors.New("search: no backend configured")

	// ErrIndexNotFound is returned when the requested index doesn't exist.
	ErrIndexNotFound = errors.New("search: index not found")
)

Common errors

Functions

func Delete

func Delete(ctx context.Context, indexName string, id string) error

Delete removes a document from the index using the default backend.

func Get

func Get(ctx context.Context, indexName string, id string, doc interface{}) error

Get retrieves a document from the index using the default backend.

func Put

func Put(ctx context.Context, indexName string, id string, doc interface{}) (string, error)

Put indexes a document using the default backend.

func SetBackend

func SetBackend(b Backend)

SetBackend sets the default search backend.

Types

type Atom

type Atom string

Atom is an indivisible string value for exact matching.

type Backend

type Backend interface {
	// Open returns an Index for the given name.
	Open(name string) (Index, error)

	// Close releases any resources held by the backend.
	Close() error
}

Backend is the interface that search backends must implement.

func GetBackend

func GetBackend() Backend

GetBackend returns the default search backend.

type Facet

type Facet struct {
	// Name is the facet field name.
	Name string

	// Value is the facet value (Atom, Range, or string).
	Value interface{}
}

Facet represents a facet value for filtering or in results.

type FacetResult

type FacetResult struct {
	// Name is the facet field name.
	Name string

	// Value is the facet value.
	Value interface{}

	// Count is the number of documents with this facet value.
	Count int
}

FacetResult represents a facet value in search results.

type FacetSearchOption

type FacetSearchOption struct {
	// Name is the facet field name (empty for auto-discovery).
	Name string

	// ValueLimit limits the number of values returned per facet.
	ValueLimit int

	// DiscoveryLimit limits the number of facets discovered (for auto-discovery).
	DiscoveryLimit int
}

FacetSearchOption configures facet discovery in search.

func AutoFacetDiscovery

func AutoFacetDiscovery(discoveryLimit, valueLimit int) FacetSearchOption

AutoFacetDiscovery returns a FacetSearchOption for automatic facet discovery.

type Index

type Index interface {
	// Search performs a search query and returns an iterator.
	Search(ctx context.Context, query string, opts *SearchOptions) Iterator

	// Put indexes a document with the given ID.
	Put(ctx context.Context, id string, doc interface{}) (string, error)

	// Get retrieves a document by ID.
	Get(ctx context.Context, id string, doc interface{}) error

	// Delete removes a document by ID.
	Delete(ctx context.Context, id string) error
}

Index represents a searchable index.

func Open

func Open(name string) (Index, error)

Open returns an Index for the given name using the default backend.

type Iterator

type Iterator interface {
	// Next returns the next result. Returns Done when iteration is complete.
	Next(dst interface{}) (string, error)

	// Count returns the total number of results (may be approximate).
	Count() int

	// Facets returns facet results if requested.
	Facets() ([][]FacetResult, error)
}

Iterator iterates over search results.

func Search(ctx context.Context, indexName string, query string, opts *SearchOptions) (Iterator, error)

Search performs a search on the given index using the default backend.

type Range

type Range struct {
	Start float64
	End   float64
}

Range represents a numeric range for faceting.

type SearchOptions

type SearchOptions struct {
	// IDsOnly returns only document IDs, not full documents.
	IDsOnly bool

	// Limit is the maximum number of results to return.
	Limit int

	// Offset is the number of results to skip.
	Offset int

	// Sort configures result sorting.
	Sort *SortOptions

	// Refinements filters results by facet values.
	Refinements []Facet

	// Facets configures which facets to return.
	Facets []FacetSearchOption

	// CountAccuracy configures count accuracy.
	CountAccuracy int
}

SearchOptions configures a search query.

type SortExpression

type SortExpression struct {
	// Expr is the field to sort by.
	Expr string

	// Reverse sorts in descending order if true.
	Reverse bool

	// Default is the default value for missing fields.
	Default interface{}
}

SortExpression defines a sort field and direction.

type SortOptions

type SortOptions struct {
	Expressions []SortExpression
}

SortOptions configures result sorting.

Jump to

Keyboard shortcuts

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