2012-07-05 39 views
0

我擁有屬於類別的product_details表。 product_details包含ID,名稱,價格,折扣和category_id等字段,而類別表具有ID和名稱等字段。我正在使用mysql數據庫正試圖根據category_id來完成組產品的詳細信息。要做分組,我參考https://gist.github.com/f987013b2feec5b28456。但我收到錯誤以下錯誤sunspot_solr分組子句中的未定義字段錯誤

RSolr::Error::Http - 400 Bad Request 
Error:  undefined field category_id 

我的模型看起來像這樣

class ProductDetail < ActiveRecord::Base 
     belongs_to :category 

     searchable do 
     text :name 
     integer :category_id 
     end 

    end 

我的控制器看起來像這樣

def index 
     @search_res1=ProductDetail.search do 

     adjust_solr_params do |params| 
      params[:group] = true 
      params[:"group.field"] = "category_id" 
      params[:"group.format"] = "simple" 
     end 

     end.execute 
     @[email protected]_res1.results 
end 

在我的日誌文件我越來越喜歡這個

RSolr :: Error :: Http in ProductDet ailsController#index

RSolr::Error::Http - 400 Bad Request 
Error:  undefined field category_id 

Request Data: "fq=type%3AProductDetail&fq=category_id_i%3A%281%29&start=0&rows=30&group=true&group.field=category_id&group.format=simple&q=%2A%3A%2A" 

Backtrace: /home/toshiba/.rvm/gems/ruby-1.9.2-p290/gems/rsolr-1.0.8/lib/rsolr/client.rb:230:in `adapt_response' 
/home/toshiba/.rvm/gems/ruby-1.9.2-p290/gems/rsolr-1.0.8/lib/rsolr/client.rb:167:in `execute' 
/home/toshiba/.rvm/gems/ruby-1.9.2-p290/gems/rsolr-1.0.8/lib/rsolr/client.rb:161:in `send_and_receive'

請幫幫我。謝謝。

回答

1

這裏包括兩個:

1.Grouping太陽黑子僅支持string領域。所以,你的searchable塊更改爲以下:

class ProductDetail < ActiveRecord::Base 
    belongs_to :category 

    searchable do 
    text :name 
    string :category_id_str do 
     category_id.to_s 
    end 
    end 
end 

2.Change組PARAMS反映屬性名稱變更:

def index 
    @search_res1=ProductDetail.search do 
    adjust_solr_params do |params| 
     params[:group] = true 
     params[:"group.field"] = "category_id_str_s" 
     params[:"group.format"] = "simple" 
    end 
    end.execute 
    @[email protected]_res1.results 
end 

我認爲這裏的太陽黑子是添加上額外_s到屬性當它索引它。

0

也許在您的/ usr/share/solr/conf的schema.xml中缺少一個類型整數?

這裏是我爲例:

<schema name="sunspot" version="1.0"> 
    <types> 
    <!-- field type definitions. The "name" attribute is 
     just a label to be used by field definitions. The "class" 
     attribute and any other attributes determine the real 
     behavior of the fieldType. 
     Class names starting with "solr" refer to java classes in the 
     org.apache.solr.analysis package. 
    --> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="string" class="solr.StrField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="tdouble" class="solr.TrieDoubleField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="rand" class="solr.RandomSortField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="text" class="solr.TextField" omitNorms="false"> 
     <analyzer> 
     <tokenizer class="solr.StandardTokenizerFactory"/> 
     <filter class="solr.StandardFilterFactory"/> 
     <filter class="solr.LowerCaseFilterFactory"/> 
     </analyzer> 
    </fieldType> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="boolean" class="solr.BoolField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="date" class="solr.DateField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="sdouble" class="solr.SortableDoubleField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="sfloat" class="solr.SortableFloatField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="sint" class="solr.SortableIntField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="slong" class="solr.SortableLongField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="tint" class="solr.TrieIntField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="tfloat" class="solr.TrieFloatField" omitNorms="true"/> 
    <!-- *** This fieldType is used by Sunspot! *** --> 
    <fieldType name="tdate" class="solr.TrieDateField" omitNorms="true"/> 
    </types> 
相關問題