太阳黑子中的多模型搜索 rails

Multi model search in sunspot rails

我的模特是

class Product < ActiveRecord::Base

has_many :category_products
has_many :categories , through: :category_products
has_many :product_tags
has_many :tags, through: :product_tags

我想搜索所有三个模型类别、标签,Products.It 工作正常,但我面临的问题是,当我搜索类别和标签时,我想找到相关的 products.But 我如何区分何时仅搜索产品列及其关联表。

我的密码是

@search = Sunspot.search[Product,Category,Tag] do
    fulltext params[:search]
    paginate(:page => params[:page] || 1, :per_page => 3)
  end
  @products = @search.results

最后我要拿产品

将“类别”和“标签”添加到您的产品搜索信息中,如下所示:

class Product < ActiveRecord::Base
  searchable do
    text :name
    text :categories do
      categories.map { |category| category.name }
    end
    text :tags do
      tags.map { |tag| tag.name }
    end
  end
end

@search = Sunspot.search(Product) do
  fulltext params[:search]
  paginate(:page => params[:page] || 1, :per_page => 3)
end
@products = @search.results

link: https://github.com/sunspot/sunspot

希望对您有所帮助!