2

我正在尝试使用 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'
4

1 回答 1

3

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

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
于 2012-08-20T17:34:36.073 回答