sunspot return all results for a model

巧了我就是萌 提交于 2020-01-04 05:19:12

问题


I have in my controller this:

def boards
   @user = User.find_by_slug(params[:id])
     @search = @user.boards.solr_search do |s|
       s.fulltext params[:search]
       s.keywords params[:search]
       s.order_by :created_at, :desc
       s.paginate :page => params[:page], :per_page => 1
     end

   @boards = @search.results
respond_to do |format|
  format.html { render :layout => nil}# panel.html.erb
  format.json { render json: @boards }
  format.js
end
end

In my view:

<table id="body_object">
 <% for board in @boards %>
  <tr class="attributes">
   <td>
     <%= board.id %>
   </td>
   <td>
     <%= board.name %>
   </td>
   <td>
     <%= board.description  %>
   </td>
  </tr>
 <% end %>
</table>

   <div id="content_pagination">
    <%= paginate @boards, :remote => :true  %>
  </div>

@user.boards in controller are every boards that belongs to user.

But I get every boards like Boards.all.

I want get only every boards that belongs to user.

I have tried with @user.boards but I have that paginate an array like sth:

@boards = Kaminari.paginate_array(@user.boards).page(params[:page]).per(1)

How can I fix this problem for sunspot?

Edited: Added Console Test

1.9.2-p290 :094 > Board.all.size #I get count boards for Board.all
 => 4 

1.9.2-p290 :095 > user = User.first #I get the first

1.9.2-p290 :096 > user.boards.size #I get count for boards belongs to user
 => 2 

1.9.2-p290 :098 > user.boards.solr_search.total # This is the problem :O The result must be 2
 => 4 

回答1:


Problem was fixed

The fix: To Model searchable block you have add the parent for this board :user_id, string if you have mongodb database and integer if you have sql database:

#search
  searchable do
   text :name
   string :user_id
 end

In controller for this case I have add s.with(:user_id, @user.id) and replace @user.boards with the Model name like:

def boards
   @user = User.find_by_slug(params[:id])
     @search = Board.solr_search do |s|
       s.fulltext params[:search]
       s.keywords params[:search]
       s.order_by :created_at, :desc
       s.with(:user_id, @user.id)
       s.paginate :page => params[:page], :per_page => 1
     end

   @boards = @search.results
respond_to do |format|
  format.html { render :layout => nil}# panel.html.erb
  format.json { render json: @boards }
  format.js
end
end

Now works fine :D. Thank you




回答2:


The problem seems to be with SOLR returning more than you expect -- the rest of the code is fine as far as I can see.

First verify that the params[:search] has the value you expect (check the development.log), and if so, you should run rails console and try the search manually.



来源:https://stackoverflow.com/questions/9689531/sunspot-return-all-results-for-a-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!