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 text-based fields, equality for everything else. This can be modified by passing lambdas to searchable for the given field.
Global search
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.
Disable search
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)
Enable search
You can also enable search for fields that don’t enable it by default:
Field::Number.new(:employee_number).searchable(true)
For text-based fields (string, text) Uchi performs a partial match using LIKE (ILIKE in PostgreSQL). For every other field type, the search term is cast to the field’s type and matched by equality; if the term can’t be cast (e.g. "abc" against an :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. searchable(true) has no effect on them.
Customize search
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:
context: TheUchi::Contextwe’re currently processing.query: TheActiveRecord::Relationthat makes up the current database queryterm: 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}%")
})