Skip to content
How-to guides and worked examplesSee the guides →

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:

ruby
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
end

Resource 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.

ruby
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 all Avo::ExecutionContext attributes
  • 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:

OptionDescriptionDefaultPossible Values
titleThe title of the resultResource titleAny string
descriptionThe description of the resultnilAny string
image_urlThe URL of the image to display in the resultnilAny valid URL
image_formatThe format of the image to display in the result:circle:square, :rounded, :circle
pathThe path to navigate to when clicking the resultRecord show pageAny valid path
ruby
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.

ruby
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.

ruby
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:

ruby
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.

ValueSurface
:resourceresource-index search bar
:globalnavbar ⌘K palette and its dedicated results page
:associationsearchable association picker on edit forms and the attach modal
:kanbankanban 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-on

Keys 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.

ruby
config.global_search = {
  enabled: -> { current_user.is_admin? }
}

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.

ruby
config.global_search = {
  navigation_section: false
}

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.

ruby
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.

ruby
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.

SurfaceArrayRelation
Global search (⌘K palette and its results page)YesYes
Searchable associationsYesYes
Resource index search barNoYes
Kanban board card pickerNoYes

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:

ruby
# 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:

ruby
{
  _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
}
KeyDescription
_idThe record's identifier
_labelRow title; the search term is highlighted inside it
_urlWhere clicking the row navigates
_descriptionOptional row description
_avatarOptional 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.