2

アソシエーションで検索し、黒点で検索するにはどうすればよいですか?

class StaticController < ApplicationController

  def search
    @search = Sunspot.search Business, Service do
      fulltext params[:q]
      paginate :per_page => 10
      order_by_geodist(:location, *Geocoder.coordinates(params[:loc]))
    end
      @biz = @search.results

end

class Business < ActiveRecord::Base
  attr_accessible :name
  has_many :services, :through => :professionals

  searchable  do
    text :name #name in business column
    # how to do I get the services?
  end

end

class Service < ActiveRecord::Base
  attr_accessible :service
  belongs_to :professional
end

class Professional < ActiveRecord::Base
  belongs_to :business
  has_many :services, as: :servicable
end

ビューには、これがあります(ループがたくさんあります)

<%= @biz.each do |b| %>
  <%= b.name %>

  <!-- looping through professionals model -->
  <% b.professionals.each do |prof| %>

    <!-- looping through services model -->
    <% prof.services.each do |s| %>
      <%= s.service %>
    <% end %>

  <% end %>
<% end %>

これは、ビジネス モデル内の名前を検索する場合には機能しますが、モデル内の用語を検索する場合はどうなるServiceでしょうか? 私の見解はビジネス側からのみ来ているため、正しく表示されません。Serviceモデルを検索するとビジネス名がポップアップ表示されるようにするにはどうすればよいですか?

ありがとう

4

1 に答える 1

7

これを実現するには、呼び出し元のモデルで関連するモデルの追加のインデックスを作成する必要があります。例えば:

class Business < ActiveRecord::Base
 attr_accessible :name
 has_many :services, :through => :professionals

 searchable  do
  text :name #name in business column
  text :services do  # this one for full text search
     services.map(&:service).compact.join(" ")
  end
  string :services , :multiple => true do #this one for exact searches
     services.map(&:service).compact
  end
 end
end 

その後、次のようなクエリを実行できます。

Bussines.search do 
  with(:services, "some_service")
end.execute.results

これで、データを取得するために mysql テーブルで結合する必要がなくなりました。solrからデータを取得するだけです。これは、solr の最大の利点の 1 つです。

これで明確になることを願っています。詳細が必要な場合は、お気軽にコメントをお寄せください。

于 2013-10-28T07:07:03.343 に答える