繁体   English   中英

在ActiveRecord模型的Elasticsearch索引之间共享设置(Tire gem)

[英]Sharing settings between elasticsearch indexes for ActiveRecord models (Tire gem)

我的轮胎对于格式为单个ActiveRecord模型的轮胎正常工作

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

如果可能,我想在多个模型之间共享这些设置。 我无法从初始化程序中找到任何方法。

我怎样才能做到这一点?

鉴于Ruby的灵活性,您可以在此处使用许多选项。 最明显的是:

  1. 使用一个模块常数/方法或多个,将所需的设置/映射存储为Hashed,然后将其传递给Tire方法。

  2. 在模块中定义共享的设置/映射/行为,然后可以将其包括在模型中。 该方法在轮胎/问题/ 481中有很好的描述。

相关代码段:

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

确实不需要在初始化程序中-您可以将其放在可从Rails应用程序加载的任何位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM