Thinking sphinx without condition on array of id is not working

444 views Asked by At

I am using ruby 1.9.3p392 rails 3.2.21 thinking sphinx 3.1.0 and Sphinx 2.2.4-id64-release

user_index.rb file is :-

ThinkingSphinx::Index.define :user, :with => :active_record do
  indexes first_name, :sortable => true
  indexes last_name
  indexes email
  indexes user_name
  indexes company_id
  indexes id
  indexes user_type_id
  indexes department.name
  indexes department.id, :as => :department_id
end

When i search as:-

assigned_user_ids = [372, 373, 374, 375, 367, 376, 377, 378, 379, 380]
@users = User.search(Riddle::Query.escape(params[:search]), 
    :conditions => {:company_id => @company.id}, 
    :without => {:id => assigned_user_ids}, :per_page => PAGINATION_SIZE, 
    :page => params[:page])

But it is still showing the user with id = 372

1

There are 1 answers

0
pat On BEST ANSWER

There are two issues here:

The first is that you're using fields instead of attributes for any non-string data, and that means some filters aren't going to work reliably. The second issue is that id is used by Sphinx internally, so you should either use Thinking Sphinx's automatic attribute sphinx_internal_id, or add an alias to your own attribute.

So, I would recommend the following index definition instead:

ThinkingSphinx::Index.define :user, :with => :active_record do
  indexes first_name, :sortable => true
  indexes last_name
  indexes email
  indexes user_name
  indexes department.name

  has company_id
  has user_type_id
  has department.id, :as => :department_id
end

And then your search would be:

assigned_user_ids = [372, 373, 374, 375, 367, 376, 377, 378, 379, 380]
@users = User.search(Riddle::Query.escape(params[:search]), 
  :with     => {:company_id => @company.id}, 
  :without  => {:sphinx_internal_id => assigned_user_ids},
  :per_page => PAGINATION_SIZE, 
  :page     => params[:page]
)

In a completely unrelated note: unless this search is only being used by administrators, I would recommend you not have email addresses in your indexed data. Allowing people to search by email address is a security risk in most situations.