Search
Search photo titles with a local Bleve index. GoForj keeps the schema in app/search.go and generates a named accessor from your search configuration. This example indexes one photo and verifies that a query returns it.
Enable Search
Start with a Project created in Quickstart. Stop forj dev before continuing. Bleve allows one process to own each index path.
Add search to the existing render.components list in .goforj.yml, then install its providers from the Project root.
forj renderSet these values in .env. The named path selects the physical directory for the photos index.
SEARCH_DRIVER=bleve
SEARCH_SUPPORTED_DRIVERS=bleve
SEARCH_PATH=storage/search
SEARCH_PHOTOS_PATH=storage/search/photosReplace app/search.go with the schema below. Keep the map key and Definition.Name identical.
package app
import "github.com/goforj/search"
// SearchDefinitions declares the photo projection owned by this app.
func SearchDefinitions() map[string]search.Definition {
return map[string]search.Definition{
"photos": {
Name: "photos",
Fields: []search.Field{
{Name: "title", Type: search.TextField, Searchable: true},
},
},
}
}Generate manager.Photos() and rebuild the app after the configuration change.
forj generate --search
forj buildProvision and Query the Index
The app validates the schema when it opens the index. Provision the schema first, while forj dev is stopped. The following check provisions the index, adds one photo, and waits for the write to become searchable.
Create tools/search-check/main.go. Replace your/module with the module path from go.mod.
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/goforj/search"
"your/module/app"
"your/module/internal/cmd"
"your/module/internal/searches"
)
// Photo contains the searchable photo metadata.
type Photo struct {
Title string `json:"title"`
}
// main reports whether the photo is searchable.
func main() {
if err := cmd.LoadEnv(); err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := check(ctx); err != nil {
log.Fatal(err)
}
}
// check releases the index before returning an error to the command.
func check(ctx context.Context) error {
definitions := app.SearchDefinitions()
if err := searches.Provision(ctx, definitions); err != nil {
return err
}
manager, err := searches.NewManager(ctx, definitions)
if err != nil {
return err
}
defer manager.Close()
index := manager.Photos()
receipt, err := search.Upsert(ctx, index, "photo-1", Photo{Title: "Sunrise at the lake"})
if err != nil {
return err
}
outcome, err := index.Wait(ctx, receipt)
if err != nil {
return err
}
if outcome.State != search.OutcomeSucceeded {
return fmt.Errorf("indexing failed: %v", outcome)
}
page, err := search.SearchAs[Photo](ctx, index, search.Query{Text: "sunrise"})
if err != nil {
return err
}
for _, hit := range page.Hits {
fmt.Println(hit.ID, hit.Value.Title)
}
return nil
}Verify Search
Run the check from the Project root. Run it again to verify that the index can reopen and update the same document ID.
go run ./tools/search-check
# photo-1 Sunrise at the lakeIn an application service, accept a *search.Index through its constructor and supply manager.Photos() from a Wire provider. The manager closes the indexes during app shutdown.
Deploy Search
Keep original photo records and image files outside the index so you can rebuild it. Run searches.Provision as a deployment step before starting the app. If an existing index has an incompatible schema, provision a new path, backfill it, then switch the app's configuration.
For multiple replicas, use an externally managed Meilisearch service. Add meilisearch to SEARCH_SUPPORTED_DRIVERS, run forj generate --search, and build a new artifact before selecting that driver. Set the service URL, API key, and an app-specific prefix through deployment configuration.
See the Search environment reference for named overrides and driver settings, the Search library for queries and filters, and deployment basics for running the built app.