思考狮身人面像搜索所有模型的搜索

时间:2014-01-24 21:43:02

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

我已经实施了针对虚拟模型的搜索,并且它有效 现在我的主页有一个表格,其中包含我的应用程序的几个模型的字段,但一次搜索一个字段不会在主页上产生任何结果,虽然我可以看到查询....(搜索tv_show名称)

 Parameters: {"utf8"=>"✓", "name"=>"count", "broadcast_date_from"=>"", "broadcast_date_to"=>"", "gender"=>"", "organization_id"=>"", "position_id"=>"", "alignment"=>"", "commit"=>"Generate"}
 Sphinx Query (1.0ms)  SELECT * FROM `episode_core`, `organization_core`, `person_core`, `position_core`, `profession_core`, `segment_core`, `tv_show_core` WHERE MATCH('*count*') AND `sphinx_deleted` = 0 ORDER BY `last_name` ASC, first_name ASC LIMIT 0, 20
 Sphinx  Found 0 results
 Rendered home/index.html.erb within layouts/home (3.0ms)
 Completed 200 OK in 13ms (Views: 12.9ms | ActiveRecord: 0.0ms)   

搜索模型tv_show时,我有结果。

 Parameters: {"utf8"=>"✓", "name"=>"count", "channel"=>"", "starting_time"=>"", "ending_time"=>"", "commit"=>"Search"}
 Sphinx Query (0.9ms)  SELECT * FROM `tv_show_core` WHERE MATCH('*count*') AND `sphinx_deleted` = 0 ORDER BY `tv_name` ASC LIMIT 0, 20
 Sphinx  Found 2 results
 TvShow Load (0.5ms)  SELECT "tv_shows".* FROM "tv_shows" WHERE "tv_shows"."id" IN (9, 4)
 Rendered tv_shows/index.html.erb within layouts/application (7.5ms)
 Rendered tv_shows/_search.html.erb (0.9ms)
 Completed 200 OK in 68ms (Views: 66.7ms | ActiveRecord: 0.5ms)   

那为什么会有区别?

全局搜索无结果

@results = ThinkingSphinx.search.search "#{params[:name]} ", 
:star => /[\w@:]+/u, 
:order => 'last_name ASC, first_name ASC',
:page     => params[:page], 
:per_page => 20

模型搜索返回结果

@tv_shows = TvShow.search "#{params[:name]} #{params[:channel]} ", 
     :star => /[\w@:]+/u,  
     :order => 'tv_name ASC', 
     :page => params[:page], 
     :per_page => 20    

更新
 好的,删除:order => 'last_name ASC, first_name ASC',让我到某处。

有另一个问题 如何根据表单中使用的字段查询特定模型?

 Parameters: {"utf8"=>"✓", "name"=>"count", "broadcast_date_from"=>"", "broadcast_date_to"=>"", "gender"=>"", "organization_id"=>"", "position_id"=>"", "alignment"=>"", "commit"=>"Generate"}
 Profession Load (0.9ms)  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
 Sphinx Query (1.7ms)  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('*count*') AND `sphinx_deleted` = 0 LIMIT 0, 20
 Sphinx  Found 4 results
 Organization Load (0.5ms)  SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" IN (109)
 Position Load (0.4ms)  SELECT "positions".* FROM "positions" WHERE "positions"."id" IN (164)
 TvShow Load (0.4ms)  SELECT "tv_shows".* FROM "tv_shows" WHERE "tv_shows"."id" IN (4, 9)     

这里我只是使用了引用TvShow模型的tv_show字段 添加条件无助于.... :condition => {:tv_name => params[:name]}

任何有识之士。

回复(以防万一)

需要在全局搜索视图中添加:

<% for result in @results %>  
  <% if result.class.name == "Organization" %>
      <%= render :partial => "organizations/result", :locals => { :result => result } %>
  <% elsif result.class.name == "Position" %>
      <%= render :partial => "positions/result", :locals => { :result => result } %>
  <% elsif result.class.name == "TvShow" %>
      <%= render :partial => "tv_shows/result", :locals => { :result => result } %>                 
  <% end %>
<% end %>    

为视图文件夹中的每个模型创建部分(_result.html.erb)以打印不同的字段(部分是可选的)。

0 个答案:

没有答案