Creating multiple search indexes | laravel-site-search-go | Spatie       

 SPATIE  

laravel-site-search-go
======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-site-search-go](https://spatie.be/docs/laravel-site-search-go/v1)  Advanced-usage  Creating multiple search indexes

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-site-search-go/v1) 

  Creating multiple search indexes    
- [ Introduction ](https://spatie.be/docs/laravel-site-search-go/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-site-search-go/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-site-search-go/v1/requirements)
- [ Installation and setup ](https://spatie.be/docs/laravel-site-search-go/v1/installation-setup)
- [ About us ](https://spatie.be/docs/laravel-site-search-go/v1/about-us)
- [ Questions and issues ](https://spatie.be/docs/laravel-site-search-go/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-site-search-go/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-site-search-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-site-search-go/v1/releasing)

Basic usage
-----------

- [ High level overview ](https://spatie.be/docs/laravel-site-search-go/v1/basic-usage/high-level-overview)
- [ Indexing your first site ](https://spatie.be/docs/laravel-site-search-go/v1/basic-usage/indexing-your-first-site)
- [ Retrieving results ](https://spatie.be/docs/laravel-site-search-go/v1/basic-usage/retrieving-results)
- [ Preventing content from being indexed ](https://spatie.be/docs/laravel-site-search-go/v1/basic-usage/preventing-content-from-being-indexed)
- [ Using a search profile ](https://spatie.be/docs/laravel-site-search-go/v1/basic-usage/using-a-search-profile)
- [ Listing indexes ](https://spatie.be/docs/laravel-site-search-go/v1/basic-usage/listing-indexes)
- [ Troubleshooting ](https://spatie.be/docs/laravel-site-search-go/v1/basic-usage/troubleshooting)

Advanced usage
--------------

- [ Creating multiple search indexes ](https://spatie.be/docs/laravel-site-search-go/v1/advanced-usage/creating-multiple-search-indexes)
- [ Using a custom indexer ](https://spatie.be/docs/laravel-site-search-go/v1/advanced-usage/using-a-custom-indexer)
- [ Indexing extra properties ](https://spatie.be/docs/laravel-site-search-go/v1/advanced-usage/indexing-extra-properties)
- [ Available events ](https://spatie.be/docs/laravel-site-search-go/v1/advanced-usage/available-events)
- [ Using the database driver ](https://spatie.be/docs/laravel-site-search-go/v1/advanced-usage/using-the-database-driver)
- [ Using the Meilisearch driver ](https://spatie.be/docs/laravel-site-search-go/v1/advanced-usage/using-the-meilisearch-driver)
- [ Building an admin integration ](https://spatie.be/docs/laravel-site-search-go/v1/advanced-usage/filament-integration)

 Creating multiple search indexes
================================

###  On this page 

1. [ Custom models and jobs ](#content-custom-models-and-jobs)

This package can crawl and create search indexes for multiple sites.

Run `site-search:create-index` for each site, or call `Service.CreateIndex`. Each call saves a configuration. Executing `site-search:crawl` dispatches an indexing job for each enabled configuration.

You can update these fields through the configured Store:

- `Name` is the logical name used when searching.
- `Enabled` controls whether `CrawlAll` and the crawl command dispatch the site.
- `CrawlURL` is the URL to crawl.
- `Driver` selects a registered driver. An empty string uses `DefaultDriver`.
- `Profile` selects a registered search profile. An empty string uses `DefaultProfile`.
- `IndexBaseName` contributes to the physical index name. The configuration ID also scopes cleanup.
- `Extra` contains driver-specific or application settings. See the database and Meilisearch driver pages.

For example:

```
service := sitesearch.From(ctx)
site, err := service.Store.FindByName(ctx, "blog")
if err != nil {
    return err
}
site.Enabled = false
site.Profile = "blog"
return service.Store.Save(ctx, &site)
```

The package owns `IndexName`, `PendingIndexName`, `NumberOfURLsIndexed`, `URLsFound`, `URLsFailed`, `FinishReason`, `CrawlingStartedAt` and `CrawlingEndedAt`. Do not edit them to trigger a crawl. Call `Crawl` or dispatch your configured crawl job instead.

Custom models and jobs
--------------------------------------------------------------------------------------------------------------------------

Go has no PHP model subclasses. Set `cfg.Store` to an adapter for your own model. Every configuration creation, lookup, crawl job, status write, search and list command uses this contract:

```
type Store interface {
    Create(context.Context, *sitesearch.SiteSearchConfig) error
    Find(context.Context, int64) (sitesearch.SiteSearchConfig, error)
    FindByName(context.Context, string) (sitesearch.SiteSearchConfig, error)
    List(context.Context) ([]sitesearch.SiteSearchConfig, error)
    Save(context.Context, *sitesearch.SiteSearchConfig) error
}
```

`Create` must assign the saved ID. Use stable IDs unique within the application's Store. `Save` must persist configuration state without discarding your model's extra fields. `List` returns all configurations, including disabled ones. The package filters enabled configurations when dispatching jobs. `ModelStore` is the default adapter. The repository's `package_test.go` contains a working custom model adapter, also exercised through the database queue worker in `crawl_test.go`.

To replace the crawl job, supply a concrete serializable type:

```
type CrawlBlog struct {
    ConfigID int64 `json:"config_id"`
}

func (j CrawlBlog) Handle(ctx context.Context) error {
    return sitesearch.From(ctx).Crawl(ctx, j.ConfigID)
}
```

Select it in your configuration callback:

```
cfg.CrawlJob = func(id int64) sitesearch.CrawlJob {
    return CrawlBlog{ConfigID: id}
}
```

The package calls this factory with ID zero during registration to register the actual selected job type. It calls the factory again for each configuration being dispatched. Always return the same concrete type. Keep contexts, functions and live database handles out of serialized fields. A custom job can implement Laravel Go's `JobOptions` to control its queue timeout and retries. Calling `Service.Crawl` inside the job retains the package's crawl timeout, shared lock and promotion behavior.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
