-->

Thinking Sphinx and negative IDs

2019-08-11 00:07发布

问题:

I have a Usermodel and a Message model where users can send messages to each other, and I use Thinking Sphinx gem for fulltext search as connector for Sphinx Search in Ruby on Rails.

In my User model I use negative IDs for system users, positive IDs for regular users.

Now I have the problem that Sphinx seems to ignore searches in messages by/to users with those negative IDs (=foreign key sender_id as has-Attribute).

Index as definded in my Message model:

define_index 'messages_index' do
  indexes title, :sortable => true
  indexes body

  has sender_id, recipient_id, created_at, updated_at
end

The search is done in the MessagesController:

@received_messages = current_user.received_messages.search params[:q]

If the current_user has a negative ID no result is returned. When I do a reindex it shows that all Sphinx collected all messages, none were left out.

Is there any way to teach Sphinx to accept negative numbers for has-Attributes? Or ideas for any work-around?

回答1:

Sphinx's integer type is unsigned, so your negative values aren't collected.

You could force the type to be a float, but don't forget to make any filters on those attributes floats as well:

has sender_id, recipient_id, :type => :float
has created_at, updated_at