Search

Uchi offers search across your repositories and fields out of the box.

Search is configured on a field-by-field basis. If a repository contains at least one searchable Field, a search field appears on its index page.

The search is fairly naive and is a bunch of conditions strung together by OR: LIKE '%term%' for string and text columns, equality for everything else. This can be modified by passing lambdas to searchable for the given field.

To change the search input placeholder, see Translations.

Global search is accessible via the search icon in the navigation, which is shown when at least one repository has a searchable field. It searches across all repositories with at least one searchable field and shows up to 5 results per repository.

Whether a field is searchable by default depends on the field type; see the Search section of each field’s page under Fields. To toggle searchability for a field use the searchable method:

Field::String.new(:password).searchable(false)

You can also enable search for fields that don’t enable it by default:

Field::Number.new(:employee_number).searchable(true)

The match depends on the database column type, not the field class. For string and text columns Uchi performs a partial match using LIKE (ILIKE in PostgreSQL). For every other column type, the search term is cast to the column’s type and matched by equality; if the term can’t be cast (e.g. "abc" against an integer id), the field is skipped.

Fields that aren’t backed by a column on the model’s table, like associations and attachments, can only be searched by passing a lambda, see Customize search. On attachments, searchable(true) is ignored. On other fields, it shows the search field, but the field never matches.

By default the search is performed using LIKE/equality as described above. To customize how a field is searched, pass a lambda to the searchable method instead of true/false:

Field::String.new(:number).searchable(lambda { |context:, query:, term:|
  # Remove space characters before searching
  term = term.tr(" ", "")
  query.where("REPLACE(number, ' ', '') LIKE ?", "%#{term}%")
})

The lambda receives the following arguments:

  1. context: The Uchi::Context we’re currently processing.
  2. query: The ActiveRecord::Relation that makes up the current database query
  3. term: The search term entered by the user

The lambda should return an ActiveRecord::Relation matching the records for that term. Results from lambda-based searchable fields are combined with results from other searchable fields on the repository.

Search for columns in an associated table

The searchable lambda isn’t limited to the field’s own attributes — it can also search by columns in other tables/models. If you have an Employee model that belongs_to :company and you want to find Employee records by querying for the Company name, you can configure the field like this:

Field::BelongsTo.new(:company).searchable(lambda { |query:, term:|
  query.joins(:company).where("companies.name LIKE ?", "%#{term}%")
})