2014-06-24 5 views
0

검색을 위해 sunspot/solr을 사용하고 있습니다. 최근에 열이 가끔 값이 없기 때문에 일부 레코드가 인덱싱되지 않는 것으로 나타났습니다.일부 열이 nil 인 경우에도 레코드를 어떻게 인덱싱합니까?

solr이 관계없이이 레코드를 인덱스 할 수 있습니까?

이 내 검색 블록이 모습입니다 : 레일

searchable do 
    text :name 
    text :description 
    # this price_as_double can sometimes be nil 
    double :price_as_decimal 
    text :colors do 
     colors.map(&:name) 
    end 
    text :store do 
     unless store.nil? 
     store.name 
     end 
    end 
    string :store do 
     unless store.nil? 
     store.name 
     end 
    end 
    text :items_style do 
     unless items_style.nil? 
     items_style.name 
     end 
    end 
    time :created_at 
    boolean :editors_pick 
    string :sold_out 
    double :likes 
    end 

답변

1

당신이 이전에 다음 함수를 추가 할 필요가

def change 
    add_column :product, :price, :string 
    add_index :product, :price 
end 

마이그레이션이 작동하고 위해 (곱셈 null 값을 허용합니다 대부분의 데이터베이스 엔진).

하지만 제품 클래스에 대한 유효성 검사는 다음과 같아야합니다.

유효성을 검사 : 가격, allow_nil : 속성 중 일부는 nil을 때 진정한

0

귀하의 unless 문 색인되는 개체를 방해하고 있습니다. 값이 (또는 빈) nil을 경우

searchable do 
    text :name 
    text :description 
    # this price_as_double can sometimes be nil 
    double :price_as_decimal 
    text :colors do 
    colors.map(&:name) 
    end 
    text :store do 
    store.present? ? store.name : '' 
    end 
    string :store do 
    store.present? ? store.name : '' 
    end 
    text :items_style do 
    items_style.present? ? items_style.name : '' 
    end 
    time :created_at 
    boolean :editors_pick 
    string :sold_out 
    double :likes 
end 

, 빈 문자열 대신 색인됩니다 : 더 좋은 방법은 이런 일을 할 것입니다.