【问题标题】:Sharing settings between elasticsearch indexes for ActiveRecord models (Tire gem)在 ActiveRecord 模型的弹性搜索索引之间共享设置(轮胎 gem)
【发布时间】:2012-12-01 22:12:30
【问题描述】:

我有 tire 在格式中为单个 ActiveRecord 模型正常工作

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

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

我该怎么做?

【问题讨论】:

  • 我的回复对您有帮助吗?

标签: ruby-on-rails elasticsearch tire


【解决方案1】:

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

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

  2. 在模块中定义共享设置/映射/行为,然后您可以将其包含在您的模型中。该方法在tire/issues/481 中有详细描述。

一个相关的sn-p:

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 应用程序加载的位置。

【讨论】:

  • 非常感谢!即使不是每个分析器都是每个模型的用户,一起定义所有分析器是否有任何缺点(性能或其他方面)?
  • 不,没有真正的开销——反正它只是弹性搜索的配置。
猜你喜欢
  • 2012-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多