【问题标题】:Multi model search in sunspot rails太阳黑子轨道中的多模型搜索
【发布时间】:2015-10-20 10:22:57
【问题描述】:

我的模特是

class Product < ActiveRecord::Base

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

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

我的代码是

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

最后我想得到产品。

【问题讨论】:

    标签: ruby-on-rails ruby sunspot-rails sunspot-solr


    【解决方案1】:

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

    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
    

    链接:https://github.com/sunspot/sunspot

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-30
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多