思考sphinx搜索has_many无法工作没有错误



模型

class Person < ActiveRecord::Base
  attr_accessible :alignment, :description, :first_name, :last_name
  has_many :roles    #table roles with active as one of the field with value equal t or f (boolean)
end    
class Role < ActiveRecord::Base
  attr_accessible :active, :organization_id, :person_id, :position_id
  belongs_to :person 
  belongs_to :organization 
  belongs_to :position
end   

person_index.rb

ThinkingSphinx::Index.define :person, :with => :active_record do
 #Fields
 indexes last_name, :sortable => true
 indexes first_name, :sortable => true
 indexes alignment
 #indexes role(:active), :as => :active
 indexes role.active, :as => :active

 #Attributes
  has created_at, updated_at
  has professions(:id), :as => :profession_ids
  has positions(:id), :as => :position_id
  has organizations(:id), :as => :organization_id
end

people_controller

 filters = {}
 filters[:profession_ids] = params[:profession_ids] if params[:profession_ids].present?
 filters[:organization_id] = params[:organization_id] if params[:organization_id].present?
 filters[:position_id] = params[:position_id] if params[:position_id].present?   
 filters[:active_ids] = role if params[:active].present?  #added   
 @people = Person.search " #{params[:lastname]} #{params[:firstname]} #{params[:alignmemt]}", 
 :with     => filters,
 :star => true, 
 :condition =>  {:alignment => params[:alignment], :active =>  params[:active]}, 
 :order => 'last_name ASC, first_name ASC',
 :page     => params[:page], 
 :per_page => 20    

当我搜索active和/或alignment时,它不是过滤结果,也不会给我错误。这些都是字符串字段, alignment在人物表中, active在另一个表(角色)

为什么?我想念什么?

更新
在此问题上尝试了active的推荐解决方案,并且结果相同的结果...

person_index.rb

ThinkingSphinx::Index.define :person, :with => :active_record do
 #Fields
 indexes last_name, :sortable => true
 indexes first_name, :sortable => true
 indexes alignment
 #Attributes
  has created_at, updated_at
  has professions(:id), :as => :profession_ids
  has positions(:id), :as => :position_id
  has organizations(:id), :as => :organization_id
  has roles.active, :as => :active_ids
end

people_controller

def index
 @role = Role.find_by_active(params[:active])   #ADDED
 filters = {}
 filters[:profession_ids] = params[:profession_ids] if params[:profession_ids].present?
 filters[:organization_id] = params[:organization_id] if params[:organization_id].present?
 filters[:position_id] = params[:position_id] if params[:position_id].present?
 @people = Person.search " #{params[:lastname]} #{params[:firstname]} #{params[:alignmemt]}", 
 :with     => filters,
 :star => true,
 :condition =>  {:alignment => params[:alignment], :active_ids =>  @role}, #CHANGED
 :order => 'last_name ASC, first_name ASC',
 :page     => params[:page], 
 :per_page => 20     

但仍然有相同的结果...为什么?

pat答案后更新了控制器

def index   
  if params[:active].present?
    role = Array.new
    rolepid = Array.new
    role = Role.find_all_by_active(params[:active])
    role.each do |num|
     puts num.person_id
     rolepid << num.person_id  #get all the person id whith the params[:active]
    end
  end
  filters = {}
  filters[:profession_ids] = params[:profession_ids] if params[:profession_ids].present?
  filters[:organization_id] = params[:organization_id] if params[:organization_id].present?
  filters[:position_id] = params[:position_id] if params[:position_id].present?
  filters[:active_ids] = rolepid if params[:active].present?
  @people = Person.search " #{params[:lastname]} #{params[:firstname]} #{params[:alignent]}", 
  #:classes => [Person, Role],
  :with     => filters,
  :star => true, 
  :condition =>  {:alignment => params[:alignment]},
  :order => 'last_name ASC, first_name ASC',
  :page     => params[:page], 
  :per_page => 20

但是现在,当它应该在角色表中查找角色时,它正在寻找活跃的表。所以我添加了 #:classes => [Person, Role],,但没有运气....

 Role Load (0.7ms)  SELECT "roles".* FROM "roles" WHERE "roles"."active" = 'f'
 Sphinx Query (0.7ms)  SELECT * FROM `person_core` WHERE `active_ids` IN (304, 34, 306, 308, 334, 295, 344, 348, 352, 354, 365, 367, 308, 429, 468, 9, 544, 590, 609, 110, 1643, 1652, 1653, 1655, 1669, 628, 1687, 1691, 1709) AND `sphinx_deleted` = 0 ORDER BY `last_name` ASC, first_name ASC LIMIT 0, 20
 Sphinx  Found 0 results

所以我更改在控制器中
filters[:active_ids] = rolepid if params[:active].present?

filters[:id] = rolepid if params[:active].present?

由于 rolepid是一个具有人ID的整数。
但是Sphinx只是在寻找不在rolepid中的4个ID ...我很困惑:|

 Parameters: {"utf8"=>"✓", "firstname"=>"", "lastname"=>"", "alignment"=>"", "organization_id"=>"", "position_id"=>"", "active"=>"f", "commit"=>"Search"}
 Role Load (0.8ms)  SELECT "roles".* FROM "roles" WHERE "roles"."active" = 'f'
 Sphinx Query (0.6ms)  SELECT * FROM `person_core` WHERE `id` IN (304, 34, 306, 308, 334, 295, 344, 348, 352, 354, 365, 367, 308, 429, 468, 9, 544, 590, 609, 110, 1643, 1652, 1653, 1655, 1669, 628, 1687, 1691, 1709) AND `sphinx_deleted` = 0 ORDER BY `last_name` ASC, first_name ASC LIMIT 0, 20
 Sphinx  Found 4 results
 Person Load (0.4ms)  SELECT "people".* FROM "people" WHERE "people"."id" IN (84, 1, 61, 50)   

为什么不从rolepid数组返回29个记录?

alignment的过滤正在工作。感谢您抓住拼写错误的单词。

如果您使用的是 active_ids作为属性(如果是整数,当然是合适的),则它应该是:with选项中的过滤器,而不是:conditions选项。<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

我不确定这是否相关,但是值得注意的是,您在查询字符串中拼写错误的对齐方式(改为有AlignMemt)。

相关内容

最新更新