Help us shape the future of Go! Take the 2026 Go Developer Survey

search

package
v1.19.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

Search building block (alpha1)

Components implementing the Search interface in this package back the Dapr Search building block (dapr/proto/runtime/v1/search.proto). The contract is deliberately portable across lexical / full-text search backends (Meilisearch, Elasticsearch, OpenSearch, Solr, Typesense, ...).

Vector similarity is a separate building block – see ../vector.

See search.go for the interface and request/response types.

Write acknowledgement semantics (IndexingOptions, IndexAck, FailedItem) and the shared validators and error helpers live in indexing.go; the vector building block reuses them.

Contract notes

  • Document.Content is a UTF-8 encoded JSON object. The runtime rejects any other content into FailedItems (INVALID_ARGUMENT) with ValidateDocumentContent before it reaches a component; components may keep the same check defensively. Document.Metadata is opaque: stored, returned unchanged, never filterable.
  • DeleteDocuments is a write: it takes the same IndexingOptions as IndexDocuments and returns an IndexAck. IDs that do not exist are not an error and deletions never report FailedItems.
  • CreateIndex returns ALREADY_EXISTS for an existing index and never reconciles settings. GetDocuments returns found documents in request order.
  • Pagination uses an opaque continuation token bound to the query shape; components append the document ID as a sort tie-breaker and declare it sortable when the provider only sorts on declared attributes.

