【问题标题】:Rails: Elasticsearch :through association mappingRails:Elasticsearch:通过关联映射
【发布时间】:2013-11-01 05:13:54
【问题描述】:

当我有 has_many, :through 关联时,我正在尝试为模型建立索引,但没有显示任何结果。

class Business < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  def self.search(params)
    tire.search(load: true) do
      query { string params[:q]} if params[:q].present?
    end
  end

  mapping do
    indexes :service_name
    indexes :service_description
    indexes :latitude
    indexes :longitude
    indexes :services do
      indexes :service
      indexes :description
    end
  end

  def to_indexed_json #returns json data that should index (the model that should be searched)
    to_json(methods: [:service_name, :service_description], include: { services: [:service, :description]})
  end

  def service_name
    services.map(&:service)
  end

  def service_description
    services.map(&:description)
  end

  has_many :professionals
  has_many :services, :through => :professionals

end

那么这就是服务模型

class Service < ActiveRecord::Base
  attr_accessible :service, :user_id, :description
  belongs_to :professional
  belongs_to :servicable, polymorphic: true
end

我也使用这个重新索引:

rake environment tire:import CLASS=Business FORCE=true

我可以在 Business 中搜索项目,但是当我尝试在 Service 中搜索某些内容时,我得到一个空结果。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.2 associations elasticsearch tire


    【解决方案1】:

    在与映射作斗争后,我创建了一个 gem 来简化搜索。 https://github.com/ankane/searchkick

    您可以使用search_data 方法来完成此操作:

    class Business < ActiveRecord::Base
      searchkick
    
      def search_data
        {
          service_name: services.map(&:name),
          service_description: services.map(&:description)
        }
      end
    end
    

    【讨论】:

    • 我还需要轮胎吗?因为我正在尝试使用 searchkick,但是我在使用 Tire 时遇到了这个错误,我试图断开它,现在我无法在没有 Tire 的情况下进行搜索。
    • Searchkick内部使用了Tire,但是你想在使用的时候去掉所有Tire相关的代码,比如to_indexed_json、mapping等
    • 我收到一个 500 错误Tire::Search::SearchRequestFailed,我删除了所有内容,但只是做了@biz = Business.search params[:q],我还保留了include Tire 的东西
    • 您需要删除包含轮胎,运行 Business.reindex,并确保索引正确的数据,检查一些业务记录。 Business.first.search_data
    • 我删除了包含,并重新索引。我仍然收到 500 错误 Tire::Search::SearchRequestFailed 不完全确定发生了什么。我注释掉了所有与轮胎相关的代码
    【解决方案2】:

    我不相信有办法映射与轮胎的关联。相反,您要做的是使用 :as 方法和 proc 定义易于搜索的字段。这样你也可以摆脱 to_indexed_json 方法(你实际上需要)

    mapping do
      indexes :service_name
      indexes :service_description
      indexes :latitude
      indexes :longitude    
      indexes :service_name, type: 'string', :as => proc{service_name}
      indexes :service_description, type: 'string', :as => proc{service_description}
    end
    

    【讨论】:

    • 这个方法对我还是不起作用。我仍然得到零输出
    • 你重新索引你的模型了吗?如果是这样,请尝试使用 Sense chrome 扩展来浏览您创建的索引,看看您是否在新列中有任何数据。
    • 我确实重新索引了模型,并且我检查了感觉,它只是给了我空数组。我不确定它是否完全从services获得模型
    • 我可以获得专业模特,但我无法获得服务?
    【解决方案3】:

    轮胎可以关联关联,我已经用它来索引 has_many 关联但还没有尝试过 has_many, :through 。尝试对对象进行索引?

    mapping do
      indexes :service_name
      indexes :service_description
      indexes :latitude
      indexes :longitude
      indexes :services, type: 'object',
        properties: {
          service: {type: 'string'}
          description: {type: 'string'}
        }
    end
    

    另外,最好有一个触摸方法:

    class Service < ActiveRecord::Base
      attr_accessible :service, :user_id, :description
      belongs_to :professional, touch: true
      belongs_to :servicable, polymorphic: true
    end
    

    和 after_touch 回调来更新索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2015-05-31
      相关资源
      最近更新 更多