Search

Perform searches and display results in your templates.


Overview

An overview on how to configure search, indexing, and the query form can be found in the Search Docs.

Example

On a search result page, you can loop through the results of the search like they were entries or pages. Because they are. You’ll have access to all the data of all the content returned so you can format your results however you’d like. In this example, we’re displaying a truncated version of the content field.

{{ search:results }}

  {{ if no_results }}
    <h2>No results.</h2>
  {{ else }}

    <a href="{{ url }}" class="result">
      <h2>{{ title }}</h2>
      <p>{{ content strip_tags="img|a|p" safe_truncate="180|..." }}</p>
    </a>

  {{ /if }}

{{ /search:results }}

Parameters

fields

array

Narrow down your search by looking in specific fields. The fields you select must be located in the corresponding index. You can pipe-separate multiple fields, eg. title|content.

Note: If you're using Algolia and have set up searchable attributes, you may only specify fields exist in there. Any additional fields will cause you the search tag to yield no results.

collection

string

The name of a collection, to search through its index. If this parameter is not provided, the default index will be used.

param

string q

The query string parameter used for the search term.

supplement_data

string true

When this is true it will convert search results to the appropriate content objects, which makes all their fields available in your templates. Disabling this will result in a performance increase, but only indexed fields will be available.

supplement_taxonomies

boolean true

By default, Statamic will convert taxonomy term values into actual term objects that you may loop through. This has some performance overhead, so you may disable this for a speed boost if taxonomies aren't necessary.


Variables

no_results

boolean

If there are no results.

first

boolean

If this is the first item in the loop.

last

boolean

If this is the last item in the loop.

count

integer

The number of current iteration in the loop.

index

integer

The zero-based count of the current iteration in the loop.

total_results

integer

The number of results in the loop.

search_score

float

The internal relevance score that Statamic given to this result. Helpful for debugging, but useless to the public. Only applies when using the local driver.

content data

array

Each page being iterated has access to all the variables inside that page. This includes things like title, content, etc. When the supplement_data parameter has been set to false, only indexed fields will be available.

is_page

boolean

Whether the current item is a page.

is_entry

boolean

Whether the current item is an entry.

is_term

boolean

Whether the current item is a taxonomy term.

_highlightResult

array

When using the Algolia driver, this array will be available as per their documentation. You can use this to output a field with the search term automatically highlighted. eg. {{ _highlightResult:myfield:value }}

Last modified on August 22, 2017