0

I have tire working correctly for a single ActiveRecord model in the format

setting do
  ...SETTINGS...
  mapping do
    ...INDEXES...
  end
end

If possible I would like to share these settings between multiple models. I can't find any way of doing it from an initializer.

How can I do this?

Felix
  • 674
  • 5
  • 16

1 Answers1

4

Given the flexibility of Ruby, you have many options available here. The most obvious ones:

  1. Use a module constant/method, or multiple ones, to store the desired settings/mappings as Hashed, and then just pass them to Tire methods.

  2. Define the shared settings/mappings/behaviour in a module, which you can then include in your models. The approach is well described in the tire/issues/481.

A relevant snippet:

module Searchable

  def self.included(base)

    p "Included in #{base}"

    base.class_eval do
      include Tire::Model::Search

      tire do
        mapping do
          indexes :title,   type: 'string', analyzer: 'snowball'
        end
      end
    end
  end
end

class Article < ActiveRecord::Base
  include Searchable
end

class Person < ActiveRecord::Base
  include Searchable
end

This really doesn't have to be in an initializer -- you can put it in any place loadable from the Rails application.

karmi
  • 14,059
  • 3
  • 33
  • 41
  • Thanks a lot! Are there any disadvantages (performance or otherwise) for defining all analyzers together even if not every analyzer is user in every model? – Felix Dec 05 '12 at 13:13
  • 1
    No, no real overhead -- it's just configuration stuff for elasticsearch anyway. – karmi Dec 12 '12 at 20:29