# search

> Package search is the seam WebSearch reaches a search engine through.

<!-- Generated from the Go source of github.com/barevoidco/zelk/core/search (cmd/zelk/internal/reference TestReferenceGoAPI). Don't edit: change the code, then run make reference. -->

```go
import "github.com/barevoidco/zelk/core/search"
```

Package search is the seam WebSearch reaches a search engine through. Hosts inject a backend, such as a provider's native search adapter, independently of the session model. Without a backend the tool is absent.


## Backend

```go
type Backend interface {
	Search(ctx context.Context, q Query) (Response, error)
}
```

Backend searches the web. Implementations must be safe for concurrent use and honour ctx.

**Example**

```go
var backend search.Backend = index{
	{Title: "Go documentation", URL: "https://go.dev/doc/", Snippet: "Documentation for Go."},
	{Title: "Other reference", URL: "https://example.org/reference", Snippet: "A different source."},
}
results, err := backend.Search(context.Background(), search.Query{Text: "Go documentation", Max: 1, AllowedDomains: []string{"go.dev"}})
if err != nil {
	panic(err)
}
for _, result := range results.Results {
	fmt.Println(result.Title, result.URL)
}
// Output: Go documentation https://go.dev/doc/
```

Output:

```text
Go documentation https://go.dev/doc/
```


## Limiter

```go
type Limiter interface {
	Limit() event.Tokens
}
```

Limiter is an optional Backend capability for model-backed searches. Limit bounds all input (including server search context) and output tokens per call. Zero means unknown; hard budgets refuse unknown bounds. Implementations must be safe for concurrent use.

**Example**

```go
var backend search.Backend = boundedIndex{}
if limiter, ok := backend.(search.Limiter); ok {
	limit := limiter.Limit()
	fmt.Println(limit.Input, limit.Output)
}
// Output: 200000 2048
```

Output:

```text
200000 2048
```


## Query

```go
type Query struct {
	Text string
	// Max is the most results wanted.
	Max int
	// AllowedDomains, if any, limits results to these domains;
	// BlockedDomains excludes some.
	AllowedDomains, BlockedDomains []string
}
```

Query is one search.


## Response

```go
type Response struct {
	Results []Result
	Usage   *event.Usage
}
```

Response is the results and usage of a search. Usage may be non-nil even when Search returns an error, if the provider charged for the request.


## Result

```go
type Result struct {
	Title, URL, Snippet string
}
```

Result is one search result.
