与太阳黑子的分面搜索

时间:2012-08-20 16:53:19

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

我正在尝试用Sunspot实现分面搜索,但我一定是做错了。

class Product < ActiveRecord::Base
  attr_accessible :category, :color, :condition, :item_no, :size
  searchable do
    text :color, :size
  end
end

这是我的产品型号,我想在导航一侧有一个菜单,可以使用类别,颜色,条件等来过滤结果。

在我的控制器中我有:

 def index

    @search = Product.search do
      fulltext params[:search]
      facet :color
    end
    @products = @search.results

    respond_to do |format|
      format.html
      format.json { render json: @products }
    end
  end

但我不断收到错误消息:

Sunspot::UnrecognizedFieldError 
No field configured for Product with name 'color'

在视图中我有:

<div id="facets">
  <h3>Category</h3>
  <ul>
    <% for row in @search.facet(:color).rows %>
      <li>
        <% if params[:color].blank? %>
          <%= link_to row.value, :color => row.value %> (<%= row.count %>)
        <% else %>
          <strong><%= row.value %></strong> (<%= link_to "remove", :color => nil %>)
        <% end %>
      </li>
    <% end %>
  </ul>
</div>

任何人都可以指出我做错了什么吗?

非常感谢您的帮助!

更新: 这是错误的痕迹。

sunspot (1.3.3) lib/sunspot/query/restriction.rb:245:in `to_solr_conditional'
sunspot (1.3.3) lib/sunspot/query/restriction.rb:92:in `to_positive_boolean_phrase'
sunspot (1.3.3) lib/sunspot/query/restriction.rb:72:in `to_boolean_phrase'
sunspot (1.3.3) lib/sunspot/query/filter.rb:11:in `to_filter_query'
sunspot (1.3.3) lib/sunspot/query/scope.rb:5:in `block in to_params'
sunspot (1.3.3) lib/sunspot/query/scope.rb:5:in `map'
sunspot (1.3.3) lib/sunspot/query/scope.rb:5:in `to_params'
sunspot (1.3.3) lib/sunspot/query/common_query.rb:51:in `block in to_params'
sunspot (1.3.3) lib/sunspot/query/common_query.rb:50:in `each'
sunspot (1.3.3) lib/sunspot/query/common_query.rb:50:in `to_params'
sunspot (1.3.3) lib/sunspot/search/abstract_search.rb:37:in `execute'
sunspot_rails (1.3.3) lib/sunspot/rails/searchable.rb:329:in `solr_execute_search'
sunspot_rails (1.3.3) lib/sunspot/rails/searchable.rb:153:in `solr_search'

1 个答案:

答案 0 :(得分:3)

这里缺少两件事。首先,您需要将颜色包含为字段,其次,facet方法告诉sunspot(即solr)返回结果集中存在的所有facet,但不通过该facet对查询进行范围调整。所以,你需要这样的东西:

searchable do
  text :color, :size
  string :color
end


@search = Product.search do
  fulltext params[:search]
  with(:color).all_of(params[:color]) unless params[:color].blank?
  facet :color
end