Rails: Elasticsearch: through association mapping

I try to index a model when I have an association has_many, :through, but the results are not displayed.

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

Then this is a service model

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

I will also reindex this:

rake environment tire:import CLASS=Business FORCE=true

I can search for items in Business, but when I try to find something in Service, I get an empty result.

+4
source share
3 answers

, Tire. : 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
+3

, . http://ankane.imtqy.com/searchkick/

search_data :

class Business < ActiveRecord::Base
  searchkick

  def search_data
    {
      service_name: services.map(&:name),
      service_description: services.map(&:description)
    }
  end
end
+3

, has_many , has_many, . ?

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 .

0

All Articles