Meilisearch (meilisearch/)

  • id is the Meilisearch primary key, content keys are top-level attributes and metadata lives under the reserved daprMetadata attribute. id is always added to sortableAttributes; filterableAttributes, sortableAttributes and searchableAttributes can be set in CreateIndex metadata. CreateIndex moves the sort ranking rule first, so an explicit Sort orders text results and relevance only breaks ties.
  • Metadata is not searched. Meilisearch cannot exclude one attribute from the default searchableAttributes (*), so a text query without SearchFields on such an index is restricted to the content attributes the index holds (read from its field distribution); SearchFields of * is narrowed the same way. A search field under daprMetadata, or with a wildcard in its top-level attribute (dapr*, *.tenant), is INVALID_ARGUMENT, also in a native query. An explicit searchableAttributes setting is used as configured.
  • Meilisearch neither counts nor returns hits past the index's pagination.maxTotalHits (1000 by default). A total that reaches it is reported with TOTAL_HITS_RELATION_LOWER_BOUND, and the page that reaches it is the last.
  • Meilisearch stores JSON numbers as 64-bit floats, so an integer outside ±2^53 comes back rounded, and a filter cannot tell apart values that differ only beyond it. Store such values as strings.
  • A Meilisearch task is atomic, so everything Meilisearch would reject per document is identified before the task is enqueued and reported in FailedItems (INVALID_ARGUMENT): content that uses id, daprMetadata, daprPayload or an attribute starting with _ (Meilisearch's own namespace), which could not be read back as written, and an ID that is not 1 to 511 bytes of ASCII letters, digits, - and _. GetDocuments omits such an ID like any other missing document.
  • IndexDocuments requires an existing index and returns NOT_FOUND otherwise: Meilisearch would create the index on the first document addition, without the settings CreateIndex applies. The check cannot cover an index deleted while the write is queued, which Meilisearch then recreates without those settings; delete it and call CreateIndex again. ListIndexes reads every page of the index list.
  • Filter field names are quoted, so a filter key is never read as filter syntax. A native query cannot set pagination or sorting, its filter is ANDed with the portable filter, and the document ID, its metadata and the ranking score are returned whatever it projects.
  • Highlights holds the requested HighlightFields only, a nested field under the path it was requested with.
  • INDEXING_MODE_WAIT_FOR_COMPLETION polls the task status API with exponential backoff by default. When the experimental tasksStreamingRoute feature is enabled (and the API key has tasks.get) the component shares one task-change stream, filtered to document addition/update and deletion tasks, and falls back to polling for that and every later wait if the route reports itself unavailable.

Documentation

Overview

Package search defines the Search building-block contract used by Dapr's search.proto runtime API. Components implement Search to store, retrieve and query index-ready documents in lexical / structured search backends.

Vector similarity is intentionally a separate building block; see the sibling vector package.

Index

Constants

View Source
const (
	// ErrorDomain is the google.rpc.ErrorInfo domain used by search and vector
	// errors.
	ErrorDomain = "dapr.io"

	// ReasonContinuationExpired is set when a provider cursor referenced by a
	// continuation token has expired. Returned with FAILED_PRECONDITION.
	ReasonContinuationExpired = "SEARCH_CONTINUATION_EXPIRED"

	// ReasonIndexingOutcomeUnknown is set when a provider reports a batch-level
	// failure but cannot establish which items were applied.
	ReasonIndexingOutcomeUnknown = "INDEXING_OUTCOME_UNKNOWN"
)

Variables

This section is empty.

Functions

func ContinuationExpiredError

func ContinuationExpiredError(msg string) error

ContinuationExpiredError returns the FAILED_PRECONDITION error used when a provider cursor has expired.

func IndexingOutcomeUnknownError

func IndexingOutcomeUnknownError(code codes.Code, msg string) error

IndexingOutcomeUnknownError wraps a batch-level provider failure whose per-item outcome cannot be established.

func NewErrorWithReason

func NewErrorWithReason(code codes.Code, reason, msg string) error

NewErrorWithReason returns a status error with a google.rpc.ErrorInfo detail carrying reason.

func ValidateDocumentContent

func ValidateDocumentContent(content []byte) error

ValidateDocumentContent checks that content is a UTF-8 encoded JSON object, the normative alpha document encoding. The returned error is an INVALID_ARGUMENT status suitable for a FailedItem.

func ValidateIndexingOptions

func ValidateIndexingOptions(ctx context.Context, opts IndexingOptions, supportsQueuedAck bool) error

ValidateIndexingOptions validates the portable IndexingOptions rules. supportsQueuedAck reports whether the provider offers a native durable queued acknowledgement; CONTINUE_ASYNC is rejected when it does not. Returned errors are INVALID_ARGUMENT status errors.

func ValidateWriteIDs

func ValidateWriteIDs(ids []string) error

ValidateWriteIDs checks that every ID of a keyed upsert is non-empty and unique within the request. Returned errors are INVALID_ARGUMENT status errors.

Types

type CreateIndexRequest

type CreateIndexRequest struct {
	Index string
	// Component-specific index settings.
	Metadata map[string]string
}

CreateIndexRequest is the input to Search.CreateIndex.

type DeleteDocumentsRequest

type DeleteDocumentsRequest struct {
	Index    string
	IDs      []string
	Metadata map[string]string
	Options  IndexingOptions
}

DeleteDocumentsRequest is the input to Search.DeleteDocuments. IDs that do not exist are not an error.

type DeleteDocumentsResponse

type DeleteDocumentsResponse struct {
	// Always IndexAckQueued or IndexAckCompleted on success.
	Ack IndexAck
}

DeleteDocumentsResponse is the output of Search.DeleteDocuments.

type DeleteIndexRequest

type DeleteIndexRequest struct {
	Index    string
	Metadata map[string]string
}

DeleteIndexRequest is the input to Search.DeleteIndex.

type Document

type Document struct {
	ID string
	// Content is a UTF-8 encoded JSON object. Field-level operations address
	// its keys with dotted paths. The runtime rejects content that is not a
	// JSON object before it reaches the component.
	Content []byte
	// Metadata is opaque caller metadata stored with the document and
	// returned unchanged. It is not indexed or filterable.
	Metadata map[string]string
}

Document is a single index-ready document.

type FailedItem

type FailedItem struct {
	ID string
	// Error carries a canonical, non-OK code.
	Error *status.Status
}

FailedItem is an item-specific write failure shared by document indexing and vector upserts.

type GetDocumentsRequest

type GetDocumentsRequest struct {
	Index          string
	IDs            []string
	IncludeContent bool
	Metadata       map[string]string
}

GetDocumentsRequest is the input to Search.GetDocuments.

type GetDocumentsResponse

type GetDocumentsResponse struct {
	Documents []Document
}

GetDocumentsResponse is the output of Search.GetDocuments. Documents are returned in request order; documents that are not found are omitted.

type GetIndexRequest

type GetIndexRequest struct {
	Index    string
	Metadata map[string]string
}

GetIndexRequest is the input to Search.GetIndex.

type GetIndexResponse

type GetIndexResponse struct {
	Index string
	// Approximate document count. Providers that cannot supply this value
	// efficiently may return 0.
	DocumentCount uint64
	Properties    map[string]string
}

GetIndexResponse is the output of Search.GetIndex.

type Hit

type Hit struct {
	Document Document
	// Score is an unnormalized provider-specific relevance score. Higher
	// values indicate a more relevant match.
	Score      float64
	Highlights map[string]string
}

Hit is a single result entry returned from a Search call.

type IndexAck

type IndexAck int32

IndexAck is the acknowledgement boundary reached by a write. Mirrors search.proto IndexAck.

const (
	// IndexAckUnspecified is never returned by a successful write.
	IndexAckUnspecified IndexAck = 0
	// IndexAckQueued means the provider accepted the request for asynchronous
	// processing. It does not indicate that any item was applied.
	IndexAckQueued IndexAck = 1
	// IndexAckCompleted means the provider completed the write and
	// FailedItems contains every item-specific failure.
	IndexAckCompleted IndexAck = 2
)

type IndexDocumentsRequest

type IndexDocumentsRequest struct {
	Index     string
	Documents []Document
	Metadata  map[string]string
	Options   IndexingOptions
}

IndexDocumentsRequest is the input to Search.IndexDocuments. It is a keyed upsert: every document has a non-empty ID that is unique within the request.

type IndexDocumentsResponse

type IndexDocumentsResponse struct {
	// Item-specific failures known at the acknowledgement boundary.
	FailedItems []FailedItem
	// Always IndexAckQueued or IndexAckCompleted on success.
	Ack IndexAck
}

IndexDocumentsResponse is the output of Search.IndexDocuments.

type IndexingMode

type IndexingMode int32

IndexingMode selects the acknowledgement boundary a write returns at.

const (
	// IndexingModeUnspecified behaves exactly as IndexingModeReturnOnAcceptance.
	IndexingModeUnspecified IndexingMode = 0
	// IndexingModeWaitForCompletion waits for a final provider result.
	IndexingModeWaitForCompletion IndexingMode = 1
	// IndexingModeReturnOnAcceptance returns after the provider durably
	// accepts the write for background processing.
	IndexingModeReturnOnAcceptance IndexingMode = 2
)

type IndexingOptions

type IndexingOptions struct {
	Mode          IndexingMode
	WaitTimeout   time.Duration
	OnWaitTimeout IndexingWaitTimeoutAction
}

IndexingOptions mirrors search.proto IndexingOptionsAlpha1.

type IndexingWaitTimeoutAction

type IndexingWaitTimeoutAction int32

IndexingWaitTimeoutAction selects what happens when WaitTimeout expires.

const (
	IndexingWaitTimeoutActionUnspecified IndexingWaitTimeoutAction = 0
	// IndexingWaitTimeoutActionContinueAsync returns IndexAckQueued and lets
	// the durably queued provider task continue.
	IndexingWaitTimeoutActionContinueAsync IndexingWaitTimeoutAction = 1
	// IndexingWaitTimeoutActionFailRequest returns DEADLINE_EXCEEDED.
	IndexingWaitTimeoutActionFailRequest IndexingWaitTimeoutAction = 2
)

type ListIndexesRequest

type ListIndexesRequest struct {
	Metadata map[string]string
}

ListIndexesRequest is the input to Search.ListIndexes.

type ListIndexesResponse

type ListIndexesResponse struct {
	Indexes []string
}

ListIndexesResponse is the output of Search.ListIndexes.

type Metadata

type Metadata struct {
	metadata.Base `json:",inline"`
}

Metadata represents a set of search-component-specific properties.

type Search interface {
	metadata.ComponentWithMetadata

	// Init is called once per component instance with the resolved metadata.
	Init(ctx context.Context, meta Metadata) error

	// Index lifecycle.
	CreateIndex(ctx context.Context, req *CreateIndexRequest) error
	GetIndex(ctx context.Context, req *GetIndexRequest) (*GetIndexResponse, error)
	ListIndexes(ctx context.Context, req *ListIndexesRequest) (*ListIndexesResponse, error)
	DeleteIndex(ctx context.Context, req *DeleteIndexRequest) error

	// Document operations.
	IndexDocuments(ctx context.Context, req *IndexDocumentsRequest) (*IndexDocumentsResponse, error)
	GetDocuments(ctx context.Context, req *GetDocumentsRequest) (*GetDocumentsResponse, error)
	DeleteDocuments(ctx context.Context, req *DeleteDocumentsRequest) (*DeleteDocumentsResponse, error)

	// Query.
	Search(ctx context.Context, req *SearchRequest) (*SearchResponse, error)

	io.Closer
}

Search is the Search building-block component contract. Method shapes mirror the alpha1 RPCs in dapr/proto/runtime/v1/search.proto using Go-native types so the runtime can adapt between gRPC, HTTP/JSON and component invocations without leaking proto types into components.

Errors returned by components SHOULD be gRPC status errors carrying a canonical code (see google.golang.org/grpc/status). Other errors are reported to callers as INTERNAL.

type SearchRequest

type SearchRequest struct {
	Index             string
	Text              string
	Native            map[string]any
	Filter            map[string]any
	TopK              uint32
	ContinuationToken string
	ReturnFields      []string
	IncludeContent    bool
	SearchFields      []string
	Sort              []SortClause
	HighlightFields   []string
	Metadata          map[string]string
}

SearchRequest is the input to Search.Search. Text and Native are mutually exclusive: at most one of them is set. A request with neither matches every document, subject to Filter.

type SearchResponse

type SearchResponse struct {
	Hits []Hit
	// Best-effort total. Nil when the provider cannot supply one.
	TotalHits *uint64
	// Opaque token for the next page. Empty when there are no more results.
	ContinuationToken string
	// Accuracy of TotalHits. Unspecified when TotalHits is nil.
	TotalHitsRelation TotalHitsRelation
}

SearchResponse is the output of Search.Search.

type SortClause

type SortClause struct {
	Field string
	Order SortOrder
}

SortClause expresses a single sort key.

type SortOrder

type SortOrder int32

SortOrder selects the direction of a sort clause. Mirrors search.proto.

const (
	SortOrderUnspecified SortOrder = 0
	SortOrderAsc         SortOrder = 1
	SortOrderDesc        SortOrder = 2
)

type TotalHitsRelation

type TotalHitsRelation int32

TotalHitsRelation describes the accuracy of SearchResponse.TotalHits.

const (
	TotalHitsRelationUnspecified TotalHitsRelation = 0
	TotalHitsRelationExact       TotalHitsRelation = 1
	TotalHitsRelationLowerBound  TotalHitsRelation = 2
	TotalHitsRelationEstimate    TotalHitsRelation = 3
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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