思考Sphinx在所有模型中搜索搜索:每个模型的结果数量

时间:2014-01-29 21:18:09

标签: ruby-on-rails-3 grouping thinking-sphinx

我进行了全局搜索(跨所有模型)。现在,我想知道每个模型可以有多少结果。

例如,如果我的搜索返回10行/结果并且我有3个模型,是否可以知道模型A有3个,模型B有2个,模型C有5个结果?我想用它来创建图表。

更新

家庭控制器

 facets =ThinkingSphinx.facets "#{params[:name]}  #{params[:alignment]} #{params[:tvShow_id]}", 
   :star => true, 
   :with     => filters, 
   :conditions =>  { :ending_time =>  params[:ending_time], :starting_time =>  params[:starting_time]},
   :page     => params[:page],
   :per_page => 20 

 puts facets[:sphinx_internal_class]    

错误

 Parameters: {"utf8"=>"✓", "tvShow_id"=>"", "broadcast_date_from"=>"", "starting_time"=>"", "broadcast_date_to"=>"", "ending_time"=>"", "gender"=>"", "organization_id"=>"", "position_id"=>"", "alignment"=>"neutral", "commit"=>"Generate"}
 Profession Load (1.0ms)  SELECT "professions".* FROM "professions" ORDER BY name
 Organization Load (2.5ms)  SELECT "organizations".* FROM "organizations" ORDER BY name
 Position Load (2.0ms)  SELECT "positions".* FROM "positions" ORDER BY name
 TvShow Load (0.5ms)  SELECT "tv_shows".* FROM "tv_shows" ORDER BY name
 Sphinx Query (1.9ms)  SELECT * FROM `episode_core`, `episode_delta`, `organization_core`, `organization_delta`, `person_core`, `person_delta`, `position_core`, `position_delta`, `profession_core`, `profession_delta`, `segment_core`, `segment_delta`, `tv_show_core`, `tv_show_delta` WHERE MATCH('*neutral*') AND `sphinx_deleted` = 0 ORDER BY `model_order` ASC LIMIT 0, 20
 Sphinx  Found 5 results
 Position Load (0.4ms)  SELECT "positions".* FROM "positions" WHERE "positions"."id" IN (54, 142)
 Organization Load (0.4ms)  SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" IN (104)
 Person Load (0.5ms)  SELECT "people".* FROM "people" WHERE "people"."id" IN (348, 1658)
 Sphinx  Retrying query "SELECT *, *, @groupby, @count FROM `episode_core`, `episode_delta`, `organization_core`, `organization_delta`, `person_core`, `person_delta`, `position_core`, `position_delta`, `profession_core`, `profession_delta`, `segment_core`, `segment_delta`, `tv_show_core`, `tv_show_delta` WHERE MATCH('*neutral*') AND `sphinx_deleted` = 0 GROUP BY `sphinx_internal_class` LIMIT 0, 20 OPTION max_matches=20; SHOW META" after error: Using the old-fashion @variables (@count, @weight, etc.) is deprecated
 Sphinx  Retrying query "SELECT *, *, @groupby, @count FROM `episode_core`, `episode_delta`, `organization_core`, `organization_delta`, `person_core`, `person_delta`, `position_core`, `position_delta`, `profession_core`, `profession_delta`, `segment_core`, `segment_delta`, `tv_show_core`, `tv_show_delta` WHERE MATCH('*neutral*') AND `sphinx_deleted` = 0 GROUP BY `sphinx_internal_class` LIMIT 0, 20 OPTION max_matches=20; SHOW META" after error: Using the old-fashion @variables (@count, @weight, etc.) is deprecated
 Sphinx Query (6.4ms)  SELECT *, *, @groupby, @count FROM `episode_core`, `episode_delta`, `organization_core`, `organization_delta`, `person_core`, `person_delta`, `position_core`, `position_delta`, `profession_core`, `profession_delta`, `segment_core`, `segment_delta`, `tv_show_core`, `tv_show_delta` WHERE MATCH('*neutral*') AND `sphinx_deleted` = 0 GROUP BY `sphinx_internal_class` LIMIT 0, 20 OPTION max_matches=20
 Completed 500 Internal Server Error in 42ms

 ThinkingSphinx::SphinxError (Using the old-fashion @variables (@count, @weight, etc.) is deprecated):
 app/controllers/home_controller.rb:92:in `index'

1 个答案:

答案 0 :(得分:2)

嗯,您可以随时从搜索结果中计算出来:

search = ThinkingSphinx.search params[:query]
groups = search.group_by(&:class)
groups.each { |klass, results| puts klass.name, results.length }

但是如果您拥有的结果数量大于您的页面大小,那么这不会起作用。更好的方法是使用facet调用:

facets = ThinkingSphinx.facets params[:query] # takes same arguments as search
facets[:sphinx_internal_class]