hel*_*llo 4 ruby-on-rails associations elasticsearch tire ruby-on-rails-3.2
我正在尝试在建立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
Run Code Online (Sandbox Code Playgroud)
那么这就是服务模型
class Service < ActiveRecord::Base
attr_accessible :service, :user_id, :description
belongs_to :professional
belongs_to :servicable, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)
我还使用这个重新索引:
rake environment tire:import CLASS=Business FORCE=true
Run Code Online (Sandbox Code Playgroud)
我可以在Business中搜索项目,但是当我尝试在Service中搜索某些内容时,我得到一个空的结果.
在与映射挣扎之后,我创建了一个宝石,使搜索更容易一些.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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2182 次 |
| 最近记录: |