如何处理sunspot-rails中缺少的字段?

时间:2012-06-06 22:37:33

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

我正在使用sunspot / rails版本2.它工作得很好,但我无法弄清楚如何处理丢失的字段。如果我没有纬度和经度,则此代码会将其映射到0,0(非洲附近):

searchable do
  text      :resume, :stored => true
  text      :city, :boost => 5
  latlon(:geo) { Sunspot::Util::Coordinates.new(latitude, longitude) }
end

我尝试使用两个搜索块,每个搜索块都有不同的条件,但是太阳黑子只使用第一个可搜索的块。我想要发生的事情是缺少位置的东西仍然可以搜索,而不是按位置。

2 个答案:

答案 0 :(得分:3)

是的,通常我会在可选条件之后进行if检查:

searchable do
  text      :resume, :stored => true
  text      :city, :boost => 5
  latlon(:geo) { Sunspot::Util::Coordinates.new(latitude, longitude) } if latitude && longitude
  # other conditions if needed
  with(:another_field, condition_var) if condition_var
end

答案 1 :(得分:1)

我想到了一种解决方法,添加一个名为has_location的布尔字段,然后在进行地理搜索时始终检查它。克鲁迪,但它应该工作。