Searching and sorting by float field with sphinx thinking

I am using sphinx thinking to search in a rails app. I have a float field called "height". I need to find this field for the exact values ​​(i.e. exactly 6.0, not 6.5). I also need to be able to sort by field.

What I have so far:

indexes height, :sortable => true

      

Problem: Doesn't sort correctly, returns 6.0 and 6.5 if I search for "6"

+1


a source to share


1 answer


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

define_index do
  # ... other fields

  has height
end

      

Attributes are sorted by default (indeed, if you add: sortable to the field, all it does is create an attribute under the hood of Thinking Sphinx), so that should allow you to sort.

Of course this doesn't allow you to search for the height, however, you also need a field:



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

  has height
end

      

I gave the field an alias because you cannot have fields and attributes with the same name.

With all of this in mind, you're looking for a float, and for Sphinx, all fields are strings. It reads 6.5 as two words - 6 and 5, separated by a full stop / period. So I would not have expected this side of things to look elegant, unfortunately.

+3


a source







All Articles