Indexing your first site | 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)  Basic-usage  Indexing your first site

 Version   v1      

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

  Indexing your first site    
- [ 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)

 Indexing your first site
========================

On this page, you'll learn how to create an index, populate it by crawling your site, and retrieve results.

First, run this command to define a site that needs to be indexed:

```
laravel site-search:create-index
```

This command asks for a name for your index and the URL of your site. You can also pass them directly:

```
laravel site-search:create-index --name=my-index --url=https://example.com
```

After that, run this command to dispatch a job that crawls your site and puts the content in a search index:

```
laravel site-search:crawl
```

Make sure your queue worker is running. To crawl immediately in the command process instead, use `laravel site-search:crawl --sync`.

Finally, use the package service to query your index:

```
results, err := sitesearch.From(ctx).Search(ctx, "my-index", "your query", sitesearch.SearchOptions{})
if err != nil {
    return err
}
for _, hit := range results.Hits {
    fmt.Println(hit.Title(), hit.URLWithAnchor(), hit.Snippet())
}
```

Import the package as `sitesearch "github.com/spatie/laravel-site-search-go"`. Pass a Laravel request, job or command context. In a web handler, return `web.JSON(results)` or pass the hits to your application's view. Escape titles, URLs and snippets when rendering HTML. Highlighted snippets also need sanitization before being rendered as HTML.

You can also create and crawl a configuration from Go:

```
service := sitesearch.From(ctx)
site, err := service.CreateIndex(ctx, "my-index", "https://example.com")
if err != nil {
    return err
}
return service.Crawl(ctx, site.ID)
```

The command crawls all enabled sites. `Crawl(ctx, id)` explicitly crawls that one configuration, even if its `Enabled` flag is false.

 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)
