Limited offer 50% off on your first year, for new customers. Applied automatically at checkout. See pricing

Search in your Rails admin panel

The first thing an admin does when a customer calls is search for them. Name, email, order number, whatever they’ve got. If your admin panel can’t find the record from whatever scrap of information is at hand, it’s not doing its job.

By on

Search, out of the box

In Uchi you don’t set up search. You get it.

Every field in a repository knows whether it’s searchable, and the ones you’d expect to search by are searchable by default: Field::String, Field::Text, and Field::Id. So given a repository like this:

module Uchi
  module Repositories
    class Customer < Repository
      def fields
        [
          Field::Id.new(:id),
          Field::String.new(:name),
          Field::String.new(:email),
          Field::Text.new(:notes),
          Field::BelongsTo.new(:company),
        ]
      end
    end
  end
end

the customers index page gets a search box, and typing “jane” finds Jane by her name, her email address, or that note someone left about her last year. As soon as a repository has at least one searchable field, the search box is there. No search configuration, no index to build.

Search everything at once

Searching one table is nice when you know what you’re looking for. Often you don’t. Is “Acme” a customer, a company, or something someone wrote in an order note?

That’s what global search is for. Click the magnifying glass in the navigation, type your term, and Uchi searches every repository with a searchable field, showing up to 5 results from each.

Each repository loads its results in its own Turbo Frame. So if your orders table is huge and takes a while, it doesn’t hold up the customers and companies that came back instantly. You see results as they arrive.

It’s also polite about permissions: you only see results from repositories you’re allowed to list.

Choosing what’s searchable

The defaults are a starting point. Some string fields shouldn’t be searched at all, and some fields that aren’t searchable by default should be. Either way, it’s one method call on the field:

Field::String.new(:api_token).searchable(false)
Field::Number.new(:customer_number).searchable(true)

How a field is matched depends on its database column, not the field class. string and text columns get a partial match with LIKE '%term%' (or ILIKE on PostgreSQL, so it’s case-insensitive there too). Everything else is matched by equality.

And Uchi is a bit clever about that equality. The search term is cast to the column’s type first, and if it can’t be, the field is skipped. Searching for “Acme” won’t try WHERE id = 'Acme', and “12bob” won’t match the customer with id 12. Boolean columns only match terms that actually look like booleans - true, 1, on, and friends - so searching for “Jane” doesn’t suddenly match every active customer.

Deliberately naive

Let’s be honest about what this is. There’s no full-text search, no relevance ranking, no Elasticsearch cluster to babysit. Under the hood, search is a bunch of conditions strung together with OR, one per searchable field. Your search term is used as a whole, so “Jane Doe” looks for exactly that, not for “Jane” and “Doe” separately.

That’s on purpose.

Admins mostly search for things they already know exist. They have the email address from the support ticket or the order number from the invoice. They don’t need ranking, they need the record, and they need it now. A plain query against your own database is fast and predictable.

When naive isn’t enough

Sometimes the data doesn’t look like what people type. Phone numbers stored as “+45 12 34 56 78” while your support staff type “12345678”. Orders that should be findable by the customer’s name, which lives in another table altogether.

For those cases, searchable also takes a lambda. You get the query and the search term, and you return a relation. It’s just ActiveRecord:

Field::String.new(:phone).searchable(lambda { |query:, term:|
  query.where("REPLACE(phone, ' ', '') LIKE ?", "%#{term.delete(" ")}%")
})

The results are combined with all the other searchable fields, so the rest of search keeps working like before.

Go find things

In summary: Search works out of the box, you can tweak it one field at a time, and when you need something special, it’s just a lambda away. The search documentation has all the details, and you can try it yourself in the demo.