Searching and sorting by a float field with thinking sphinx

馋奶兔 提交于 2019-12-11 12:27:24

问题


I'm using thinking sphinx to for search on a rails app. I have a float field called 'height'. I need to be able to search this field for exact values (i.e. exactly 6.0, not 6.5). I also need to be able to sort on the field.

What I have so far:

indexes height, :sortable => true

Problem: doesn't sort properly, returns 6.0 and 6.5 if I search for '6'


回答1:


If you're dealing with float values, it's best to have them as an attribute instead of a field:

define_index do
  # ... other fields

  has height
end

Attributes are sortable by default (indeed, if you add :sortable to a field, all it is doing is creating an attribute under the hood of Thinking Sphinx), so this should allow you to sort.

Of course, this doesn't allow you to search for the height, though, so you'll need a field as well:

define_index do
  # ... other fields
  indexes height, :as => :height_field

  has height
end

I've given the field an alias, because you can't have fields and attributes named the same thing.

Given all that, you're searching on a float, and to Sphinx, all fields are strings. It reads 6.5 as two words - 6 and 5, separated by a full stop/period. So I wouldn't expect that side of things to work out elegantly, unfortunately.



来源:https://stackoverflow.com/questions/879267/searching-and-sorting-by-a-float-field-with-thinking-sphinx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!