Search API
Per-option reference for search. For task-oriented documentation and worked examples, see the Search guide.
Resource-level options live in the self.search hash on the resource class; global search options live in config/initializers/avo.rb:
class Avo::Resources::User < Avo::BaseResource
self.search = {
query: -> { query.ransack(name_cont: q, m: "or").result(distinct: false) },
item: -> { {title: record.name} },
hide_on_global: false,
display_count: true
}
end
Avo.configure do |config|
config.global_search = {enabled: true, navigation_section: true, search_on_type: true}
config.search_results_count = 8
endResource configuration
-> query
The search query, executed with Avo::ExecutionContext whenever any search surface runs against this resource. Without it, the resource has no Index search bar and is skipped by the global search.
self.search = {
query: -> { query.ransack(name_cont: q, m: "or").result(distinct: false) }
}- Type: Proc
- Default:
nil— search disabled for the resource - Locals:
q(stripped search string),query(base scope with authorization scopes applied),params,search_type, plus allAvo::ExecutionContextattributes - Return: an
ActiveRecord::Relation, or an Array of hashes for custom search providers
-> item
Configures how each result row renders in the global search palette, the direct-match section, and searchable association pickers. The resource-index search bar is unaffected — it re-renders the regular index views. The proc has access to record and resource and returns a hash with the following keys:
| Option | Description | Default | Possible Values |
|---|---|---|---|
title | The title of the result | Resource title | Any string |
description | The description of the result | nil | Any string |
image_url | The URL of the image to display in the result | nil | Any valid URL |
image_format | The format of the image to display in the result | :circle | :square, :rounded, :circle |
path | The path to navigate to when clicking the result | Record show page | Any valid path |
self.search = {
query: -> { query.ransack(name_cont: q, m: "or").result(distinct: false) },
item: -> do
{
title: "[#{record.id}] #{record.name}",
description: record.truncated_body,
image_url: main_app.url_for(record.cover_photo),
image_format: :rounded,
path: avo.resources_post_path(record, custom: "search")
}
end
}- Type: Proc returning a Hash
- Default:
nil— rows render the record's title with no description or image
-> hide_on_global
Excludes the resource from the global search — the palette results, the direct-match lookup, and the dedicated results page. The search bar on the resource's own Index view keeps working.
self.search = {
query: -> { query.ransack(id_eq: q, m: "or").result(distinct: false) },
hide_on_global: true
}- Type: Boolean
- Default:
false
-> display_count
Whether the global search shows result counts in each resource's header — "Users (8 of 21)". Counting runs an extra query per resource; disable it on large datasets or when a custom search provider can't count.
self.search = {
query: -> { query.ransack(name_cont: q, m: "or").result(distinct: false) },
display_count: false
}A lambda works too, evaluated with access to all attributes of Avo::ExecutionContext:
self.search = {
display_count: -> { current_user.admin? }
}- Type: Boolean or Proc
- Default:
true
search_type
A local injected into the query proc identifying which surface triggered the search, so one proc can serve different queries per surface.
| Value | Surface |
|---|---|
:resource | resource-index search bar |
:global | navbar ⌘K palette and its dedicated results page |
:association | searchable association picker on edit forms and the attach modal |
:kanban | kanban board card picker |
INFO
search_type is injected by add-ons: the global search add-on sets it for the index, global, and association surfaces, and the kanban board sets :kanban. Other callers run the proc without it: Avo's own search without the global search add-on, the AI assistant, and the MCP server. Referencing it there raises an error, so guard with defined?(search_type).
Global search configuration
License: Add-onKeys of the config.global_search hash in config/initializers/avo.rb. Also listed in the customization API.
-> enabled
Whether the global search renders at all — the navbar trigger, the Cmd + K shortcut, and the dedicated results page.
config.global_search = {
enabled: -> { current_user.is_admin? }
}- Type: Boolean or Proc (evaluated with
Avo::ExecutionContext) - Default:
true
-> navigation_section
Whether the palette shows the "Go to" section — links to the Index page of every resource the current user can access, filtered as the user types.
config.global_search = {
navigation_section: false
}- Type: Boolean or Proc (evaluated with
Avo::ExecutionContext) - Default:
true
-> search_on_type
Whether typing in the palette triggers the search automatically. When false, the dropdown still opens on focus or Cmd + K, but the user must press Enter to run the search and navigate to the dedicated results page.
For a non-empty query, "Show all results for..." is highlighted by default. Arrow keys select a matching resource in the "Go to" section; Enter then opens that resource instead.
The palette tells users about it. It shows "Press Enter to search" under its opening hint and beside "Show all results for...", translated in every locale Avo ships.
config.global_search = {
search_on_type: false
}- Type: Boolean
- Default:
true
WARNING
Unlike enabled and navigation_section, this key does not accept a lambda — any Proc is truthy, so it would behave as true.
Limiting results
-> search_results_count
How many results Avo shows per resource on search surfaces. Applied only to queries that return an ActiveRecord::Relation without a .limit() of their own — a .limit() in the query proc always wins. Arrays from custom search providers are never auto-capped. The dedicated global search results page ignores the limit and lists every match.
config.search_results_count = 16- Type: Integer
- Default:
8
The delay before a keystroke fires the search request is controlled by config.search_debounce.
Custom search providers
Only global search and searchable associations accept an Array. Both render the hashes directly as a result list. The resource index search bar and the kanban board card picker do not — they build a table from database records, sorting, filtering, scoping, and paginating the result, so they require an ActiveRecord::Relation.
| Surface | Array | Relation |
|---|---|---|
| Global search (⌘K palette and its results page) | Yes | Yes |
| Searchable associations | Yes | Yes |
| Resource index search bar | No | Yes |
| Kanban board card picker | No | Yes |
WARNING
Returning an Array to the resource index raises NoMethodError: undefined method 'order' for an instance of Array. Typing in the index search box hides that error — the request fails and the list silently keeps showing the previous, unfiltered rows.
Branch on search_type so the Array is only returned where it is rendered, and every other surface gets a relation:
# app/avo/resources/comment.rb
self.search = {
query: -> do
case (search_type if defined?(search_type))
when :global, :association
[
{_id: 1, _label: "Record One", _url: "https://example.com/1"},
{_id: 2, _label: "Record Two", _url: "https://example.com/2"},
{_id: 3, _label: "Record Three", _url: "https://example.com/3"}
]
else # the resource index and kanban card picker — these require a relation
query.ransack(id_eq: params[:q], m: "or").result(distinct: false)
end
end
}Branching this way round matters: the relation is the fallback, so a surface that doesn't inject search_type still gets a query it can use.
When the query proc returns an Array, each element must be a hash with this structure:
{
_id: 1,
_label: "The label",
_url: "https://example.com/records/1",
_description: "Some description about the record",
_avatar: "https://example.com/avatar.jpg",
_avatar_type: :rounded
}| Key | Description |
|---|---|
_id | The record's identifier |
_label | Row title; the search term is highlighted inside it |
_url | Where clicking the row navigates |
_description | Optional row description |
_avatar | Optional image URL |
_avatar_type | :square, :rounded, or :circle |
WARNING
With array results the result count is not available, and search_results_count is not applied — cap the array in the proc with .first(N) if needed.