Retrieving results | 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  Retrieving results

 Version   v1      

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

  Retrieving results    
- [ 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)

 Retrieving results
==================

###  On this page 

1. [ Getting results ](#content-getting-results)
2. [ Limiting results ](#content-limiting-results)
3. [ Paginating results ](#content-paginating-results)
4. [ Deep linking to sections ](#content-deep-linking-to-sections)
5. [ Hit properties ](#content-hit-properties)
6. [ Driver-specific parameters ](#content-driver-specific-parameters)

You can retrieve results from an index using `sitesearch.From(ctx).Search`.

Getting results
-----------------------------------------------------------------------------------------------------

Here's how you can retrieve results from an index named `my-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())
}
```

Pass a request or a job's context so the package can access its application. An empty query returns `sitesearch.ErrNoQuery`. A configuration that has not built its first physical index returns no hits.

Limiting results
--------------------------------------------------------------------------------------------------------

The default limit is 20. Set `Limit` to a pointer to change it. A pointer to zero returns no hits, while preserving the total count. `Offset` skips matching hits.

```
limit := 10
results, err := sitesearch.From(ctx).Search(ctx, "my-index", "your query", sitesearch.SearchOptions{
    Limit: &limit,
    Offset: 20,
})
```

`Results` contains `Hits`, `TotalCount`, `ProcessingTimeInMs`, `Limit` and `Offset`. Meilisearch's total count may be an estimate.

Paginating results
--------------------------------------------------------------------------------------------------------------

Call `Paginate` with the page number and page size:

```
page, err := sitesearch.From(ctx).Paginate(ctx, "my-index", "your query", 2, 20, nil)
```

Page numbers start at 1. `HasMore` tells you whether another page exists. `Hits` contains at most the requested page size.

Deep linking to sections
--------------------------------------------------------------------------------------------------------------------------------

Search results can link directly to a section of a page. When your HTML headings have `id` attributes, the indexer associates them with nearby text:

```
<h2 id="installation">Installation</h2>
<p>To install the package, run...</p>

<h2 id="configuration">Configuration</h2>
<p>After installation, configure...</p>
```

Use `hit.URLWithAnchor()` to get the full URL including the fragment. If no anchor is available, it returns the base URL. The database driver combines the page's entries into one document and keeps the first nonempty anchor. Meilisearch and the array driver retain individual entries.

Hit properties
--------------------------------------------------------------------------------------------------

`Title()` falls back from the page title to the first heading. `Snippet()` chooses the longest of the entry, description and first heading. `HighlightedSnippet()` returns that property's highlighted value when the driver supplies one. Highlight markers are `<em>` tags.

Treat indexed content as untrusted. Escape text before displaying it. If you render highlighted HTML, sanitize it and only allow the highlight tags your application needs.

`DateModified()` returns a nullable time. Custom properties are in `hit.Extra`, rather than dynamic top-level Go fields. Meilisearch's `_formatted` object is available in `hit.Formatted`, including nested formatted custom properties.

Driver-specific parameters
--------------------------------------------------------------------------------------------------------------------------------------

`SearchOptions.Parameters` passes additional parameters to Meilisearch. The database and array drivers ignore it. Use it for options such as filters and sorting. Meilisearch must have the relevant filterable or sortable attributes configured first.

 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)
