Class: Huginn::Generators::TrigramIndexesGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
SearchableColumns
Defined in:
lib/generators/huginn/trigram_indexes/trigram_indexes_generator.rb

Overview

Scaffolds a migration that creates the PostgreSQL expression GIN indexes (gin_trgm_ops on an IMMUTABLE unaccent wrapper of the column) backing Huginn::Searchable's :pg_trgm strategy for every model that includes Huginn::Searchable.

rails g huginn:trigram_indexes                 # all Searchable models
rails g huginn:trigram_indexes Person Product  # specific models

The migration creates an IMMUTABLE public.f_unaccent(text) wrapper and the GIN indexes. To make the search actually use them, point the gem at the wrapper (the pg builtin unaccent() is STABLE on PG 13+ and cannot be indexed directly):

Huginn.configure { |c| c.unaccent_function = "public.f_unaccent" }

Indexes are only used when the :pg_trgm strategy is configured and the pg_trgm/unaccent extensions are installed.

Instance Method Summary collapse

Instance Method Details

#copy_migrationObject



33
34
35
36
37
38
39
40
41
# File 'lib/generators/huginn/trigram_indexes/trigram_indexes_generator.rb', line 33

def copy_migration
  @entries = resolve_entries
  return if @entries.empty?

  migration_template(
    "add_huginn_trigram_indexes.rb.tt",
    "db/migrate/add_huginn_trigram_indexes.rb"
  )
end