Search code examples
ruby-on-rails-3sphinxthinking-sphinx

Thinking Sphinx search for different conditions from the same join table?


This question is similar to

How do you search for different conditions from the same join table?, but I didn't get solution from this.z

My code

has_one    :insurance_type_link, as: :enumerable, class_name: "Utility::UserDefinedEnumLink",
         conditions: {user_defined_enum_links: {user_defined_enum_id: Utility::UserDefinedEnum.insurance_types.map(&:id)}}
has_one    :payment_duration_link, as: :enumerable, class_name: "Utility::UserDefinedEnumLink",
         conditions: {user_defined_enum_links: {user_defined_enum_id: Utility::UserDefinedEnum.payment_durations.map(&:id)}}

My define_index has this

indexes insurance_type_link.user_defined_enum.enum_value,  as: :it, sortable: :insensitive
indexes payment_duration_link.user_defined_enum.enum_value, as: :pd, sortable: :insensitive

And sql query generated inside inside development.sphinx.conf is

AS `it_sort`, LOWER(IFNULL(`user_defined_enums_user_defined_enum_links`.`enum_value`, '')) 
AS `pd_sort`, IFNULL(`user_defined_enums`.`enum_value`, '')

So it is generating different query to both the relation as a result only first query is giving me proper search results.

How to fix this ?

Thanks in advance.


Solution

  • I fixed it using a short cut method, not sure whether it is okay or not... I defined a third relation that include both the relations that I defined earlier only for the sake of searching and used that in define_index method .