The search object has the following attributes:

search.defaultsortby

Returns relevance, the default sort order for search.

search.filters

Returns an array of filter objects set up on the search page.

Only filters relevant to the current search results are returned. For search results with more than 1,000 products, the filters are empty.

search.performed

Returns true if an HTML form with the attribute action="/search" was submitted successfully. Use it to show content depending on whether a search was performed.

{% if search.performed %}

 <!-- Show search results -->
{% endif %}

search.results

Returns an array of matching search results. The elements can be:

Loop through search.results to access the attributes of each result.

{% for item in search.results %}
  <h3>{{ item.title | link_to: item.url }}</h3>
{% endfor %}

Search result object_type

Search results have an extra attribute object_type that tells the type of each result. This helps to tell result types apart when you output them.

{% for item in search.results %}
  <h3>{{ item.title | link_to: item.url }}</h3>
  {% if item.object_type == 'article' %}
    {% comment %}
      'item' is an article.
      All properties of the article object are available.
    {% endcomment %}
    {% if item.image %}
      <div class="result-image">
        <a href="{{ item.url }}">{{ item.image | image_url: width: 200 | image_tag }}</a>
      </div>
    {% endif %}
  {% elsif item.object_type == 'page' %}
    {% comment %}
      'item' is a page.
      All properties of the page object are available.
    {% endcomment %}
  {% else %}
    {% comment %}
      'item' is a product.
      All properties of the product object are available.
    {% endcomment %}
    {% if item.featured_image %}
      <div class="result-image">
        <a href="{{ item.url }}">{{ item.featured_image | image_url: width: 200 | image_tag }}</a>
      </div>
    {% endif %}
  {% endif %}

  <span>{{ item.content | strip_html | truncatewords: 40 | highlight: search.terms }}</span>
{% endfor %}

search.results_count

Returns the number of results found.

search.sort_by

Returns the sort order applied to the search by the sort_by URL parameter. Without a sort_by URL parameter, the default is relevance.

For example, for this URL:

/search?q=snowboard&sort_by=price-ascending
Sort by: {{ search.sort_by }}
Sort by: price-ascending

search.sort_options

Returns an array of sort options for the search.

For example:

<select id="sort-by">
  {% assign sort_by = search.sort_by | default: search.default_sort_by %}
  {% for option in search.sort_options %}
    <option value="{{ option.value }}" {% if option.value == sort_by %}selected="selected"{% endif %}>{{ option.name }}</option>
  {% endfor %}
</select>
<select id="sort-by">
  <option value="relevance">Relevance</option>
  <option value="price-ascending">Price, low to high</option>
  <option value="price-descending">Price, high to low</option>
</select>

Each option has these properties:

option.name

The customer-facing label of the sort option. Merchants can edit it in the language editor.

option.value

The value of the sort option that is assigned to the sort_by parameter when a form is submitted.

search.terms

Returns the string entered in the search input.

Use the highlight filter to style all matches of search.terms in the search results differently. For example:

{{ item.content | highlight: search.terms }}
<!-- If the search term was "Yellow" -->
<strong class="highlight">Yellow</strong> shirts are the best!

search.types

Returns an array of strings for the types the search was performed on. The elements can be any combination of article, page and product.

The search types are visible in the URL parameters of the search page. For example, for this URL:

/search?type=article,product&q=*
{% for type in search.types %}
  - {{ type }}
{% endfor %}
- article
- product

More properties

VersaCommerce also provides these properties on the search object:

Properties Returns
collection, performed? Search-backed collection and completion predicate